skip to content

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%

answer

  1. three orthonormal axes
  2. forward from eye to target
  3. two cross products
  4. operand order sets handedness
  5. parallel up vector breaks it

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.

solid answer

~40 s

Assuming right-handed axes, column vectors and a camera that looks down `-z` in view space: `f = normalize(target - eye)`, `r = normalize(cross(f, worldUp))`, `u = cross(r, f)`. The view matrix has rows `(r, -dot(r, eye))`, `(u, -dot(u, eye))`, `(-f, dot(f, eye))` and `(0, 0, 0, 1)`; it is the inverse of the camera's world transform, which is cheap because the rotation part is orthonormal. `r` must be normalised because `|cross(f, up)|` is the sine of the angle between them. Two traps: swapping the cross-product order or the sign of `f` silently changes handedness, and when `f` is parallel to `worldUp`, as for an RTS camera looking straight down, the cross product is zero and the matrix fills with NaNs, so you need a fallback up axis.

code

pseudocode · 12 lines
pseudocode
function lookAt(eye, target, worldUp, fallbackUp):
    f = normalize(target - eye)
    s = cross(f, worldUp)
    if lengthSquared(s) < 1e-8:            // forward is parallel to worldUp
        s = cross(f, fallbackUp)            // e.g. (0, 0, -1) for a top-down camera
    r = normalize(s)
    u = cross(r, f)                         // already unit length
    return Matrix4Rows(
        ( r.x,  r.y,  r.z, -dot(r, eye)),
        ( u.x,  u.y,  u.z, -dot(u, eye)),
        (-f.x, -f.y, -f.z,  dot(f, eye)),
        ( 0,    0,    0,    1))

go deeper

for a junior

Recall the three axes: forward from eye to target, right from a cross product with world up, and the true up from a second cross product.

for a middle

Derive the full matrix, explain why right needs normalising, and show that the eye maps to the origin and the target onto the forward axis.

for a senior

Diagnose the symptoms: upside down means two flipped axes, mirrored with inverted culling means one, NaNs mean forward parallel to up; name a fallback.

for a principal

Standardise one handedness and forward axis across tools and runtime, with a tested look-at helper, so camera code is never re-derived with a different operand order.

## What a view matrix is A **view matrix** moves the world into **view space**, where the camera sits at the origin and looks along a fixed axis. It is the **inverse** of the camera's own world transform. A **look-at** function builds it from three inputs: the camera position `eye`, the point it looks at `target`, and a **world up** vector, usually the level's vertical axis. Many engines store the camera as an ordinary transform and derive the view matrix from it every frame; a look-at is the case where the orientation comes from a target point, as in third-person, orbit and cutscene cameras. This answer assumes **right-handed** axes, **column vectors** (`p' = V * p`), world up `(0, 1, 0)`, and a camera that looks down `-z` in view space with `+x` right and `+y` up. ## Building the three axes 1. **Forward**: `f = normalize(target - eye)`. 2. **Right**: `r = normalize(cross(f, worldUp))`. Normalising is required: for unit vectors `|cross(a, b)| = sin(angle)`, so unless the camera looks exactly horizontally the raw cross product is shorter than 1. 3. **True up**: `u = cross(r, f)`. Because `r` and `f` are perpendicular unit vectors, `u` is already unit length and perpendicular to both. This replaces the world up, which is generally not perpendicular to a tilted forward. A quick check: for `f = (0, 0, -1)` and `worldUp = (0, 1, 0)`, `cross(f, worldUp) = (1, 0, 0)` and `cross(r, f) = (0, 1, 0)`, exactly the expected right and up. ## Assembling the matrix The camera's world transform has `r`, `u` and `-f` as the columns of its rotation and `eye` as its translation. Its inverse transposes the rotation and rotates and negates the translation: ``` | r.x r.y r.z -dot(r, eye) | | u.x u.y u.z -dot(u, eye) | | -f.x -f.y -f.z dot(f, eye) | | 0 0 0 1 | ``` Two checks make the derivation self-verifying: - `eye` maps to the origin, because each row gives `dot(axis, eye) - dot(axis, eye) = 0`. - `target`, at distance `d` from `eye`, maps to `(0, 0, -d)`: the first two rows give zero because `target - eye` is parallel to `f`, and the third gives `-dot(f, target - eye) = -d`. ## The handedness trap The cross product formula is the same in both conventions; handedness decides which operand order gives "right" and whether forward is `-z` or `+z` in view space. | Convention | right | view-space forward | third row | |---|---|---|---| | Right-handed, looks down `-z` | `cross(f, up)` | `-z` | `-f` | | Left-handed, looks down `+z` | `cross(up, f)` | `+z` | `+f` | Mixing them produces symptoms worth recognising: - Computing `r = cross(up, f)` in the right-handed version negates `r`, and then `u = cross(r, f)` is negated too. Two flipped axes are a 180-degree rotation about the view axis, so the image comes out **upside down**, not mirrored. - Flipping a single axis, for example computing `r = cross(up, f)` but then using the world up directly instead of a second cross product, gives rotation rows with determinant -1: the image is **mirrored** left to right and triangle winding reverses, so back-face culling removes the wrong faces. ## The degenerate up vector When `f` is parallel to `worldUp`, `cross(f, worldUp)` is the zero vector, normalising it divides by zero, and every entry becomes NaN. An RTS camera pitched to look straight down, or a free camera passing through the vertical, hits exactly this. Fixes: - Detect a near-zero `lengthSquared(cross(f, worldUp))` and switch to a fallback up, such as the level's north axis for a top-down camera. - Clamp the camera's pitch just short of vertical. - Keep the camera's previous up or right vector and reuse it while forward is near vertical. ## Checks you can run - The rotation rows should be orthonormal; their determinant should be +1, not -1. - The camera position should map to `(0, 0, 0)` and the target to `(0, 0, -distance)`. - A point to the camera's right should get a positive view-space `x`. These three lines of test catch every mistake above without looking at a rendered frame.

  • Why can you invert the camera's transform by transposing instead of running a general 4x4 inverse?
    The camera's rotation part is built from orthonormal axes, and the inverse of an orthonormal matrix is its transpose. The translation then becomes the negated eye position expressed in the camera's axes, `-dot(axis, eye)` per row. That is cheaper and numerically cleaner than a general inverse, but only valid while the camera transform has no scale or shear.
  • How would you add camera roll, such as a banking flight camera, to a look-at matrix?
    Rotate the up vector around the forward axis by the roll angle before taking the cross products, or rotate the finished right and true-up vectors around forward. Both keep forward pointing at the target and tilt the horizon. Rolling the world up vector itself before the look-at only works while it stays non-parallel to forward.
  • Your look-at camera shows the scene mirrored and with inside-out meshes. What do you check first?
    Check the determinant of the rotation rows: -1 means exactly one axis is flipped. A typical source is a right axis computed with the other handedness's operand order while the up row is taken straight from world up instead of a second cross product. Two flipped axes would instead show an upside-down image with correct culling.

  • The cross product of two unit vectors is always unit length.
  • World up can be used directly as the camera's up axis.
  • Swapping the cross-product operands only mirrors the image.
  • The view matrix is the camera's world matrix with the translation negated.
  • A camera looking straight down suffers gimbal lock in its view matrix.