DEV Community

Frodo Alaska
Frodo Alaska

Posted on

Gamedev Math: Not Just A Tool But A Necessity

Math, My Love

I never liked math. My everlasting enemy in school wasn't the bullies (I was the bully). It wasn't the drugs (I was the drugs). It certainly wasn't Becky (I was, in fact, Becky). It was the maths. You can put me up on a podium, tell me to talk about history the rest of the day with a slim jim up my ass, and I will happily oblige. However, if you ask me to find the square root of your mother's abdominal area... I wouldn't have a clue (but your mom is so fat anyway so that would be impossible to calculate such a thing). I thought I was finally done with math when I left school. I did not want to see those symbols ever again in my life... but then I fell in love. I fell in love with a little thing called game dev. You might have heard of it.

Even though I used Unity throughout my first year of game development, I still had to use so much math. It was an engine for god's sake! Why did I have to get the tangent of the angle between the mouse's position and the player's position! I did not care! I just wanted to make the game. Despite my best efforts to escape the hell that was math, I still used math as much as you use hair gel to hide the fact that you're slowly getting bald. And I had forgotten almost everything! I did not pay any attention to school when it came to math. I felt like I was a monkey trying to use piano keys as a way to wipe my bottom area.

I watched tutorials whenever I was stuck with a particularly annoying math-intensive problem. Especially if it had something to do with rotations. I watched some Brackeys here and some CodeMonkey there. Yet, I still had issues. Perhaps it was an issue that I did not find an immediate answer to (although those were very rare). Or, and this was the most common, I just did not remember the solution because I did not understand it fundamentally.

It was really frustrating. It made me hate maths even more than I already did. Something had to change... and I'm not talking about your drinking habits.

Socrates

What the bald guy? No, not him, silly. I'm talking about the greatest math library ever created. Shameless plug.

Yes, I made a math library completely from scratch using nothing but my own two hands (C++) and a few pictures of your mom to keep me company. The person who was having a problem with rotations back in Unity is dead now. I killed him... no, officer, I meant figuratively.

After reading a few books and watching a couple of amazing tutorials, I was able to manifest myself a small and simple math library. Is it the best math library out there? Absolutely not. Is it the most optimal and fastest? What? Are you kidding? Of course not! But I don't really care. I made it because I wanted to learn and boy did I learn a lot.

Not only did I understand what libraries like glm do under the hood, but it also gave me an appreciation for these kinds of libraries and just the general math of it all. Getting deeper (that's what she said) into the maths and really understanding what makes a projection matrix work the way it works was really fascinating and incredible. If I told my 15-year-old self that I would be making a math library from scratch... well, first, I would ask what the hell a math library is and then probably be amazed. I would also punch my 15-year-old self because he was a fucking moron.

But wait. Was it necessary? Did I really have to go that far for math? Couldn't I just use something like glm and go about my day? Well, obviously yes. In fact, that was the case for so long. So why? Well...

Useful Is Not Necessary

Math is a very useful tool to have in your toolbelt as a game developer. Personally, it has helped me to manifest my sick ideas into reality (a game about Donkey from Shrek getting trapped in Alcatraz). It made me a better programmer overall. Especially when I'm programming in a lower-level environment. Yet, a useful tool is not a necessity. It's a cool thing to have. It'll help you in your journey. But will you die from dehydration if you don't have it? No. Not drinking water will do that to you. That is a necessity. And while we can argue about how nothing is really a necessity in the current landscape of game development where engines are available in the thousands. However, I would argue that--even with engines--math is a very necessary tool to have. It won't kill you if you don't have it, but you will suffer without it.

I've been on both sides of the coin. I knew next to nothing about math and I also, now, know a lot about it. I can tell you for sure that I enjoyed my time, making games with the math knowledge than without it. Things were easier to interpret. I could intuitively understand and implement things like camera projection, plane vs. line intersection, or even finding the intersection point between your mom's belly and infinity.

Listen, I am not telling you to go out, reincarnate Issac Newton from the dead, and demand from him to teach you the meaning of life using an apple. However, knowing even a little bit of math goes a long way. At the end of the day, we--game developers--are trying to become the best damn game makers we can possibly be. We are not here to make a couple of bucks out of a cheap rip-off and call it a day. We are here to learn. You need to understand that game development is a complex and difficult endeavor. And, if you're not prepared for it, you will get fucked in the ass more than I would if a goth... well, let's move on from that.

Math is not just a useful tool, believe me, it's a necessary tool to have. But how much math do you need?

Too Much Or Too Little

I can give you a short answer and I can give you a long answer. Since this is my property, I will give you a long answer. So sit down.

Not everyone enjoys programming. I get it. For a lot of people, it doesn't make sense, it's very unintuitive and very hard. And, if you're specializing in another field in game development, it's totally okay. Not every game developer has to be John Carmack (all hail Carmack). Especially if you have a programmer to help you, you don't really need to know much of the math. Not really at all. It's cool if you know some of it but you're not forced to learn it.

However, if you're a programmer, then things are different. You need the math. If you program your game on your own or for someone else, you need the math. Hey, I'm not saying you need to learn matrices. But, I'm saying that you need to know more than just the basics of math. The dot product, the cross product, tangents, cosines, sines, getting the distance between two vectors, the length of a vector, and so much more. You need to know these things. Even if you're not gonna make a renderer from scratch using only OpenGL and some tears, you need to learn the math.

For example, let's say that you're an unoriginal bastard who did not have a single productive day in his life. You decide one day to make a Dark Souls clone. Wow, how original. You really like it when you can sneak up on enemies and get the orgasmic backstab. Now, you'd like to implement that. How would you implement something like that? Well, easy enough. Just go on YouTube and type in the search bar with your useless fingers, "Backstab tutorial Unity". A video pops up, you follow it through, and you somehow--finally--make something work and implement it correctly. Now you have backstabbing in your very original game... but it doesn't feel right. For some reason the movement is stiff. The backstabbing is weird. There are some weird edge cases where the backstab should work but it doesn't. Why? It's because you're a lazy bastard that's why. I'm sorry, though. I still love you.

Math, my friend. It's the savior of all. You would know how to solve that problem without the need to look up a video on YouTube if you knew the math. Do understand, however, that I'm not making a slight at the tutorials on YouTube. No no. Absolutely not. But, these tutorials are only a temporary solution. A short-term and low-effort answer to a much more complicated question.

You don't need to know why exactly the dot product works the way it does, for example. But it would be useful to know what it does and when to use it. Intuitively understanding basic trigonometry and geometry will help you a long way to implement these gameplay features. Who knows, perhaps you'll even be able to make something way better than anyone ever did. And while there are many things you'll need to enhance in order to be a better game programmer, math is certainly on the list.

But what if you want to go further? Further than any Unity programmer has ever gone. Well, unless you're making a game from scratch or you're interested, then you don't really need to venture that far. But, hey, I can show you the way if you want. For a little favor if you know what I mean... anyway.

One word: Matrices. Well, and quaternions. And maybe more than that. Whatever, you get the point. While in game engines we have many tools to help us along our journey, when you're on your own, that is not the case.

If you want to venture deeper into the mines of game development and perhaps make your own game engine, then you'll need to know more than the dot product or the cross product. In this level of game development, you are forced to learn math. Projection matrices, view/clipping matrices, and even physics. But, again, you do have libraries like glm to help you along your journey. However, I still believe you should venture deeper than glm.

Here's the thing about gamedev math in general: it's all there. You just have to implement it. Making a physics library is not hard because of the math. Absolutely not. The formulas are all there. They have been there for hundreds of years. The hard part about making a physics library or a math library is the performance. Making these expensive calculations multiple times in a frame can be and will be very costly. Your job (at this level at least) is to make sure everything runs smoothly while still making the right calculations at the right time.

It helps to know what the glm::perspective function in glm does under the hood to know if it is costly to compute or not. Again, I'm not saying to meditate under your neighbor's balcony naked to truly understand why a perspective matrix works the way it does. That's not your job as a game programmer. That's a mathematician's job. Your job is to take the formula and make it work fast. That would require you to understand what the formula really does under the hood. How does an orthographic matrix get built under the hood? How can I make a skew matrix? How do quaternion rotations work? How do I get RAM for free from GTA 5?

Maths at this level is not only a necessary tool, but it is a crucial step to take.

The Bottom Line

Game development is not a race. Making games requires a wide set of skills harmonizing together at the perfect moment to make a semi-decent game. All we can do here is learn. Learn about the math, the game design, the art, the music, the story, and every other skill required to make a game. I'm not saying you need to be the jack of all trades. But do understand that learning the art of making games is more important than your stupid Legend Of Zelda and Skyrim clone. Take your time. Learn the fundamentals. Don't rush it. You will regret it.

Keep learning. Keep making games. You might make it or you might not. But hey, at the end of the day, you learned a shit ton of skills and made some pretty nice games. Fuck it. That's something worth living for.

Credits: Me.

Top comments (0)