Q: How important do you think it is to learn calculus for game programming? Which math fields are most useful in hobby game development?
A: I needed calculus for classwork, however I have virtually never used it for any of my dozens of freeware and small commercial projects.
That said, I’m not very involved in writing physics or 3D graphics libraries – when I need those features, I reference libraries written by other people who are more knowledgable in those areas. Someone working on rendering features or optimizations for 3D engines might well have more use for calculus than I do.
For most gameplay-related math – jumping, bumping into things, the speed of a shield recharge – simplified approximation suffices. In many cases the sloppier estimation approach is even better than trying to work out proper precision. As one example, the tried-and-true platform game mechanic of “how long the jump button is held after leaving the ground determines jump height” clearly does not in any way reflect the real physics of jumping. It’s more important for a videogame to behave as the player wants (or expects) than it is for a videogame to behave realistically.
Of course, if we were trying to design an authentic flight simulator for military training, or model a car accident for the purposes of studying safety and real-world engineering, this would be a very different story. But for games we can usually just twiddle with numbers until things look and feel right. Videogame spaceships and helicopters move because we add a velocity value to their coordinates every frame – not because of rocket propulsion or Bernoulli’s principle. For 2D game programming, spacecrafts and helicopters only need 1st grade arithmetic.
But beyond basic gameplay interactions, here are the fields of math that I run into most frequently for game programming, along with notes on what I find each most useful for:
- Basic Geometry. 2D graphics programming is a matter of nudging object coordinates around on a plane, then drawing images every frame based on those coordinates. Bounding boxes (comparing coordinate differences) and the distance formula (Pythagorean Theorem) are commonly used to test whether two objects have collided with one another.
- Trigonometry. Sine and cosine are helpful for angle-to-component translation, for example when determining what percentage of a bullet’s total velocity the x-speed and y-speed are given the angle of a firing cannon (see illustration below) – this also applies to an overhead race car. Atan2 is handy for getting an angle between two things, given their relative offset in grid locations – good for getting an enemy to face the player, pointing a simple homing missile, and so on.
- Vector Geometry. The dot product is an incredibly versatile math operation to gain mastery of, even in the simplest 2 vector 2D case. Learn how to use it, and you’ll greatly increase the number of nifty things that you can pull off in game programming. This is useful for reflection off angled surfaces, checking line-of-sight, determining which direction something is facing, and many other common spatial/angle relationships.
Addition (courtesy of fellow indie Tyler Glaiel): Normalizing vectors is another basic and useful aspect of vector geometry. Properly applied, these offer a more efficient way to accomplish some of the same things that trigonometry is commonly used for, including aggressive homing missile logic.
- Boolean Logic. It’s common in game code to want something to only happen either when multiple things are true (if player is on ground AND pressing jump button, then jump) or when at least one of several things is true (if [W Key] OR [Up Arrow] is pressed, then try to jump). At first glance this doesn’t look like math, but when these statements begin to compound there’s are math-like rules that can be used to untangle and simplify boolean logic.
- Simple Algebra. Old-fashioned line-intersection check calculations are great, and trivial to write as a function. This comes up for things like pong AI, which needs to anticipate where an elastically bouncing ball will intersect a screen edge…
… (continued in ebook)
*This entry is now in the Videogame Developer’s Strategy Guide, free with Gamkedo Weekly Check-In.
Learn and practice team game development with Gamkedo Club.
Membership worldwide. Professional support. Proven process.
Subscribe by e-mail to receive weekly updates with Gamkedo.Community interviews and YouTube training videos for game developers!