Game Maths
Vectors, matrices and quaternions — the small pile of linear algebra that every frame of every game runs on.
A Small Amount of Maths, Used Constantly
Nothing here is advanced mathematics. What makes it hard is that it is load-bearing: rendering, physics, cameras, AI steering and animation all reduce to the same handful of operations. Get the intuition once and the rest of the engine stops looking arbitrary.
The practical goal is not to derive these from first principles. It is to know, immediately, which operation answers the question in front of you — how far, which way, which side, how much of this direction is in that one.
Vectors
A vector is a direction and a length. The same three numbers are read as a position in one context and a displacement in another, and the difference matters: subtracting two positions gives you a displacement, adding two positions gives you nonsense.
| Operation | Answers |
|---|---|
| Length (magnitude) | How far |
| Normalise | Which way, discarding distance |
| Dot product | How aligned two directions are |
| Cross product | A vector perpendicular to both |
| Lerp | A point some fraction of the way along |
The dot product is the workhorse. On two normalised vectors it is the cosine of the angle between them: 1 is the same direction, 0 is perpendicular, -1 is opposite. That single number answers is the enemy in front of me, is this surface facing the light, is the player looking at the door.
The cross product gives a perpendicular. It builds coordinate frames, finds surface normals from triangles, and tells you which side of a line something is on by the sign of the result.
Matrices & Transforms
A matrix packs translation, rotation and scale into one object that can be applied to a point in a single multiply — and, crucially, composed. A wheel attached to a car attached to a moving world is three matrices multiplied together, and the wheel needs to know nothing about any of it.
Every vertex takes the same journey, usually called the MVP chain:
| Matrix | Takes you from |
|---|---|
| Model | The object's own space to world space |
| View | World space to the camera's space |
| Projection | Camera space to clip space |
Matrix multiplication is not commutative. Rotate-then-translate moves an object along its own rotated axes; translate-then-rotate swings it around the origin like a hammer throw. Most transform bugs that look like mysterious orbiting are this, and nothing else.
The fourth row and column exist so translation fits into a multiply at all. That is also why positions carry w = 1 and directions carry w = 0 — a direction should be rotated but never moved.
Quaternions & Rotation
Three angles around three axes — Euler angles — are readable and wrong. When one axis lines up with another you lose a degree of freedom and the object stops being able to rotate in a direction it obviously should. That is gimbal lock, and it arrives at exactly the moment a camera looks straight up.
Interpolation is the reason engines expose them at all. Blending between two orientations with slerp follows the shortest rotation at a constant rate; blending Euler angles gives you wobble, sudden flips at the wraparound from 359° to 0°, and animation that looks broken in a way artists cannot fix.
You almost never construct one by hand. You build them from an axis and an angle, or from a look direction, multiply them to compose rotations, and slerp between them. Treat the four components as an implementation detail.
Interview Questions
What does the dot product tell you?
On normalised vectors it is the cosine of the angle between them — 1 aligned, 0 perpendicular, -1 opposite. It answers facing and alignment questions without any trigonometry.
Why compare squared distances?
The square root is the costly part and it is monotonic, so it never changes which distance is smaller. For any comparison, sorting or radius check, skip it.
Why is transform order significant?
Matrix multiplication is not commutative. Rotate-then-translate moves along the rotated axes; translate-then-rotate swings the object around the origin instead.
What is gimbal lock?
With Euler angles, when two rotation axes align you lose a degree of freedom and can no longer rotate in one direction. It typically appears when a camera pitches to straight up or straight down.
Why quaternions over Euler angles?
They cannot gimbal lock, they compose without ambiguity, and slerp between two of them follows the shortest arc at a constant rate — which is what makes blended animation look right.
Why does a position use w = 1 and a direction w = 0?
The fourth component decides whether the translation part of a matrix applies. A point should move with the transform; a direction should only be rotated and scaled.