Math in Video Game development

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Math in Video Game development
by on (#217987)
How many of you think math is important in video game development? I've been trying to build my own game that involves throwing objects/calculating trajectories with vectors and matrices however, I've been struggling to get it work. It has been a bit too long for me to recall on them and math was never really my strongest subject. The tutorials I've found on YouTube aren't close enough to what I want to do with my game. I tried just creating it myself with some math tutorials like KhanAcedemy, Paul's Online Notes, and Study Pug but I am quite lost at the moment and can't get it work. Do you guys know any websites closely tied to developing games? That would be lots of helps to me and others looking to start building their first game.
Re: Math in Video Game development
by on (#217989)
I think math is certainly important in video game development (and also many games other than video games too), although what kind of math is important is difference depending on what kind of game. I did not find math a difficult subject though; it is easier to understand than language arts and social studies and so on. Also, I do not know the websites closely tied to developing games, as mostly I have figured out the things by myself, looking up stuff on Wikipedia and so on when I was unsure.
Re: Math in Video Game development
by on (#217991)
It depends on the type of game, but I do feel a basic understanding of math and physics helps. While working on a Zelda-type game, everything was very simple and basic. On the other hand, a platformer project was a lot more complex. Some of that was by choice, such as having elastic collisions instead of simple ejections. It's really a matter of how far you want to take the simulation vs approximating things.
Re: Math in Video Game development
by on (#217994)
I don't really think you need anything more than trigonometry for simple 2D games? If you don't have one already, make a sine/cosine table because it's essential for some more complicated (but still relevant) problems.

Now, with 3D graphics, it's probably a whole other story...
Re: Math in Video Game development
by on (#217995)
Velocity is the integral of acceleration, and displacement is the integral of velocity. That's calculus. And when acceleration depends on some other variable, that's differential equations.
Re: Math in Video Game development
by on (#217996)
tepples wrote:
Velocity is the integral of acceleration, and displacement is the integral of velocity. That's calculus. And when acceleration depends on some other variable, that's differential equations.


But you can do all of that pretty easily without having the slightest clue about what all those words mean.
Re: Math in Video Game development
by on (#217997)
gauauu wrote:
But you can do all of that pretty easily without having the slightest clue about what all those words mean.

You can do a lot of things without knowing much math. That doesn't mean it's not useful or applicable. If you don't know what you don't know, it might be hard to see what you could have used from it. Understanding more math can give you a lot of tools to work with that might do a job better or more easily.

Like, someone can give you a recipe for a parabolic jump, by adding acceleration to velocity to position. That's fine at first, but when you need to modify it, analyze it, do something more complicated... it can make the difference between tweaking values all night until the curve "looks" the way you want (and possibly giving up), and being able to just dictate a curve that, e.g. passes through specific points, has the velocity you want at those points, stays in the air for a specific amount of time, is attracted to a moving magnet, etc... or sometimes it helps to know if it's actually impossible to build the curve you're asking for. Instead of a long period of trial and error, then giving up, you might have realized much sooner that you should try something else.

Here's a basic list of areas of math that I think are pretty useful to games. This is also pretty much the same categories of math you'd find as requirements for a general Computer Science degree:

  • Geometry - The basic stuff of shapes, angles, measurement, etc. Trigonometry, i.e. understanding angles and circles has a lot of applications elsewhere too.
  • Linear Algebra - Especially important for geometry, intersections, collision, motion, etc. Matrices and other methods to understand the relationship between 1D, 2D, 3D, and higher dimensions.
  • Calculus - Useful for anything to do with curves, motion, curved surfaces, shapes. Relationships between surface are and volume, questions about how much or how long for various processes that happen over time. (Combined with Linear Algebra this is kind of the backbone of a lot of 3D rendering topics.)
  • Discrete Math - If Calclus deals with smooth, continuous curves, Discrete Math is instead individual steps. Integers. Graphs and connections. Ways of dealing with collections of things. Insights into modular arithmetic.
  • Statistics & Probability - Probability, tendencies, odds, expectations. Not just making a prediction but having an idea of how reliable it is. Extremely applicable to AI, and provides a lot of tools for analysis. (Strongly correlated with Discrete Math as well.)
  • Formal Logic - Sometimes is just really helps to be able to work through logic mechanically.

All of these are well worth developing a basic understanding of. Eventually the deeper stuff becomes more specialized and less generically applicable, but the foundation level of each of these topics is very practical knowledge.
Re: Math in Video Game development
by on (#217999)
If you understand this, you're doing okay.
Re: Math in Video Game development
by on (#218002)
Rahsennor wrote:
If you understand this, you're doing okay.

That author is pretty good at burying a useful tip in rhetoric that makes makes me not want to read it at all. (And completely ignores some reasons/situations where it might be completely appropriate to use Euler integration as well.)
Re: Math in Video Game development
by on (#218016)
rainwarrior wrote:
That author is pretty good at burying a useful tip in rhetoric that makes makes me not want to read it at all. (And completely ignores some reasons/situations where it might be completely appropriate to use Euler integration as well.)

I had a much better link bookmarked but it's 404 now. Verlet integration is still the most complex math I've used in actual game code, though, which was the point I was trying to make.
Re: Math in Video Game development
by on (#218017)
Everything in a computer is a number. Everything it does is a number, everything you make it do is modifying numbers. So basically a computer is a glorified calculator and so you really have to understand maths. Putting numbers in and out of A,X and Y is Algebra. Boolean Algebra is the only way you can understand AND/OR/EOR BNE/BEQ/BCC/BCS/BPL/BMI etc

If you want a smaller target set, taking what rainwarrior said, I would put Boolean Algebra and Linear Algebra as the "must" haves, and then the others are "as needed".
Re: Math in Video Game development
by on (#218021)
Rahsennor wrote:
rainwarrior wrote:
That author is pretty good at burying a useful tip in rhetoric that makes makes me not want to read it at all. (And completely ignores some reasons/situations where it might be completely appropriate to use Euler integration as well.)

I had a much better link bookmarked but it's 404 now. Verlet integration is still the most complex math I've used in actual game code, though, which was the point I was trying to make.

Ah I think half the content in the article is really good, especially the diagrams, and I think the concept is explained well. It's just that the other half is extremely and pointlessly arrogant, and wrongly positions Euler as invalid/obsolete, which it really isn't.

e.g. in NES development, it's probably a good tradeoff of accuracy vs CPU usage to stick with regular Euler integration in most cases. (...and if you've got CPU to spare, RK4 might be more applicable too? This Verlet method is a middle ground, between "fast" and "accurate".)

Anyhow, sorry for the off topic critique of that article.

Oziphantom wrote:
If you want a smaller target set, taking what rainwarrior said, I would put Boolean Algebra and Linear Algebra as the "must" haves, and then the others are "as needed".

I'd say for 3D graphics and animation, Calculus and Linear Algebra are a must. If you were making a turn based RPG, may Stats and Probability would be the most important one on my list. It depends what kinda stuff you want to do, but I tried to give a brief description of how each is used... kinda difficult to go into everything (and BTW, I think Calculus is still fairly useful in an NES/2D context too).
Re: Math in Video Game development
by on (#218026)
rainwarrior wrote:
Oziphantom wrote:
If you want a smaller target set, taking what rainwarrior said, I would put Boolean Algebra and Linear Algebra as the "must" haves, and then the others are "as needed".

I'd say for 3D graphics and animation, Calculus and Linear Algebra are a must. If you were making a turn based RPG, may Stats and Probability would be the most important one on my list. It depends what kinda stuff you want to do, but I tried to give a brief description of how each is used... kinda difficult to go into everything (and BTW, I think Calculus is still fairly useful in an NES/2D context too).

Code:
World->Screen->MetaSprite->Sprite
             \
              ->MetaTile->Tile
Space conversion and concepts is something I see a lot of people new to game dev really struggle with, and I feel having some basic grasp of Linear Algebra would really assist them in working through the logic and understanding how to think in terms of multiple Spaces. Even if you are doing 2D in 3D you need to be able to get to the sprite origin and then +/- to make the billboard.
I agree you will probably end up coding more Calculus but you will always be thinking and planning in LA.

As per the original topic, there are a number of books on the subject.
There is a dedicated Maths for Games Programmers book. I've not read it though. All of the Prima La Mothe series have a section that covers the basics of maths. If you want a from "this is a vector and this is a point" all the way down to "now you have made a software perspective correct textured rasterizer" this "even thicker than Petzold" book covers it all in great detail https://www.amazon.com/Tricks-Programmi ... 0672318350 I would recommend any La Mothe book though he has a great talent for making it very approachable. Even if it is how to do voice control with a resister based DAC on a parallel port with debug strings on 2nd MDA adapter ISA card ;)
Re: Math in Video Game development
by on (#218045)
rainwarrior wrote:
Ah I think half the content in the article is really good, especially the diagrams, and I think the concept is explained well. It's just that the other half is extremely and pointlessly arrogant, and wrongly positions Euler as invalid/obsolete, which it really isn't.

Yeah, I linked it for the diagrams. It wasn't until I saw them (or rather these - archive.org to the rescue again) that I really understood why Verlet was more accurate. I knew the math checked out, but seeing the difference made it stick.

For anyone actually following my links, see also position Verlet. Not having a velocity term to keep updated can be quite handy.

rainwarrior wrote:
e.g. in NES development, it's probably a good tradeoff of accuracy vs CPU usage to stick with regular Euler integration in most cases. (...and if you've got CPU to spare, RK4 might be more applicable too? This Verlet method is a middle ground, between "fast" and "accurate".)

In a basic platformer the only acceleration is gravity, which is constant, so you need a second order integrator. Less will always be inaccurate and more won't buy you anything. (I'm not counting collision response because you generally don't want that to be accurate anyway, for gameplay reasons. Most players would like to stay on the ground unless they press jump.)

But for the NES I agree it's probably overkill. You can't beat Euler for simplicity.
Re: Math in Video Game development
by on (#218052)
For a NES Euler is mostly overkill ;) For most things Pos += Delta is all you need.
Re: Math in Video Game development
by on (#218056)
The cool thing about math is just that you are thinking about things in a very logical way and because of this is applies to almost everything. When you code things it important to think in very precise ways... math helps with this. If you are doing something very specific then you might need math.

In the original Doom for PC and Wolfenstein they used special techniques to give the illusion of 3D. These are areas which you would benefit from having a good math background.

If you want to get good at math just find some free tutoring online community and volunteer. Teaching others is one of the fastest ways of learning new things.

Oh by the way.... math major here. :)
Re: Math in Video Game development
by on (#218059)
Oziphantom wrote:
For a NES Euler is mostly overkill ;) For most things Pos += Delta is all you need.

That is (explicit) Euler. Find the first derivative of the function at the current time and multiply it by the time increment to get the delta (these two steps can be combined pretty easily on NES since the derivative is usually trivial and the time increment is constant), and then just add the delta to the value of the function at the current time to get the value of the function at the next time. I'm pretty sure this is the simplest possible numerical integrator.