Game Development · Guide

Game Maths

Vectors, matrices and quaternions — the small pile of linear algebra that every frame of every game runs on.

— min read Game Development

A Small Amount of Maths, Used Constantly

Game maths is narrow and deep. You need vectors, matrices and quaternions — but you need them in every system, thousands of times a second, and a misunderstanding shows up as a bug you cannot describe.

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.

OperationAnswers
Length (magnitude)How far
NormaliseWhich way, discarding distance
Dot productHow aligned two directions are
Cross productA vector perpendicular to both
LerpA 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.

Comparing distances? Compare squared lengths. The square root is the expensive part and it changes nothing about the ordering — a nearest-enemy search over hundreds of entities does this every frame.

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:

MatrixTakes you from
ModelThe object's own space to world space
ViewWorld space to the camera's space
ProjectionCamera 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.

A quaternion stores a rotation as an axis and an amount, in four numbers. It cannot gimbal lock, it composes cleanly, and it interpolates along the shortest arc.

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.

Quaternions drift. Repeated multiplication accumulates floating-point error until the length is no longer 1 and the rotation starts scaling the object. Normalise them periodically.

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.

Quick Quiz

1. Two normalised vectors have a dot product near 0. They are…
2. For finding the nearest enemy, you should compare…
3. An object orbits the origin instead of turning in place. Likely cause:
4. Gimbal lock is a property of…
5. Slerp between two quaternions gives you…