fbpx

You don’t need a lot of math to program. What you do need is usually simple. But you do need some. Don’t worry, it’s not that hard and I’ll explain it so you can understand. Game development probably needs a bit more math than you might guess.

Let’s start with a point. It’s a simple concept that really just represents a position. A point has no dimensions itself. In other words, it has no length, width, or height. It just exist at some location.

It’s the location that has dimensions. If we’re talking about a point in a one dimensional world, then there’s only two directions, left or right. Or, depending on how you want to view the line, it could be up or down. Or, if you can imagine that you are inside the line itself looking down it’s length, then you really have forwards and backwards as the two directions you can go.

Once you know the directions, you have to decide how far you want to travel. We normally think of a line divided into equal measures like a ruler. If you start out at position zero and move forward one unit, then you can say that you’re now at position one. Moving backward from there by two units will take you back to position negative one.

Everything is relative. We think of a ruler as having a specific beginning at one end that we call zero. But if you were inside that line that extends forward and backward in infinite amounts, then where is zero? Well, it’s wherever you want, really. The main thing is to be consistent. Once you pick a spot that you consider to be zero, then stick with that. Everything else will be based on that point.

Listen to the full episode to learn about 2D planes and 3D and even 4D spaces. You’ll also learn why the indices are drawn 90 degrees apart from each other and what this really means. Or you can read the full transcript below.

Transcript

But you do need some. Don’t worry, it’s not that hard and I’ll explain it so you can understand. Game development probably needs a bit more math than you might guess.

There’s a lot of things that get thrown in a game, sure, that you need to calculate where or what it’ll hit. But there’s math involved in just drawing the game objects so they look good.

Things such as cloth billowing in the wind or reflections in the gentle waves of a pool of water are details that are harder to get right. They’re also things we notice if they look wrong or fake.

It’s always a struggle with programming to decide how detailed you need to be. More detail either means more data or more computations. Less detail means flat surfaces that should be curved, or trees that all tend to move in the same direction at the same time, or just skipping things completely like deciding to not bother with shadows.

There’s lots of tricks and shortcuts that have been developed over the years. And then there’s that fact that computers have gotten faster so games can consider it possible to include details that would have been impossible years ago.

It’s harder to do these calculation in a game than it is in a motion picture. A game has a game loop that’s very sensitive to timing. A movie can take all the time you need to draw each image. It might take years to record and edit a major motion picture. But it takes you exactly 1 minute to play a game for 1 minute. And in that 1 minute of game time are 60 seconds that need 60 frames in order to look good. That’s 3600 frames that need to be drawn in 1 minute.

Then there are games that simplify things on purpose. I mean, if the whole world is made out of squares, then it really simplifies a lot of calculations.

I’ve always said that if you can’t get something perfect, then people will notice. Unless you make it so obviously imperfect that you can say you did it that way on purpose.

Okay, I’m getting a bit off topic here. I wanted to explain some math terms today that will help you understand how to program games. Even if you use a library where all these things are already taken care of for you, a basic understanding will still help you build better and more believable software.

Let’s start with a point. It’s a simple concept that really just represents a position. A point has no dimensions itself. In other words, it has no length, width, or height. It just exist at some location.

It’s the location that has dimensions. If we’re talking about a point in a one dimensional world, then there’s only two directions, left or right. Or, depending on how you want to view the line, it could be up or down. Or, if you can imagine that you are inside the line itself looking down it’s length, then you really have forwards and backwards as the two directions you can go.

Once you know the directions, you have to decide how far you want to travel. We normally think of a line divided into equal measures like a ruler. If you start out at position zero and move forward one unit, then you can say that you’re now at position one. Moving backward from there by two units will take you back to position negative one.

Everything is relative. We think of a ruler as having a specific beginning at one end that we call zero. But if you were inside that line that extends forward and backward in infinite amounts, then where is zero? Well, it’s wherever you want, really. The main thing is to be consistent. Once you pick a spot that you consider to be zero, then stick with that. Everything else will be based on that point.

That’s fine if you’re all by yourself. The moment you have two people living in the line, either they both have to agree on where is point zero or they have to always map between one person’s viewpoint and the other. It gets more complicated if the units of measurement are also different. I mean who got to say that an inch was to be as long as it is. Or that a meter is as long as it is.

How you represent your dimensions is very important. No matter how many dimension you have. The metric system is much better organized around 10s just like the way we count. And it sticks to that factor of ten. Not at all like how there are 12 inches in a foot and 3 feet in a yard. And do you remember how many yards are in a mile. I don’t. And I grew up using miles my whole life. It’s just not the sort of information that I want sitting around cluttering up my mind. But I know there’s a thousand meters in a kilometer.

Another thing to consider that will be important soon is to think about what would happen if the line that you’re imagining is your home was really a thread. And that thread was tangled up and twisted all around. We can picture this. But try for a moment to imagine that your whole world exists inside the line. The only directions you know are forward and backward. If your world is actually curved because the thread spins in a loop for a while, you don’t see this. Inside the line, it’s still straight ahead or straight back.

Just keep this thought in mind. To make things simple, we’ll stick to lines really being straight and unbendable.

Alright, let’s move on to two dimensions. This is a plane. A flat surface. At least it’s flat to anybody living in the plane. We normally think of the ground as being flat but that’s just because the earth is so much bigger than our viewpoint typically allows us to see. So it’s possible to stretch and twist a plane so it’s no longer perfectly flat. Just like the line, we’ll stick to planes being perfectly flat.

Now we have two dimensions to work with. Typically called x and y. How are these dimensions related to each other? We normally look at the axis lines. These are lines like the one dimension ruler that mark off the distances. The lines are drawn so the y axis passes through the x six zero point. And the x axis passes through the y axis zero point. Both zero points meet at the origin of the dimensions.

The axis lines are also drawn so they’re perpendicular to each other. This means the lines make a 90 degree angle. But don’t focus so much on the angle. The most important point is that if you move along one axis only, then you shouldn’t be moving along the other axis. The best way to show this is to draw the axis lines at 90 degrees.

We’re used to this kind of arrangement. From graph paper to maps, we use this arrangement all the time. You can get to any point from the origin with a straight line. But we usually represent the location of this point by giving both the x component and the y component. It doesn’t mean that you have to travel along the x axis and then along the y axis to get to the point. The coordinates are there just to identify the location of the point.

While we’re talking about planes, let me explain what a normal is and what it’s used for. The normal will extend into the third dimension but that’s okay. I’ll explain it now. If the plane is flat like the ground, then a normal is like a flagpole sticking straight up into the sky. It doesn’t matter how tall the flagpole is. You’ll usually find normals will be one unit long. That makes a short flagpole, I know. What a normal does is point straight out from the plane so that your program will know which direction the plane is facing.

This direction is used to figure out how to color a flat surface. Light can behave differently to our eyes when it reflects off a surface as that surface changes its direction. And a normal is a good way to keep track of the direction. That’s why the length of the normal is not important. It’s easier to keep track of and specify the direction of a line to figure out which way a surface is facing than track the surface itself. And normals are even more important when we get to curved surfaces.

What about 3 dimensions? Again, we add another dimension axis line in such a way that traveling along any axis means no movement along any of the other axis lines. We can again use a line at 90 degrees from both the x and y axis. This third dimension is called the z axis. You can draw the three axis lines on paper using angles other than 90 degrees. Just remember that the 90 degrees is not the important part. It’s the isolation between the axis lines that’s really important.

A point in 3 dimension space now needs three coordinates to specify properly. And this is also where some game engines start to disagree on which axis points where. If you have a piece of graph paper with x and y axis drawn on it, then to get the z axis you’d need to poke your pencil through the paper so it sticks straight out of the paper.

Now, do you hold this paper up in front of you so that x represents left and right while y represents up and down? Then z will represent forward and backward directions. Or do you lay the paper flat on the ground in front of you so that x still shows left and right, y is now forward and backward, while z is up and down? There is no right or wrong answer. It’s just something you’ll need to keep in mind. It’s just another difference of viewpoints that need to be managed just like the position of the origin and the length of the units.

What about 4 or more dimensions? Well, these are a bit more difficult to draw. I mean, it’s already hard enough draw three dimensions on a piece of paper. Adding another dimension is possible but at some point, the extra dimensions stop being used to represent real things and are used anytime you need a new set of numbers that can change without affecting the existing numbers. Remember, it’s not the angle that’s important but that movement along an axis should not include movement along any other axis.

You can always move in multiple dimensions at once. This is like walking diagonally to a point instead of along each dimension one at a time. Each axis just lets you keep track of positions separate from the other axis lines. And anytime you need to be able to twist or curve your existing set of dimensions without those dimensions being aware of the curve, all you have to do is add one or more extra dimensions.