# 2D Game Programming – Thoughts on Math

Now that I’ve been doing game development for around 14-years (although many of those years were not serious because I was in my adolescence), I figured I should give my thoughts on the mathematical side of it. I’ve never been one to say that I love math, although I did well in school without trying (that’s a story for another day). The highest math level I ever reached in school was Geometry, which as far as I was concerned in my past, was the math level you had to reach to graduate.

When I started getting into developing games seriously, that is, not just using tools to do everything for me; I was unpleasantly greeted by the fact that I had a way harder time with the math than I thought I would. So with that, I started looking into what I needed to produce what I wanted to produce. Here’s a minimal list of the topics I had to touch upon and understand.

• Matrices [Rotation, Scaling, Translating] (Linear Algebra)
• Vectors (Linear Algebra)
• Cosine, Tangent, Sine … also Cosecant, Secant, and Cotangent (Trigonometry)
• Arc-Cosine, Arc-Tangent, and Arc-Sine, which are inverted versions of the above (Trigonometry)

So, guess what, I had zero experience with any of that. Absolutely no experience at all. My math level stopped at geometry, which is still the most useless math ever taught. Where I live (US), here’s the math system.

1. Elementary/Fundamental Math (+, -, /, x)
2. Pre-Algebra
3. Algebra
4. Geometry
6. Statistics / Trigonometry / Pre-Calculus (all-in-one class)
7. Calculus 1
8. Calculus 2
9. Calculus 3
10. Statistics 2
11. Linear Algebra
12. Calculus 4
13. Abstract Algebra (A.K.A. Modern Algebra)
14. Differential Equations
15. Number Theory

This means, if this were a video game, I’m basically on level 4 out of 15. Being realistic, unless I planned to become a mathematician, all 15 levels are not really needed. On the other hand, to my surprise, 2D games required some pretty serious high level math understanding. This was a real downer for me, but I didn’t care and I started studying anyways, because I can.

So, I created things here and there that were pretty neat, but wrong. Very wrong. It wasn’t until about 6 months down the road that I started really getting a grasp of what on earth I was doing. I’m sure if I had gone through the proper channels, if you will, that it could have been a lot easier, but I didn’t. I made it as hard as possible on myself to learn these things.

What was easiest to grasp? Vectors. Vectors were really easy for me to grasp.
What was the hardest for me to grasp? Matrices, because they required really understanding (Arc)[(Co)Sine/Tangent].

About [2D] vectors, the reason Vectors were so easy for someone like me, or really, anyone I would think, is because it’s just a point (or whatever else you want it to be). Which is dependent on how many dimensions the vector represents.

• 1D = x
• 2D = x, y
• 3D = x, y, z
• 4D = x, y, z, w

Really, the variable names don’t even matter. I could have called it point a and b, but for standardization, x and y make it easier. As long as you understand algebra, understanding what a vector is, is pretty simple. Draw a grid, put a point down, figure out it’s x and y values, and you have solved the location of a vector point. (Woo!)

If you took a proper algebra class, the whole “rise over run” thing should have been taught along with the Pythagorean theorem. These both help to describe a vector and their magnitude and angle.

Let’s take a vector of (x: 1, y: 1). Just simply (1, 1).

Using the Pythagorean theorem of a^2 + b^2 = c^2, calculate: x * x + y * y = magnitude squared
1 * 1 + 1 * 1 =
1 + 1 = 2 squared
square root of 2 = 1.41421356237
magnitude = 1.41421356237

That’s right, without ever needing to jump through the hoops in math, you can calculate the magnitude/length of a vector at the lowest algebraic level.

How about rise over run? y/x?

The arc tangent of y/x will yield you the result 45 degrees. Which is the angle of the vector. This one requires a bit more than rise over run, but you see how it really just builds on the simple stuff.

All-in-all, vectors became easier and easier to understand the more I used them.

I still have issues with matrices. The issues involved aren’t so much understanding them, as they are making sure I get the math right.

Let’s look at how are matrices rotated.

Well, let’s make this simple just look at the vector (1, 1) and rotate it 90 degrees. Which, if we understand how the angle is found, then to do the opposite is no problem.

To find an angle (in programming)
angle = (360 + atan2(y, x)) % 360

To set from an angle (in programming)
magnitude = sqrt(x * x + y * y) vector_x = cos(angle) * magnitude vector_y = sin(angle) * magnitude

This translated into actual math means
magnitude = 2 angle = 45 + 90 = 135 vector_x = cos(angle) * magnitude = -1.41421356237 vector_y = sin(angle) * magnitude = 1.41421356237

That is, we went from pointing up-right to pointing up-left. So if you can rotate a vector, then guess what, you can rotate a 2D matrix representation. Which uses the same exact math. Look below.

$R(\theta) = \begin{bmatrix} \cos \theta \ \ -\sin \theta \\ \sin \theta \ \ \ \ \ \cos \theta \\ \end{bmatrix}$

$R(135) = \begin{bmatrix} -0.707106781187 \ \ -0.70710678118 \\ 0.707106781187 \ \ \ \ \ -0.707106781187 \\ \end{bmatrix}$

0.707106781187 is the normalized form of 1.41421356237. That is, when added up 0.707106781187 plus it’s counter part, will add up to -1, 0, or 1, which is 0, 90, 180, and 270 degrees.
$\sin^{-1}(0.707106781187)\ +\ \cos^{-1}(-0.707106781187) = 180^\circ$
So don’t let that number throw you off.

While showing what I’ve learned, I may or may have not taught you anything, but as far as game making goes. Yes, you do need to learn Vectors, Matrices, (Arc)Sine, (Arc)Cosine, (Arc)Tangent, and some other things here and there. I once thought that these would be really hard to learn considering that my math isn’t spectacularly high, but it turns out I was wrong. I got by just fine. So if someone going into making games wonders if they have to understand a certain level of math, the answer is yes, Algebra. Then you can learn the other things after. Because going up the ladder is really not necessary unless you plan to go into 3D, which I’m not particularly interested in.