skip to content

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%

answer

  1. project the box onto the normal
  2. half-extents times absolute normal
  3. the corner farthest along the normal
  4. three outcomes per plane

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.

solid answer

~50 s

I store the box as centre `c` and half-extents `e` and compute the signed distance of the centre, `s = dot(n, c) - d`. The box's points reach at most `r = e.x*|n.x| + e.y*|n.y| + e.z*|n.z|` either side of that along the normal, so `s > r` means fully in front, `s < -r` fully behind, and otherwise the box straddles the plane. The equivalent min/max form picks the **positive vertex**, the corner farthest along `n` (max on each axis where `n` is non-negative, min elsewhere): if it is behind the plane, the whole box is. For a frustum with inward normals, a box fully behind any plane is outside, in front of all six is inside, else intersecting. The test is conservative near frustum corners, and since `s` and `r` scale together with `|n|`, it needs no normalised planes.

code

pseudocode · 16 lines
pseudocode
// plane: dot(n, x) = d; box: centre c, half-extents e
function classifyBox(n, d, c, e):
    s = dot(n, c) - d
    r = e.x*abs(n.x) + e.y*abs(n.y) + e.z*abs(n.z)
    if s > r:  return FRONT
    if s < -r: return BEHIND
    return STRADDLING

// frustum planes have inward normals
function boxInFrustum(planes, c, e):
    result = INSIDE
    for (n, d) in planes:
        side = classifyBox(n, d, c, e)
        if side == BEHIND:     return OUTSIDE
        if side == STRADDLING: result = INTERSECTING
    return result

go deeper

for a junior

Recall that a box covers a range of signed distances, so it can be fully in front, fully behind or straddling a plane.

for a middle

Derive the projected radius from the half-extents and absolute normal, explain the positive vertex, and run the six-plane loop with its early exit.

for a senior

Explain why the six-plane test is conservative near frustum corners, why unnormalised planes still give correct verdicts, and how the radius extends to oriented boxes.

for a principal

Weigh how much exactness the primitive should buy against how many boxes are tested per frame, and keep false positives a deliberate, measured cost rather than an accident.

## The problem Visibility checks, trigger volumes and spatial queries keep asking one primitive question: is this **axis-aligned bounding box (AABB)** in front of a plane, behind it, or crossing it? A **view frustum** is six such planes (left, right, top, bottom, near, far), with normals pointing into the visible volume. Running the box through all six answers "outside, inside or partly inside". The brute-force answer transforms all eight corners and computes eight signed distances per plane. Both methods below do it with one or two dot products. ## The projected radius Store the box as a **centre** `c` and **half-extents** `e = (e.x, e.y, e.z)`, so every point of the box is `c + (x, y, z)` with `|x| <= e.x`, `|y| <= e.y`, `|z| <= e.z`. With the plane `dot(n, x) = d`: 1. The signed distance of a box point is `dot(n, c) - d + n.x*x + n.y*y + n.z*z`. 2. Write `s = dot(n, c) - d`, the centre's signed distance. 3. The remaining sum is largest when each term is positive and as big as it can be: `x = e.x * sign(n.x)` and so on, giving the **projected radius** `r = e.x*|n.x| + e.y*|n.y| + e.z*|n.z|`. 4. So the box covers exactly the signed-distance interval `[s - r, s + r]`. The verdict follows: - `s > r`: the whole box is in front of the plane. - `s < -r`: the whole box is behind it. - Otherwise the interval contains zero and the box straddles the plane. Precomputing `|n|` component-wise per plane makes `r` a single dot product, `dot(e, abs(n))`. ## The positive and negative vertex The same idea in min/max form picks corners instead of computing a radius. For a box with corners `bmin` and `bmax`: - The **positive vertex** `pv` takes `bmax` on each axis where `n` is non-negative and `bmin` where it is negative. It is the corner farthest along the normal, with signed distance `s + r`. - The **negative vertex** `nv` takes the opposite choice, with signed distance `s - r`. If `pv` is behind the plane, every corner is, so the box is fully behind. If `nv` is in front, the box is fully in front. Both forms compute the same interval; centre-extents suits boxes stored that way and avoids per-axis selects. ## Using it for a frustum With inward-pointing normals: 1. For each of the six planes, compute the verdict. 2. If any plane reports **fully behind**, the box is outside: stop. 3. If every plane reports **fully in front**, the box is entirely inside. 4. Otherwise it intersects the frustum, or may only appear to. Step 2's early exit is where much of the saving comes from: in a large world, many boxes are rejected by the first plane or two. ## Conservativeness and cost The six-plane test can report a box as intersecting when it is actually outside. A box near a frustum corner can sit outside two planes' intersection edge while straddling each plane individually, so no single plane rejects it. For visibility that is safe, since a false "visible" costs extra work and never makes a visible object vanish. A tighter test costs extra separating directions, and whether it pays is a decision for the culling system, not for this primitive. | Method | Work per plane | Notes | |---|---|---| | Eight corners | Eight dot products | Simple, wasteful | | Positive and negative vertex | Three selects, one or two dot products | Natural for min/max boxes | | Centre and projected radius | Two dot products | Natural for centre-extents boxes | ## Extensions - **Unnormalised planes:** `s` and `r` are both multiplied by `|n|`, so the verdict is unchanged; only the distances lose their units. - **Oriented boxes:** with unit axes `u0`, `u1`, `u2`, the radius becomes `e.x*|dot(n, u0)| + e.y*|dot(n, u1)| + e.z*|dot(n, u2)|`. - **Spheres:** the projected radius of a sphere is just its radius, so the same three-way test applies with `r` fixed. Where the six planes come from (extracting them from the camera's matrices) and which objects get tested are separate subjects; this test is the arithmetic every frustum check reduces to.

  • Why does the verdict survive planes whose normals were never normalised?
    If the plane is `(k*n, k*d)` for some positive `k`, the centre's signed distance becomes `k*s` and the projected radius becomes `k*r`. Comparisons like `s > r` are unchanged by a common positive factor, so the in, out or straddling verdict is identical; only the numeric distances are scaled.
  • How does the test change for an oriented box?
    Project each of the box's own unit axes onto the normal: `r = e.x*|dot(n, u0)| + e.y*|dot(n, u1)| + e.z*|dot(n, u2)|`. For an axis-aligned box those axes are the world axes, and the formula reduces to the absolute normal components.
  • A box well outside the frustum, near one of its corners, is reported as intersecting. Is the test broken?
    No. The box straddles each of the planes meeting at that corner without lying fully behind any one of them, so a per-plane test cannot reject it. The result errs toward keeping the object, which is safe for visibility; rejecting it needs extra separating directions, a cost the culling system decides whether to pay.

  • Testing the box's centre against the plane is enough to decide the box's side.
  • A box not fully behind any frustum plane must overlap the frustum.
  • The projected radius uses the normal's components without absolute values.
  • The positive vertex is the box corner closest to the plane.
  • Plane normals must be unit length for the in or out verdict to be right.