skip to content

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%

answer

  1. one over zero is infinity
  2. zero times infinity
  3. NaN poisons min and max
  4. parallel ray: inside the slab or never

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.

solid answer

~50 s

The slab method intersects the ray's `t` interval with each axis's `[(bmin - o) / d, (bmax - o) / d]`, sorted, and reports a hit if the interval stays non-empty. With `d.y = 0`, IEEE 754 gives `1 / d.y = ±infinity`, and the y slab yields `-inf` and `+inf` when the muzzle is strictly between the box's y bounds (no constraint) or two same-signed infinities when it is outside (a miss), both correct. When muzzle height equals a crate's top or bottom exactly, `0 * infinity` is NaN, and the outcome depends on which operand a min or max returns for NaN and on how the final comparison is written, so grazing crates hit or miss by implementation detail. The fix is an explicit parallel branch: if `|d.y|` is below a tolerance, miss if `o.y` is outside `[bmin.y, bmax.y]`, otherwise skip that axis, with the boundary rule chosen deliberately.

code

pseudocode · 19 lines
pseudocode
function rayAabb(o, d, bmin, bmax, maxRange):
    tNear = 0
    tFar = maxRange
    for axis in [x, y, z]:
        if abs(d[axis]) < EPS:
            // parallel: inside this slab for every t, or never
            if o[axis] < bmin[axis] or o[axis] > bmax[axis]:
                return MISS
            continue
        inv = 1 / d[axis]
        t1 = (bmin[axis] - o[axis]) * inv
        t2 = (bmax[axis] - o[axis]) * inv
        if t1 > t2:
            swap(t1, t2)
        tNear = max(tNear, t1)
        tFar = min(tFar, t2)
        if tNear > tFar:
            return MISS
    return HIT(tNear)                 // 0 when the origin starts inside

go deeper

for a junior

Recall that the slab method intersects the ray's t interval with one interval per axis, and a hit needs all three to overlap.

for a middle

Explain the slab steps with a precomputed reciprocal direction, and what IEEE 754 infinities do for a ray parallel to an axis.

for a senior

Diagnose the grazing-ray inconsistency from 0 times infinity, show how min, max and the final comparison decide its fate, and fix it with an explicit parallel branch and boundary rule.

for a principal

Weigh branch-free, NaN-ordered intersection code against explicit parallel handling across scalar and vectorised paths, and decide which boundary rule every collision-adjacent query should share.

## The slab method An **axis-aligned bounding box (AABB)** is the overlap of three **slabs**: the region between two parallel planes on each axis, `bmin.x <= x <= bmax.x` and likewise for y and z. A ray `o + t*d` is inside a slab for an interval of `t`, and it is inside the box exactly where all three intervals overlap. The test: 1. Start with the ray's own interval: `tNear = 0`, `tFar = maxRange`. 2. For each axis, compute `t1 = (bmin - o) / d` and `t2 = (bmax - o) / d` on that axis, and swap them if `t1 > t2`. 3. Shrink the interval: `tNear = max(tNear, t1)`, `tFar = min(tFar, t2)`. 4. If `tNear > tFar`, the intervals do not overlap: a miss. Otherwise the ray enters at `tNear`. Implementations usually compute `invD = 1 / d` once per ray and multiply, since one ray is tested against many boxes. ## Where a zero component goes In a top-down shooter every bullet ray is horizontal, so `d.y` is exactly zero. Under default **IEEE 754** settings, dividing by zero does not stop the program: `1 / +0` is `+infinity` and `1 / -0` is `-infinity`. That is usually the right answer, because a ray parallel to the y slab is either inside it for every `t` or for none. | Muzzle height `o.y` against the crate | `t1`, `t2` on the y axis | Effect | |---|---|---| | Strictly between `bmin.y` and `bmax.y` | `-inf`, `+inf` | No constraint: correct | | Below `bmin.y` or above `bmax.y` | Both `+inf` or both `-inf` | Empty interval, a miss: correct | | Exactly equal to `bmin.y` or `bmax.y` | One of them is `0 * inf` = NaN | Undefined by the math; decided by the code | The third row is the bug. A crate whose top sits exactly at muzzle height, common when crates and the gun are placed on round heights, produces a NaN. ## Why the symptom is inconsistent A NaN fails every ordered comparison, so its fate depends on details nobody chose deliberately: - A `min` written as `a < b ? a : b` returns `b` whenever either value is NaN, so the NaN survives or vanishes depending on which operand it is. - Some hardware min and max instructions also return a fixed operand when one is NaN, so a vectorised version can disagree with a scalar one. - If `tNear` becomes NaN, a final check written `if tNear > tFar: miss` reports a **hit**, while `if tFar >= tNear: hit` reports a **miss**. - Crates placed by transforms rather than typed-in values may sit a rounding error above or below the muzzle height, so neighbouring crates on the same row go different ways. The result is a grazing shot that hits one crate and passes over the next. Playtesters call it random; it is deterministic, just not designed. ## Fixes - **Explicit parallel branch (recommended):** if `|d[axis]|` is below a small tolerance, miss when `o[axis] < bmin[axis]` or `o[axis] > bmax[axis]`, and otherwise skip the axis. The comparisons decide the boundary rule; here a grazing ray counts as touching. - **NaN-aware ordering:** write each min and max so a NaN operand is consistently discarded. This keeps the branch-free form vectorised implementations like, at the cost of a subtle invariant every maintainer must preserve. - **Nudging zero components to a tiny value** avoids the NaN but answers the grazing case by the nudge's sign rather than by a chosen rule. Two related traps: - **Skipping the axis without checking the origin** treats every box as overlapping in y, so bullets hit crates hanging entirely above or below the muzzle. - **Compiler settings that assume no infinities or NaN** can rewrite the infinity-based form into something that no longer works; the floating-point reasons for that belong with numerical robustness. ## Other edge cases worth stating - **Origin inside the box:** `tNear` stays at 0, so the hit is reported at the muzzle; a query that needs the exit point uses `tFar`. - **Maximum range:** starting `tFar` at the weapon's range makes the test reject boxes beyond it for free. - **Signed zero:** `-0.0 == 0.0` is true, so a parallel check with a tolerance catches both zeros.

  • Why do the infinities themselves not need special handling?
    A ray parallel to a slab is inside it for all `t` or for none. When the origin is strictly inside, the two terms are infinities of opposite sign, which leave the interval unchanged; when it is outside, they are infinities of the same sign, which empty it. Only the exact-boundary case produces `0 * infinity`, a NaN.
  • Why is skipping the axis when its direction component is zero not a fix?
    Skipping drops the slab constraint entirely, which is only right when the origin lies within the slab. A bullet fired at waist height would then hit crates hanging entirely above it, because their y range is never checked. The parallel branch must test the origin against the slab before skipping.
  • Why precompute the reciprocal of the direction once per ray?
    A shot is usually tested against many boxes, for example while walking a bounding-volume hierarchy. Precomputing `1 / d` per axis turns six divisions per box into six multiplications, and the reciprocal's sign also tells the code which of `bmin` or `bmax` is the near plane without a swap.

  • Dividing by a zero float component always crashes, so infinities never arise.
  • If a direction component is zero, just skip that axis.
  • Infinities from dividing by zero are always wrong and must be filtered out.
  • Nudging zero direction components to a tiny value makes the test exact.
  • Every min and max implementation treats NaN operands the same way.