skip to content

Geometric Queries and Intersection

Ray vs sphere, plane, triangle and AABB tests, plane-vs-box tests, barycentric coordinates and closest points. Interviewers set these on whiteboards because raycasts drive shooting and picking.

on this pageshow

explore

questions

6

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%

answer

  1. one vector and one scalar
  2. offset from any point on it
  3. dot with the normal, minus the offset
  4. sign picks the side

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.

solid answer

~50 s

I store a plane as a normal `n` and a scalar `d`: the plane is every `x` with `dot(n, x) = d`, and from any point `a` on it `d = dot(n, a)`. From a triangle I take `n = normalize(cross(b - a, c - a))`, so the vertex winding decides which way it faces. The signed distance `s = dot(n, p) - d` is positive on the side the normal points to, negative behind and zero on the plane; with a unit normal it is the true distance, with an unnormalised one it is scaled by `|n|`, which still gives correct side tests. On top of it I build the projection `p - s*n`, segment-crossing tests from the signs at both ends, and ray-plane intersection `t = (d - dot(n, o)) / dot(n, dir)` with a guard for a near-zero denominator.

go deeper

for a junior

Recall the form dot(n, x) = d, how to get d from a point on the plane, and that the sign of dot(n, p) - d says which side p is on.

for a middle

Build the plane from a triangle with a cross product, explain how winding sets the normal's direction, and derive ray-plane and segment-crossing tests from signed distance.

for a senior

Show where plane conventions bite in a shipped codebase: mixed offset sign conventions, unnormalised planes feeding distance thresholds, and parallel-ray guards that need a tolerance rather than an exact zero test.

for a principal

Weigh agreeing one plane convention and one winding across physics, rendering and gameplay code, since a subsystem that disagrees can silently flip front and back.

## The plane equation A **plane** in 3D is the set of points that sit at the same "height" along one direction. That direction is the plane's **normal** `n`, a vector perpendicular to the plane. The height is a single scalar, the **offset** `d`. Together they give the plane equation: `dot(n, x) = d` Every point `x` that satisfies it lies on the plane. If you know any point `a` on the plane, the offset is simply `d = dot(n, a)`. Some code writes the equation as `dot(n, x) + d = 0` instead; that only flips the sign of the stored offset, so pick one form for the whole codebase and state it. When `n` has unit length, `|d|` is the distance from the world origin to the plane. When it does not, `|d|` is that distance multiplied by `|n|`, which is why a plane is usually **normalised** by dividing both `n` and `d` by `|n|`. ## Building a plane from a triangle Game geometry usually hands you three vertices `a`, `b`, `c` rather than a normal. The recipe is: 1. Take two edges: `e1 = b - a` and `e2 = c - a`. 2. Take their cross product: `n = cross(e1, e2)`. It is perpendicular to both edges and so to the plane. 3. Normalise it, unless the triangle is degenerate (zero area gives a zero-length cross product, which cannot be normalised). 4. Set `d = dot(n, a)`. The **winding order** matters. In right-handed coordinates, `cross(b - a, c - a)` points toward a viewer who sees `a`, `b`, `c` counter-clockwise; in left-handed coordinates the same formula points toward a viewer who sees them clockwise. Swap two vertices and the normal flips, which flips the sign of every distance computed against the plane. ## Signed distance and what it tells you For a point `p`, the **signed distance** is: `s = dot(n, p) - d` - `s > 0`: `p` is on the side the normal points to, often called the front or positive half-space. - `s < 0`: `p` is behind the plane. - `s = 0`: `p` lies on the plane (in floating point, within a small tolerance). - `|s|` is the true perpendicular distance only when `n` is unit length; otherwise it is scaled by `|n|`. The sign is still correct, so a pure side test does not need a normalised plane. A useful mental picture is altitude: the plane is sea level, the normal points up, and `s` is the height above or below it. ## What you build on top of it Signed distance is the primitive almost every other plane query reuses: - **Projecting a point onto the plane:** `p - s*n` with a unit normal, the foot of the perpendicular. - **Does a segment cross the plane?** Compute `s0` and `s1` at its two endpoints. Opposite signs mean it crosses, at the parameter `t = s0 / (s0 - s1)` along the segment. - **Ray versus plane:** for a ray `o + t*dir`, solve `dot(n, o + t*dir) = d`, giving `t = (d - dot(n, o)) / dot(n, dir)`. A denominator near zero means the ray runs parallel to the plane; a negative `t` means the plane is behind the ray's origin. The sign of `dot(n, dir)` tells you whether the ray strikes the front or the back. - **Frustum and box tests:** a view frustum is six planes with normals pointing inward, and testing a bounding volume against it starts from the signed distance of the volume's centre to each plane. ## Common traps | Trap | Consequence | |---|---| | Normal never normalised | Distances are off by a factor of `|n|`; side tests still work | | Vertex winding reversed | The normal flips, so "front" and "behind" swap | | Mixing `dot(n, x) = d` and `dot(n, x) + d = 0` | The offset's sign is wrong and every distance shifts | | No guard on `dot(n, dir)` | Division by a near-zero value for rays parallel to the plane | A hitscan shot against a flat floor is the simplest case of all of this: compute `t` from the ray-plane formula, reject it if it is negative or beyond the weapon's range, and the hit point is `o + t*dir`.

  • How do you intersect a ray with that plane?
    Substitute the ray `o + t*dir` into `dot(n, x) = d` to get `t = (d - dot(n, o)) / dot(n, dir)`. If `dot(n, dir)` is near zero the ray is parallel and either misses or lies in the plane. A negative `t` means the plane is behind the origin. The sign of `dot(n, dir)` says whether the ray approaches from the front (negative) or from behind (positive).
  • How do you tell where a segment crosses the plane?
    Take the signed distances `s0` and `s1` of its endpoints. If they have the same sign it does not cross. If they differ, the crossing is at `t = s0 / (s0 - s1)` along the segment from the first endpoint, because the signed distance varies linearly along it and reaches zero there.
  • How do you normalise a plane stored as an unnormalised (n, d)?
    Divide both `n` and `d` by `|n|`. Dividing only the normal moves the plane, because the equation `dot(n, x) = d` must be scaled on both sides to describe the same set of points.

Signed distance works like altitude measured from sea level: the plane is the sea surface, the normal points up, positive values are above the water, negative values are below it, and zero is the shoreline itself.

  • The offset d is the distance from the origin even when the normal is not unit length.
  • A negative signed distance means the calculation went wrong.
  • The cross product of two edges points the same way whatever the vertex order.
  • Ray-plane intersection never needs a guard on the denominator.
  • Normalising a plane means dividing only its normal by its length.
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

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 do you find the closest point on a triangle to a given point, and why is projecting onto its plane and clamping barycentric coordinates wrong?

level: seniorimportance: should knowfreq 35%

basics

~20 s

Project the point onto the triangle's plane; if its barycentrics are all non-negative, that is the answer. Otherwise the closest point lies on an edge or vertex: test Voronoi regions or take the nearest clamped edge point. Clamping barycentrics picks a wrong point.

open as a page

Why can a slab-method ray-versus-AABB test hit or miss crates inconsistently when a y-up top-down shooter fires bullet rays whose y component is exactly zero, and how do you fix it?

level: seniorimportance: should knowfreq 40%

basics

~20 s

With d.y = 0, the slab terms (bmin.y - o.y) / d.y become infinities, which work, until o.y lies exactly on a box face: 0 * infinity is NaN, and how min, max and comparisons treat NaN decides hit or miss. Handle parallel axes explicitly.

open as a page