TExcelTAD
Home › Math for Games › Math for Games Intro

Math for Games Intro

Every moving thing in a game is a little bit of math. A character walking toward a door is a vector added to a position every frame. A turret that tracks the player is an atan2 call. A camera that orbits a car is a matrix. A coin that bobs smoothly is a sine wave, and a forest that looks hand-placed is noise.

You do not need a math degree for any of this. Game math is a small, practical toolbox: about a dozen operations that you will use every day, plus a handful of tests (rays, boxes, spheres) that you will use every week. This course teaches that toolbox by writing it yourself in short C++ programs you can run, then shows how the same operations look in Unity, Unreal, Godot, Three.js and GLM.

What you will learn

Section The question it answers
Vectors Where is it, which way is it going, is it in front of me?
Angles How far do I turn, and in which direction?
Matrices How do I move, rotate and scale whole objects, and attach them to each other?
Spaces How does a point in a model end up as a pixel on screen (and back)?
Rotations How do I store and blend 3D orientations without gimbal lock?
Interpolation and curves How do I move smoothly, at any frame rate, along a path?
Geometry and collision Did the bullet hit? Are these boxes overlapping?
Randomness and noise How do I get variety that I can reproduce?
Engine mapping and Toolkit What is this called in my engine or library?

How the examples work

The runnable examples are C++20, compiled and run on Compiler Explorer when you press ▶ Run. Each example defines the tiny structs it needs (Vec2, Vec3, Mat4, Quat) right there in the file, so you can see every line of math. Real projects use a library such as GLM, but writing a vector type once is the best way to understand what the library is doing for you.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>

struct Vec2 { double x, y; };

Vec2 operator+(Vec2 a, Vec2 b) { return {a.x + b.x, a.y + b.y}; }
Vec2 operator*(Vec2 v, double s) { return {v.x * s, v.y * s}; }

int main() {
    std::cout << std::fixed << std::setprecision(3);
    Vec2 position{0, 0};
    Vec2 velocity{3, 1.5};          // units per second
    double dt = 0.25;               // a quarter-second frame, to keep the numbers small

    for (int frame = 1; frame <= 4; frame++) {
        position = position + velocity * dt;
        std::cout << "frame " << frame << ": (" << position.x << ", " << position.y << ")\n";
    }
}
frame 1: (0.750, 0.375)
frame 2: (1.500, 0.750)
frame 3: (2.250, 1.125)
frame 4: (3.000, 1.500)

That loop is the heart of every game: position = position + velocity * dt. Most of this course is about computing better velocity values, better orientations and better questions about where things are.

Numbers are printed with std::fixed << std::setprecision(3) so the output is the same on every compiler. When a result could be a tiny negative number such as -0.0000001, the examples clean it up to 0.000 so you never see a confusing -0.000.

Some lessons also have live canvas visualizations written in plain JavaScript. Drag the mouse or move the sliders and watch the math change.

Conventions used in this course

Every engine picks its own conventions, and mixing them up causes most "my model is upside down" bugs. Inside this course we use one set and stick to it:

  • Right-handed, Y-up 3D coordinates: +X right, +Y up, +Z toward the viewer, so the camera looks down -Z. This matches OpenGL, GLM, Three.js and Godot.
  • Column vectors: a matrix transforms a vector as M * v, and A * B * v applies B first.
  • Radians for every angle passed to sin, cos and friends; degrees only for printing.
  • Units are meters and seconds unless a lesson says otherwise.

The Handedness lesson near the end shows how Unity (left-handed, Y-up), Unreal (left-handed, Z-up, centimeters) and the rest differ, and how to convert.

Note: Formulas are written in plain notation such as a · b = ax*bx + ay*by, |v| for length and v̂ for a unit vector. If a line of math looks dense, read the C++ right below it: it says the same thing.

Prerequisites

You should be comfortable with basic C++ (structs, functions, loops, operator+). The C++ course's lessons on structures and operator overloading cover everything needed. High-school algebra is enough math to start; everything else is introduced when it is needed.

Test yourself

What does the update position = position + velocity * dt do each frame?

  1. Teleports the object to the velocity
  2. Moves the object by the distance it covers in this frame's time
  3. Normalizes the velocity
  4. Rotates the object

In this course's right-handed, Y-up convention, which way does the camera look by default?

  1. +Z
  2. -Z
  3. +Y
  4. +X

Why do the examples print with std::fixed << std::setprecision(3)?

  1. It makes the math more precise
  2. It makes the printed output identical across compilers and runs
  3. C++ cannot print doubles otherwise
  4. It rounds values toward zero