skip to content

What does the separating axis theorem say, and which candidate axes must a collision test check for two oriented boxes in 3D?

level: middleimportance: must knowfreq 60%

answer

  1. projections that do not overlap
  2. convex shapes only
  3. faces of each, plus edge pairs
  4. opposite faces share an axis

basics

~20 s

Two convex shapes are disjoint exactly when some axis separates their projections. For two oriented boxes in 3D, testing 15 axes is enough: the 3 face normals of each box and the 9 cross products of one box's edge directions with the other's.

solid answer

~50 s

The separating axis theorem says two convex shapes do not intersect if and only if there is an axis on which their projections do not overlap. For convex polyhedra only a finite set of axes can separate: the face normals of each shape and the cross products of an edge direction from one with an edge direction from the other. A box has 3 unique face normals and 3 unique edge directions, so two boxes need `3 + 3 + 3*3 = 15` axes. On each axis `L`, project each box to a radius `r = e0*|dot(L, u0)| + e1*|dot(L, u1)| + e2*|dot(L, u2)|` and report separation if `|dot(cB - cA, L)| > rA + rB`; the first such axis is an early out. If none separates, the axis with the smallest overlap, measured on a normalised axis, gives the contact normal and depth and says whether the contact is face or edge-edge. Skip near-zero cross products from parallel edges.

code

pseudocode · 22 lines
pseudocode
function satBoxBox(A, B):          // each box: centre c, unit axes u[0..2], half extents e[0..2]
    T = B.c - A.c
    axes = [A.u[0], A.u[1], A.u[2], B.u[0], B.u[1], B.u[2]]
    for i in [0, 1, 2]:
        for j in [0, 1, 2]:
            axes.push(cross(A.u[i], B.u[j]))
    bestOverlap = INFINITY
    for L in axes:
        len = length(L)
        if len < EPSILON:
            continue                    // parallel edges: covered by a face axis
        L = L / len
        rA = A.e[0]*abs(dot(L, A.u[0])) + A.e[1]*abs(dot(L, A.u[1])) + A.e[2]*abs(dot(L, A.u[2]))
        rB = B.e[0]*abs(dot(L, B.u[0])) + B.e[1]*abs(dot(L, B.u[1])) + B.e[2]*abs(dot(L, B.u[2]))
        dist = abs(dot(T, L))
        if dist > rA + rB:
            return NO_CONTACT           // found a separating axis
        overlap = rA + rB - dist
        if overlap < bestOverlap:
            bestOverlap = overlap
            normal = L if dot(T, L) >= 0 else -L   // point from A toward B
    return (normal, bestOverlap)

go deeper

for a junior

Recall that SAT applies to convex shapes and that a single axis with a gap between the projections proves the shapes are apart.

for a middle

Explain where the 15 axes for two boxes come from, how a box projects onto an axis, and why the first separating axis lets the test exit early.

for a senior

Show the numerical judgment: skipping parallel-edge axes, normalising before comparing overlaps, biasing toward face axes and caching last frame's separating axis.

for a principal

Decide where SAT stays the right tool, such as boxes and simple hulls, and where its edge-pair cost on detailed hulls makes a support-function test the better default.

## The theorem in one sentence The **separating axis theorem (SAT)** says: two **convex** shapes are disjoint if and only if there exists an axis (a direction) onto which their projections are two intervals that do not overlap. Picture shining a light perpendicular to the axis: if the two shadows on the axis have a gap between them, a plane perpendicular to that axis fits between the shapes. The theorem is only valid for convex shapes. A concave mesh must be split into convex pieces (or handled triangle by triangle) before SAT applies. ## Why a finite set of axes is enough In principle there are infinitely many directions to try. For **convex polyhedra** the theorem tightens: if any separating axis exists, one of these does: - a **face normal** of the first shape, - a **face normal** of the second shape, - the **cross product** of an edge direction of the first with an edge direction of the second, which covers two edges passing each other at an angle. For a box, opposite faces share an axis and parallel edges share a direction, so a box contributes **3 face normals** and **3 edge directions** (its own three local axes). Two boxes therefore need `3 + 3 + 3 * 3 = 15` candidate axes. Testing only the six face normals is the classic mistake: two boxes positioned edge to edge, rotated against each other, can pass all six face tests while still being apart. ## Projecting an oriented box onto an axis An **oriented box** has a centre `c`, three unit axes `u0, u1, u2` and three half extents `e0, e1, e2`. Its projection onto axis `L` is an interval centred on `dot(c, L)` with radius `r = e0 * |dot(L, u0)| + e1 * |dot(L, u1)| + e2 * |dot(L, u2)|` Rather than comparing two intervals, compare the distance between centres with the sum of radii: with `T = cB - cA`, the boxes are separated on `L` when `|dot(T, L)| > rA + rB`. This comparison gives the right yes or no answer even if `L` is not unit length, because both sides scale by the same factor. ## The test, step by step 1. Build the 15 candidate axes: A's three axes, B's three axes, and `cross(A.u[i], B.u[j])` for all nine pairs. 2. For each axis, skip it if its length is nearly zero; that happens when an edge of A is parallel to an edge of B, and the face axes already cover that configuration. 3. Normalise the axis, compute `rA`, `rB` and `|dot(T, L)|`. 4. If the distance exceeds `rA + rB`, return **no contact** immediately. Most pairs that reach narrow phase are separated, so this early out is where SAT earns its keep. 5. Otherwise record the **overlap** `rA + rB - |dot(T, L)|` and keep the axis with the smallest overlap. 6. If all 15 axes overlap, the boxes intersect. ## From overlap test to contact normal When no axis separates, the axis of **minimum overlap** is the direction along which the boxes can be pushed apart the shortest distance. That axis, flipped so it points from A toward B, is the **contact normal**, and the overlap is the **penetration depth**. The kind of axis that won tells you the contact type: | Winning axis | Contact type | Next step | |---|---|---| | Face normal of A | Face of A against B | A's face is the reference face for clipping | | Face normal of B | Face of B against A | B's face is the reference face | | Edge cross product | Edge against edge | One point from the closest points of the two edges | ## Traps interviewers listen for - **Normalise before comparing overlaps.** On an unnormalised cross product the overlap is scaled by the axis length, so it is not a distance and cannot be compared with face-axis overlaps. - **Parallel edges.** A cross product of two nearly parallel edges is a tiny vector; normalising it amplifies rounding error into a random direction that can report a false separation or a bogus minimum. - **Face bias.** When a face axis and an edge axis overlap by nearly the same amount, prefer the face axis unless the edge axis is clearly better by a small tolerance. Otherwise the contact type flips frame to frame and resting boxes jitter. - **Coherence.** Caching the axis that separated a pair last frame and testing it first makes the early out happen on the first axis most of the time. - **Cost on general hulls.** For two convex hulls, edge-pair axes grow with the product of their unique edge directions; hulls with dozens of edge directions each need hundreds of cross products, which is why pruning schemes or GJK-style tests are used for complex hulls. SAT is the narrow-phase algorithm interviewers most often ask candidates to sketch because it is short, exact for boxes, and exposes whether the candidate understands both the geometry and its numerical traps.

  • Why skip a cross-product axis whose length is nearly zero instead of normalising it?
    A near-zero cross product means the two edges are almost parallel. Normalising it turns rounding noise into an arbitrary direction, which can falsely report a separation or win as the minimum-overlap axis with a meaningless normal. For two boxes the face-normal axes already cover the parallel-edge configuration, so skipping the axis loses nothing.
  • How does the SAT result tell you whether the contact is face-face or edge-edge?
    Track which kind of axis gave the minimum overlap. A face normal of either box means a face contact, and that face becomes the reference face for building the manifold. An edge cross product means two edges cross, and the contact is a single point from the closest points of those two edges.
  • Why is SAT expensive on two general convex hulls?
    Face axes grow linearly, but edge axes grow with the product of each hull's unique edge directions, so detailed hulls need hundreds of cross products. Implementations prune edge pairs that cannot form a face of the Minkowski difference, cache last frame's separating axis, or switch to GJK, which only needs a support function.

  • Testing the six face normals is enough for two boxes in 3D.
  • SAT works on concave meshes if you test every triangle normal.
  • Overlaps on unnormalised cross-product axes can be compared with face-axis overlaps.
  • Each box contributes six face axes and twelve edge directions.
  • The axis with the largest overlap gives the contact normal.