skip to content

Game Mathematics

Vectors, matrices, quaternions, projection, splines, intersection tests, float traps and noise. Interviewers use it to find who can derive a transform rather than only call an engine function.

on this pageshow

explore

questions

page 1 of 2

What chain of coordinate spaces does a mesh vertex cross from its model data to a screen pixel, and what does each step do?

level: juniorimportance: must knowfreq 62%

answer

  1. one frame per stage
  2. authored pivot, then one shared level frame
  3. camera moved to the origin
  4. four components before the divide
  5. divide by w, then scale to pixels

basics

~20 s

Model space, then world space through the model matrix, view space through the view matrix, clip space through the projection matrix, normalised device coordinates after dividing by w, and screen pixels after the viewport mapping.

solid answer

~40 s

With column vectors the vertex goes `clip = P * V * M * p`, so the rightmost matrix applies first. The **model matrix** `M` places the mesh's own pivot-relative vertices in the shared **world** frame. The **view matrix** `V` is the inverse of the camera's world transform, so in **view space** the camera sits at the origin looking down its forward axis. The **projection matrix** `P` maps the visible volume into four-component **clip space**; for a perspective camera it copies view depth into `w`. Dividing `x`, `y` and `z` by `w` gives **normalised device coordinates**, where the visible region has `x` and `y` between -1 and 1. Finally the **viewport mapping** scales NDC to pixel coordinates, usually flipping `y` because pixel rows grow downwards.

go deeper

for a junior

Recall the six spaces in order, which matrix or step enters each one, and that the view matrix is the inverse of the camera's transform.

for a middle

Explain why the divide by w creates foreshortening, why orthographic leaves w at 1, and how the viewport mapping flips y for pixel coordinates.

for a senior

Use the chain to debug: a mirrored marker means a point behind the camera, a mirrored image means one flipped axis, and an upside-down position usually means a missing y flip.

for a principal

Be ready to justify one documented convention for the codebase (vector convention, handedness, up axis, NDC depth range) so tools, gameplay and rendering code never translate between them by accident.

## The chain at a glance Every vertex a game draws or picks is described in a sequence of **coordinate spaces**, and each arrow between them is one transform. This answer assumes **column vectors**, so a point is transformed as `p' = M * p` and in a product such as `P * V * M * p` the rightmost matrix applies first. Under the row-vector convention the same chain is written `p * M * V * P`; the spaces are identical. | Space | Origin and axes | Reached by | What code uses it for | |---|---|---|---| | **Model** (local, object) | the mesh's authored pivot | nothing; this is the data | vertex positions as modelled | | **World** | one shared origin for the level | model matrix `M` | gameplay, physics, lighting, AI | | **View** (camera, eye) | the camera at the origin | view matrix `V` | depth sorting, view-relative effects | | **Clip** | four components `(x, y, z, w)` | projection matrix `P` | testing against the view volume | | **NDC** | a box, `x` and `y` in `[-1, 1]` | divide by `w` | resolution-independent position | | **Screen** (window) | pixels, plus a depth value | viewport mapping | picking, UI anchoring | ## Model and world space An artist builds a crate around its own pivot, so its vertices are stored in **model space** and are the same for every crate in the level. Each instance carries a **model matrix**, usually built from translation, rotation and scale, that moves those vertices into **world space**, the one frame every system agrees on. Objects parented to other objects chain their matrices (parent times child) before reaching world space. ## View space Projection is simplest when the camera sits at the origin and looks along an axis, so the next step moves the whole world rather than the camera. The **view matrix** is therefore the **inverse** of the camera's own world transform: moving the camera two metres right is the same as moving the world two metres left. A common right-handed convention puts the camera looking down `-z` with `+y` up and `+x` right; many engines instead use a left-handed frame looking down `+z`. Either works as long as the view and projection matrices agree. ## Clip space and the divide The **projection matrix** maps the visible volume (a truncated pyramid called the **frustum** for a perspective camera, a box for an orthographic one) into **clip space**, a four-component homogeneous space. A perspective matrix copies the point's distance in front of the camera into `w`. Dividing `x`, `y` and `z` by `w` (the **perspective divide**) produces **normalised device coordinates**, and because far points have a larger `w` they shrink toward the centre: that division is where foreshortening comes from. An orthographic matrix leaves `w` at 1, so the divide changes nothing and size does not depend on distance. - In NDC the visible region is `-1 <= x <= 1` and `-1 <= y <= 1`. - The depth range differs by graphics API: some use `[-1, 1]` for `z`, others `[0, 1]`. Code that crosses that boundary must know which one it targets. - Points behind the camera end up with `w <= 0`; geometry is clipped against the view volume before the divide precisely so that such points never get divided. ## NDC to screen The **viewport mapping** turns NDC into pixels for a viewport of a given size and offset. With the pixel origin at the top-left and pixel rows growing downwards: - `pixelX = viewportX + (ndc.x + 1) / 2 * width` - `pixelY = viewportY + (1 - ndc.y) / 2 * height` The `1 - ndc.y` term is the **y flip**: NDC `y` grows upwards while pixel rows grow downwards. Depth is mapped into the depth buffer's range at the same time. ## Slips interviewers listen for 1. Writing the product in the wrong order for the convention, for example `M * V * P * p` with column vectors. 2. Using the camera's world transform as the view matrix instead of its inverse. 3. Treating clip space as NDC and forgetting the divide by `w`. 4. Forgetting the y flip between NDC and pixels, so screen positions come out upside down. 5. Rebuilding `P * V` per object instead of once per frame per camera, then multiplying each object's `M` on the right. A candidate who can name each space, say which matrix enters it and say what the space is good for can answer almost every follow-up on this topic, because picking, health bars over heads and custom camera code are all walks forwards or backwards along this one chain.

  • Why keep a separate world space instead of going straight from model space to view space?
    World space is the frame every system shares: gameplay distances, physics, lighting, AI and audio all need positions that do not depend on which camera is looking. Several cameras (split screen, a minimap, a shadow-casting light) can each have their own view matrix over the same world. Renderers still often concatenate `V * M` into one model-view matrix per object, but the world frame stays the reference everything else is written in.
  • What happens to a point behind a perspective camera as it moves along this chain?
    In view space it has the wrong sign of depth, so the projection gives it `w <= 0`. Dividing by a negative `w` flips the signs of `x` and `y`, which would place it mirrored through the screen centre. The pipeline avoids this by clipping in clip space before the divide; any code that projects points itself, such as a health-bar anchor, has to test `w` first.
  • Where does a direction vector, such as a surface normal or a light direction, differ from a point on this chain?
    A direction has no position, so it must ignore translation: it is carried with `w = 0`, or transformed by the upper 3x3 part of the matrix only. It does not go through the perspective divide, and normals under non-uniform scale need their own matrix. Lighting is usually computed in world or view space, before projection, for that reason.

  • The view matrix is the camera's world transform, not its inverse.
  • Clip space and normalised device coordinates are the same thing.
  • The projection matrix outputs pixel coordinates directly.
  • Mesh files store each object's vertices in world space.
  • The multiplication order does not matter as long as all three matrices are used.
open as a page

How do you represent a plane with a normal and an offset, and what does a point's signed distance to that plane tell you?

level: juniorimportance: must knowfreq 60%

basics

~20 s

A plane is a unit normal n and an offset d, holding every point x with dot(n, x) = d. The signed distance dot(n, p) - d tells which side p is on and, with a unit normal, how far away it is.

open as a page

What do lerp, inverse lerp and remap compute, and how is remap built from the other two?

level: juniorimportance: must knowfreq 65%

basics

~20 s

Lerp blends two values by t: a + (b - a) * t. Inverse lerp recovers t from a value: (v - a) / (b - a). Remap chains them, mapping a value in one range to the matching value in another.

open as a page

Why do games use 4x4 matrices with homogeneous coordinates for 3D transforms, and why do points get w = 1 but directions w = 0?

level: juniorimportance: must knowfreq 70%

basics

~20 s

A 3x3 matrix cannot translate a 3D point, because translation moves the origin. A fourth coordinate w makes translation part of one matrix multiply: points use w = 1 so they move, directions use w = 0 so they ignore it.

open as a page

Why does 0.1 + 0.2 not equal 0.3 in IEEE 754 double precision, and how should game code compare floats instead of using ==?

level: juniorimportance: must knowfreq 55%

basics

~20 s

IEEE 754 stores binary fractions, so 0.1, 0.2 and 0.3 are each rounded to the nearest representable double and the sum lands one step above the stored 0.3. Compare computed floats with a threshold or tolerance, never ==.

open as a page

How do you pick an item from a weighted loot table so each item drops in proportion to its weight?

level: juniorimportance: must knowfreq 48%

basics

~20 s

Sum the weights, draw one uniform number in [0, total), then return the first item whose running (cumulative) total is greater than the draw. Each item owns a slice of the range exactly as wide as its weight.

open as a page

With Euler angles, what is gimbal lock, and why does a yaw-pitch-roll flight camera lose a degree of freedom when it pitches straight up?

level: juniorimportance: must knowfreq 72%

basics

~20 s

Gimbal lock is the singularity of every three-angle Euler sequence: at pitch of plus or minus 90 degrees the yaw and roll axes line up, so both spin the camera about one axis and a degree of freedom disappears.

open as a page

How does the sign of a dot product tell a sentry turret whether the player is in front of it or behind it?

level: juniorimportance: must knowfreq 80%

basics

~20 s

Dot the turret's world-space forward vector with the offset from turret to player: positive means in front, negative behind, zero exactly beside, because the dot product's sign is the sign of the cosine of the angle between them.

open as a page

In a strategy game, how do you turn a mouse click's pixel position into a world-space ray for selecting the unit under the cursor?

level: middleimportance: must knowfreq 58%

basics

~20 s

Convert the click to NDC, build two NDC points at near and far depth, multiply both by the inverse view-projection matrix, divide each by its w, and use the near point as origin and the normalised difference as direction.

open as a page

How do you derive a ray-versus-sphere intersection test, and what does the discriminant of the resulting quadratic tell you?

level: middleimportance: must knowfreq 62%

basics

~20 s

Substitute the ray o + t*d into |x - c|^2 = r^2 to get a quadratic in t. A negative discriminant is a miss, zero a tangent graze, positive two crossings; the hit is the smallest non-negative root.

open as a page

Why does lerp(value, target, 0.1) run every frame behave differently at 30 and 144 frames per second, and how do you make it frame-rate independent?

level: middleimportance: must knowfreq 60%

basics

~20 s

Each frame keeps 90% of the remaining gap, so after one second 0.9^30 (about 4%) remains at 30 fps but almost nothing at 144 fps. A blend factor of 1 - exp(-k * dt) makes the decay depend only on elapsed time.

open as a page

A moon mesh beside a planet at the world origin orbits it instead of spinning in place; how does transform multiplication order cause this?

level: middleimportance: must knowfreq 78%

basics

~20 s

The rotation was applied after the translation, so it pivots the already-moved moon about the world origin. With column vectors the model matrix must be T * R * S: scale and spin about its own centre, then move.

open as a page

Why is a shuffle that swaps every slot with any random index biased, and how does Fisher-Yates make every ordering equally likely?

level: middleimportance: must knowfreq 45%

basics

~20 s

Swapping every slot with any of n indices gives n^n equally likely sequences, which cannot split evenly over n! orderings. Fisher-Yates swaps slot i only with an index in [0, i], giving exactly n! equally likely paths, one per ordering.

open as a page

For unit quaternions, how do you rotate a vector, compose two rotations in the right order, and why is the conjugate the inverse?

level: middleimportance: must knowfreq 66%

basics

~20 s

A unit quaternion q = (cos(a/2), sin(a/2) n) rotates a vector by the sandwich q v q*; the product q2 q1 applies q1 first; and since q q* = 1, the conjugate (w, -x, -y, -z) is the inverse.

open as a page

How do you tell whether a target is on a turret's left or right, using a 2D perp-dot product or a 3D cross product?

level: middleimportance: must knowfreq 70%

basics

~20 s

With x right and y up, the 2D perp-dot f.xt.y - f.yt.x of facing f and target offset t is positive on the left. In 3D, dot(cross(f, t), up) does the same, but handedness decides which sign means left.

open as a page

How do you derive the vector a ball bounces off along when it hits an angled wall with unit surface normal n?

level: middleimportance: must knowfreq 70%

basics

~20 s

Split the incoming vector v into its part along the normal, dot(v, n)n, and its part along the wall. A bounce flips only the normal part: r = v - 2dot(v, n)*n, which keeps the length when n is unit length.

open as a page

What does smoothstep compute, and why does passing t through an easing curve make interpolated motion feel better than plain lerp?

level: juniorimportance: should knowfreq 45%

basics

~20 s

Smoothstep clamps a value to [0, 1] between two edges and returns 3t^2 - 2t^3, a curve with zero slope at both ends. Feeding an eased t into lerp makes motion start and stop gently instead of jumping to full speed.

open as a page

Why do game range checks often compare squared distances instead of distances, and when does that shortcut stop working?

level: juniorimportance: should knowfreq 60%

basics

~20 s

Distances are non-negative and squaring keeps their order, so length(d) < r exactly when dot(d, d) < r*r, skipping a square root per check. It stops working when the distance itself is needed, as in summing distances or a linear falloff.

open as a page

How do you build a look-at view matrix by hand from a camera position, a target point and a world up vector?

level: middleimportance: should knowfreq 48%

basics

~10 s

Compute forward = normalize(target - eye), right = normalize(cross(forward, up)) and trueUp = cross(right, forward), use them as the rotation rows, and put -dot(axis, eye) in the translation column to invert the camera's transform.

open as a page

How do you turn an enemy's world-space head position into pixel coordinates to place a floating health bar over it?

level: middleimportance: should knowfreq 50%

basics

~20 s

Multiply the head position (w = 1) by view-projection, hide the bar if clip w is not positive, divide by w, then map x to (ndc.x + 1) / 2 * width and y to (1 - ndc.y) / 2 * height.

open as a page

How does the Moller-Trumbore algorithm intersect a ray with a triangle, and what are the barycentric coordinates it returns used for?

level: middleimportance: should knowfreq 50%

basics

~20 s

Moller-Trumbore solves o + td = (1-u-v)v0 + uv1 + vv2 for t, u and v by Cramer's rule, exiting early once u, v or u + v leave [0, 1]. The barycentrics interpolate vertex data such as UVs and normals at the hit.

open as a page

How do you test an axis-aligned bounding box against a plane, as each of a view frustum's six planes requires, without checking all eight corners?

level: middleimportance: should knowfreq 45%

basics

~20 s

For a box with centre c and half-extents e, compare s = dot(n, c) - d with the projected radius r = ex*|nx| + ey*|ny| + ez*|nz|: s > r is fully in front, s < -r fully behind, anything else straddles.

open as a page

How does de Casteljau's algorithm evaluate a cubic Bezier curve, and why does the curve not pass through its two inner control points?

level: middleimportance: should knowfreq 50%

basics

~20 s

De Casteljau evaluates a cubic Bezier by repeated lerp: blend each adjacent pair of the four control points by t, then the three results, then the two. The inner points only pull the curve; it touches just the endpoints.

open as a page

How does a Catmull-Rom spline pass through every waypoint of a camera path, and how does it relate to a cubic Hermite curve?

level: middleimportance: should knowfreq 45%

basics

~20 s

Each Catmull-Rom segment is a cubic Hermite curve whose end tangents come from the neighbours: half the vector from the previous to the next waypoint. Hermite curves hit their endpoints by design, so every waypoint is passed with a shared tangent.

open as a page

Using transform matrices, how do you convert a point from one object's local frame into another's when both are placed in world space?

level: middleimportance: should knowfreq 48%

basics

~10 s

Go through world space: multiply by the source frame's local-to-world matrix, then by the inverse of the destination frame's local-to-world matrix. With column vectors that is p_B = inverse(M_B) * M_A * p_A.

open as a page

How do you invert a rigid transform matrix holding only rotation and translation without a general 4x4 inverse, and when does that shortcut break?

level: middleimportance: should knowfreq 58%

basics

~20 s

Transpose the rotation and rotate the negated translation back: inverse [R | t] = [R^T | -R^T * t]. It breaks once the 3x3 part holds scale or shear, because the transpose then no longer inverts it.

open as a page

When comparing two floats in game code, how do absolute epsilon, relative epsilon and ULP-distance comparisons differ, and where does each fail?

level: middleimportance: should knowfreq 42%

basics

~20 s

An absolute epsilon fails for large values, where neighbouring floats are farther apart than it; a relative epsilon and a ULP distance fail near zero. Robust code combines an absolute floor with a relative tolerance.

open as a page

When scattering dropped loot uniformly inside a circle around a defeated enemy, why must the random radius use a square root?

level: middleimportance: should knowfreq 30%

basics

~20 s

Area grows with the square of the radius, so a radius drawn as radius * u crowds points toward the centre. Drawing radius * sqrt(u) with an angle of 2 * pi * v makes every equal patch of the disk equally likely.

open as a page

For a terrain heightmap, how do value, Perlin and Simplex noise differ, and what do fBm octaves add on top?

level: middleimportance: should knowfreq 40%

basics

~20 s

Value noise blends random heights stored at grid points and looks blocky; Perlin noise blends random gradients for smoother, less grid-bound hills; Simplex noise uses a triangle grid with fewer corners and artifacts. fBm sums octaves of rising frequency and falling amplitude.

open as a page

When interpolating between two unit quaternions, how do slerp and nlerp differ in cost and angular speed, and when is nlerp good enough?

level: middleimportance: should knowfreq 52%

basics

~20 s

Slerp moves along the arc between two unit quaternions at constant angular speed but needs an inverse cosine and sines; nlerp normalises a plain lerp, follows the same arc far more cheaply, and runs faster mid-way, an error negligible for small angles.

open as a page

showing 1–30 of 45