Introduction To Game Math

## When are We Ever Going to Use This Stuff?

Perhaps you remember people in your math classes in school saying "When are we ever going to use this stuff?" Maybe it was even you saying it.

Let me give you RB's Principle of Mathematics:

If you know how to use it, you'll find ways to use it.

Math is used everywhere, and if you are interested in making games, you'll use it all of the time. You'll use all sorts of math, from basic arithmetic to calculus. Here's the thing though. The more math you know, the better off you'll be. But you can get by with only a small understanding of math, and expand your knowledge as you go. So don't be too worried if math isn't exactly your strong point.

Now, just to help you understand where I'm coming from, I have been a math tutor in college for virtually all levels of math, from the people who are struggling with the basics, through the more complicated calculus, differential equations, probability, and so on.

## What You'll Want to Know

Games use all sorts of math. There's no way around it. In fact, any amount of programming will require basic math. In this section, I want to briefly go over the topics in math that you'll find useful. Obviously, I'm not going to be able to cover everything in these math tutorials, but hopefully, it will be enough to give you the basics.

Let's look at the broad categories of math that you will likely use as you make games.

### Basic Arithmetic

You'll definitely need a strong understanding of basic arithmetic. Things like adding two numbers, subtraction, division, and multiplications are absolutely necessary. Also, doing this with fractions and decimals is a must as well.

### Geometry and Trigonometry

Geometry, is the mathematical study of measuring things like size and position of objects in the world (or your game world), and Trigonometry is the more specific category of geometry that deals with the size, position, and other properties of triangles. Both of these will come in handy in your game. It will be useful to be able to measure distances between objects and angles between them. Coordinate systems, radians vs. degrees, and various forms of object intersections will provide useful as well. As I mentioned earlier, these are all things that you can learn as you go, so don't be too worried if you haven't done anything with them before, or struggled with them in school.

### Linear Algebra

Linear algebra is a different sort of mathematical creature than most. It deals specifically with matrix stuff. The plural form of the word 'matrix is 'matrices'. A matrix is simply a two-dimensional array of numbers. They are used frequently in computer games, specifically for transforming objects (moving, rotating, and scaling them) and they are also used for setting up view and projection information, to enable the rendering system of your game to know where to draw things on your screen.

Many of the people that I've taught them how to make games have expressed some concern about linear algebra and matrix stuff. The good news is that XNA, like any other useful game engine or toolset out there, takes care of all of the dirty work for you. With just a basic understanding, you'll be set, and you can dig deeper at your own pace.

### Calculus

OK, I'll be honest, I have yet to program a derivative or integral for a game, or even had an explicit need for it. But I can't count the number of times that I have put the basic principles of calculus to use in a game, especially for things like acceleration of a ship, and updating the velocity and position accordingly. Calculus is one place where if you understand it, you'll find ways to use it.

### Probability & Statistics

Probability and statistics are two areas where not every game will use them, but occasionally, a game will use them heavily. Having a basic understanding of how to calculate the odds of a particular event happening can come in useful. I remember playing Civilization 4, and when you are about to get into combat, a little box in the corner displays the "Odds of Success" to give you an idea of how likely you are to win. Because the developers had at least a basic understanding of probability, they could calculate the odds and display it for the user. This was an incredibly nice feature of the game.

## What's Next?

The first place we are going to start is to take a look at some of the built-in features that XNA provides to help you do basic math. We will first look at the .NET Framework's Math class, and then the XNA MathHelper class. After that we can look at some of the more common things that people need to do with math, and how to accomplish them in a game.

 Having problems with this tutorial? Try the troubleshooting page!