skip to content

Matrices and Transformations

Why games use 4x4 matrices and homogeneous coordinates, how translate-rotate-scale compose and why order matters, and inverting a transform. Interviewers probe order: it causes orbit-not-spin bugs.

on this pageshow

explore

questions

6

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%

answer

  1. translation is not linear
  2. a fourth coordinate carries the offset
  3. w multiplies the translation column
  4. point minus point gives a direction

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.

solid answer

~50 s

A 3x3 matrix is a linear map, so it always sends the origin to the origin and can never translate a 3D point. Adding `w` and using a 4x4 matrix whose last column (with column vectors, `p' = M * p`) holds the translation `t` gives `M * (x, y, z, w) = A * (x, y, z) + w * t`. With `w = 1` a point picks up the offset; with `w = 0` a direction such as a facing vector does not, which is exactly right because moving an object does not change which way it points. The payoff is that scale, rotation, translation and every parent transform collapse into one matrix that composes with one multiply, inverts as one unit and uploads as one block. The `w` also keeps bookkeeping honest: point minus point has `w = 0`, a direction.

go deeper

for a junior

Recall that 3x3 matrices cannot translate, that the 4x4 form puts translation in the matrix, and that points use w = 1 while directions use w = 0.

for a middle

Show the multiplication: the result's offset term is w times the translation column. Explain why point minus point is a direction and how that guards against transforming a direction as a point.

for a senior

Connect the representation to cost and correctness: one composed matrix per object, cheap rigid inverses, 3x4 storage with an implicit row, and where w stops being 1 (projection).

for a principal

Weigh storage choices for the team: full 4x4 everywhere for uniformity, 3x4 for memory and bandwidth, or separate components for editing, and where conversions are allowed to happen.

## The problem: translation is not linear A **linear map** is any function `f` with `f(a + b) = f(a) + f(b)` and `f(k * a) = k * f(a)`. Every 3x3 matrix acting on a 3-component vector is linear, and every linear map sends the zero vector to zero. **Translation** — "move every point by `(5, 0, 0)`" — moves the origin to `(5, 0, 0)`, so no 3x3 matrix can do it. Rotation and scale are linear; translation is **affine** (linear plus an offset). Games could keep a 3x3 part and a separate offset vector, and many do in storage. But then every composition, inversion and hand-off to the GPU has to carry two pieces with their own rules. **Homogeneous coordinates** fold the offset into the matrix so that one uniform operation, matrix multiplication, does everything. ## Adding a fourth coordinate Write a 3D position as `(x, y, z, w)` and use a 4x4 matrix. With the **column-vector convention** (`p' = M * p`, the vector on the right), an affine transform looks like this: ``` | a00 a01 a02 tx | | x | | a00*x + a01*y + a02*z + tx*w | | a10 a11 a12 ty | * | y | = | a10*x + a11*y + a12*z + ty*w | | a20 a21 a22 tz | | z | | a20*x + a21*y + a22*z + tz*w | | 0 0 0 1 | | w | | w | ``` The upper-left 3x3 block `A` holds rotation and scale, the last column holds the translation `t`, and the bottom row stays `(0, 0, 0, 1)` so `w` passes through unchanged. (With the row-vector convention, `p' = p * M`, the same matrix is transposed and the translation sits in the bottom row instead.) ## Points versus directions The `w` in the result multiplies the translation, so it decides whether the offset applies: - **Points** (a vertex, a spawn location, a muzzle position) use `w = 1` and get moved. - **Directions** (a facing vector, an axis, the difference between two points) use `w = 0` and only get rotated and scaled. Worked example: a matrix that only translates by `(5, 0, 0)`. 1. The point `(1, 2, 3, 1)` becomes `(6, 2, 3, 1)`: moved, as it should be. 2. The direction `(0, 0, 1, 0)` stays `(0, 0, 1, 0)`: an object that slides sideways still faces the same way. The arithmetic of `w` mirrors what the geometry allows: | operation | resulting w | meaning | |---|---|---| | point - point | 1 - 1 = 0 | a direction (the arrow between them) | | point + direction | 1 + 0 = 1 | a point (walk along the arrow) | | direction + direction | 0 + 0 = 0 | a direction | | point + point | 1 + 1 = 2 | not a point on its own; only a weighted mix whose weights sum to 1, such as a midpoint, is | Surface normals are directions too, so they use `w = 0`, but they need one more correction when the matrix scales unevenly — a separate subject. ## Why one matrix beats separate pieces - **Composition is one multiply.** Scale, then rotate, then translate, then the parent's transform: multiply the matrices once and every vertex pays for one matrix-vector product instead of a chain. - **Inversion is uniform.** Going from world space back to an object's local space is the inverse of one matrix. - **Hierarchies chain naturally.** A child's world matrix is its parent's world matrix times its own local matrix. - **The GPU expects it.** Vertex transforms are written as one 4x4 multiply, so the same matrix type serves gameplay code and rendering. The bottom row `(0, 0, 0, 1)` is what every affine transform has. A projection matrix is the case that changes it and produces `w` values other than 1; that belongs with projection, not here. ## Common mistakes - Transforming a direction with `w = 1`, so a facing vector gets the object's position added and points somewhere absurd. - Treating `w` as SIMD padding with no meaning and leaving garbage in it. - Subtracting two positions and then transforming the result as a point. - Storing only a 3x4 matrix (a common memory saving) and forgetting that the implicit bottom row is still `(0, 0, 0, 1)` when converting back.

  • Why do some engines store a 3x4 matrix or separate position, rotation and scale instead of a full 4x4?
    Memory and clarity. For an affine transform the bottom row is always (0, 0, 0, 1), so a 3x4 matrix drops 4 floats and rebuilds the row implicitly. Separate components are easier to edit and animate. The math is identical: either form is expanded to the same 4x4 product when transforms are composed or sent to the GPU.
  • Is it enough to transform a surface normal with w = 0 through the model matrix?
    It handles translation correctly, because the normal ignores the offset. It is still wrong when the matrix scales unevenly: the normal stops being perpendicular to the surface. Normals need the inverse-transpose of the upper-left 3x3 block, then renormalising.
  • What is the w of the midpoint of two points, and why?
    Exactly 1. The midpoint is 0.5 * p + 0.5 * q, and the weights sum to 1, so w = 0.5 + 0.5 = 1. Any affine combination whose weights sum to 1 is a valid point; a plain sum p + q has w = 2 and is not.

  • A 3x3 matrix can translate a 3D point if you pack the offset in
  • Directions should use w = 1 so they move with the object
  • w is only SIMD padding and carries no meaning
  • Subtracting two positions gives another position
  • Directions with w = 0 skip rotation as well as translation
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

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

A statue scaled non-uniformly has lighting normals tilted the wrong way; why does transforming normals by the model matrix fail, and what fixes it?

level: seniorimportance: should knowfreq 56%

basics

~10 s

Non-uniform scale stretches the surface so a normal pushed through the model matrix is no longer perpendicular to it. Transform normals with the inverse-transpose of the model matrix's upper-left 3x3 block, then renormalise.

open as a page

Your engine's math library, asset exporters and shaders keep disagreeing about matrices; what matrix-convention standard would you set for the team, and how would you enforce it?

level: principalimportance: should knowfreq 30%

basics

~20 s

Separate two choices — which side the vector multiplies on, and how the 16 floats sit in memory — pick one of each, document them, and enforce them with types, named accessors, boundary conversions and golden tests.

open as a page