skip to content

How do you project a vector onto an axis and reject it from that axis, and how does a turret use both to aim?

level: middleimportance: should knowfreq 55%

answer

  1. the part along, the part across
  2. unit axis or divide by its square
  3. flatten onto the ground plane
  4. two-argument arctangent for elevation

basics

~20 s

Projecting v onto unit axis a gives dot(v, a)*a; rejecting gives the perpendicular rest, v - dot(v, a)*a. A turret rejects the up part of its target offset to aim its base, and pitches its barrel from the two parts' lengths.

solid answer

~50 s

For a unit axis `a`, `dot(v, a)` is the signed length of `v` along `a`, `dot(v, a) * a` is the vector projection and `v - dot(v, a) * a` is the rejection, the part perpendicular to `a`; for a non-unit axis divide by `dot(a, a)`. A turret with unit up axis `up` and target offset `t` computes `flat = t - dot(t, up) * up`, the offset flattened onto its turning plane, and normalises `flat` for the base's heading. The barrel's elevation is `atan2(dot(t, up), length(flat))`, which is well behaved near straight up, unlike an arccosine. The degenerate case is a target directly above, where `flat` is zero and must not be normalised. The same pair answers other questions: progress along a straight corridor is a scalar projection, and distance from an infinite line is the length of a rejection.

code

pseudocode · 11 lines
pseudocode
function aimTurret(turretPos, up, targetPos, lastHeading):   // up is unit length
    t = targetPos - turretPos
    height = dot(t, up)                     // scalar projection onto up
    flat = t - height * up                  // rejection: offset in the turning plane
    flatLenSq = dot(flat, flat)
    if flatLenSq > EPS * EPS:
        heading = flat * (1 / sqrt(flatLenSq))
    else:
        heading = lastHeading               // target straight above or below
    elevation = atan2(height, sqrt(flatLenSq))
    return heading, elevation

go deeper

for a junior

Recall that projection is the part along an axis and rejection the part across it, and write both for a unit axis.

for a middle

Explain the non-unit form with dot(a, a), then apply it: flatten a target offset onto a turret's turning plane, normalise it for the heading and get elevation from the two-argument arctangent.

for a senior

Show the cases that break in production: the zero rejection when a target is directly overhead, the arccosine near the poles, a tilted turret using the world's up, and scalar versus vector projection mix-ups.

for a principal

No standing decision lives in this formula; at lead level the point is to say where a shared projection helper should guard degenerate axes, so every caller does not reinvent the zero-length check.

## Two pieces of every vector Any vector `v` can be split, relative to a chosen axis `a`, into a part **along** the axis and a part **across** it. The part along is the **projection**; the part across is the **rejection**. The two are perpendicular and add back up to `v`. Interviewers ask this because so much gameplay code is "keep this part of the motion, drop that part". ## The formulas | name | unit axis `a` | any non-zero axis `a` | |---|---|---| | scalar projection (signed length along `a`) | `dot(v, a)` | `dot(v, a) / length(a)` | | vector projection | `dot(v, a) * a` | `dot(v, a) / dot(a, a) * a` | | rejection | `v - dot(v, a) * a` | `v - dot(v, a) / dot(a, a) * a` | The unit-axis forms are the ones written most often, and the most common bug is using them with an axis that is not unit length: `dot(v, a) * a` then comes out scaled by `length(a)` squared. ## Worked example: aiming a turret A turret has a base that turns about its **up axis** and a barrel that pitches. It sits at the origin with `up = (0, 1, 0)`, and the offset to its target is `t = (6, 3, 8)`. 1. **Projection onto up:** `dot(t, up) = 3`, so the vertical part is `(0, 3, 0)`: the target is 3 units above the turret's turning plane. 2. **Rejection from up:** `flat = t - 3 * up = (6, 0, 8)`, the offset flattened onto the turning plane. Its length is `sqrt(36 + 64) = 10`. 3. **Base heading:** `normalize(flat) = (0.6, 0, 0.8)`. The base turns until its forward vector matches this. 4. **Barrel elevation:** `atan2(3, 10)`, about 16.7 degrees above the horizontal. The two-argument arctangent of (height, horizontal distance) is the robust way to get the elevation. Taking 90 degrees minus the arccosine of `dot(normalize(t), up)` gives the same angle on paper but loses precision near straight up and straight down, and returns NaN if rounding pushes its argument past 1. A turret bolted to a sloped deck should use **its own** up axis, not the world's. Everything above still holds; only `up` changes. ## Other places games use the pair - **Progress along a straight path.** For a corridor centre line starting at `p0` with unit direction `d`, `dot(x - p0, d)` is how far along it a player at `x` has reached, negative if they are behind the start. - **Distance from a line.** The length of the rejection of `x - p0` from `d` is the distance from `x` to the infinite line through `p0`. Clamping to a finite segment turns this into a closest-point query. - **Removing one component of a movement.** Rejecting a desired move from a surface normal keeps the part along the surface and drops the part heading into it; handling a whole character controller around that is a separate subject. - **Splitting for a bounce.** Reflection about a normal is the projection reversed and the rejection kept. ## Pitfalls - **Non-unit axis with the unit formula.** Normalise the axis or divide by `dot(a, a)`. - **The zero rejection.** When the target is directly above the turret, `flat` is `(0, 0, 0)`; normalising it divides zero by zero and produces NaN. Test its squared length against a small threshold first and keep the previous heading when it fails. - **Confusing the scalar and the vector projection.** `dot(v, a)` is a signed number; `dot(v, a) * a` is a vector. Mixing them up type-checks in a loosely typed helper and silently gives nonsense. - **Forgetting the start point.** `dot(x, d)` without subtracting `p0` measures progress from the world origin, so it is off by `dot(p0, d)`.

  • How do you measure how far a player is from a straight laser beam fired by the turret?
    Treat the beam as a line through the muzzle `p0` with unit direction `d`. Reject `x - p0` from `d`: `r = (x - p0) - dot(x - p0, d) * d`. The length of `r` is the distance to the infinite line. If the beam has a start and an end, clamp `dot(x - p0, d)` between 0 and the beam's length first, which makes it a closest-point-on-segment query.
  • Why use the two-argument arctangent for the barrel's elevation instead of an arccosine?
    The arctangent of (height, horizontal distance) is well conditioned at every angle and never fails on rounding. The arccosine of a dot product of unit vectors is ill conditioned near 0 and 180 degrees, so small rounding errors become large angle errors there, and a rounded value just above 1 gives NaN.

Rejecting the up component is like a stick's shadow at noon with the sun straight overhead: the shadow on the ground is the horizontal part of the stick, and the stick's height is its projection onto up. A stick standing straight up casts no shadow, which is the zero-length case.

  • Uses dot(v, a) * a as the projection when a is not unit length.
  • Calls the scalar dot(v, a) the projection vector and adds it to positions.
  • Normalises the flattened aim vector without checking for zero length.
  • Uses the world up axis for a turret mounted on a tilted surface.
  • Measures progress along a path with dot(x, d), forgetting to subtract its start.