Skip to content

rayVsAabb

rayVsAabb(origin, dir, box): RayHit | null

Defined in: src/modules/collision/narrowphase.ts:199

Ray vs AABB, slab method. Returns the entry t (strictly positive) and the axis of the face it enters through, or null when the ray misses, the box lies entirely behind the origin, or the origin is on or inside the box.

t is parametric in units of dir, which is what makes one function serve both ray shapes: pass a unit vector for a world distance (hitscan, picking), or the segment vector to - from and treat t <= 1 as “the segment crosses the box” (line-of-sight against walls).

A zero-length dir never hits. A ray travelling exactly along a face counts as a hit once it enters that face’s span from outside (the slab method treats boxes as closed) — an origin already sitting on the boundary is still a miss, as above — and so does a corner graze, where entry and exit land on the same t. Its axis is whichever face the ray ultimately enters through, which need not be the face it grazes. When two axes enter at the same t — a ray through an exact corner or crack — the y face wins, consistent with aabbVsAabbSwept and bounceOffAabb.

Note that the 3D copies in examples/portal / examples/doom break corner ties the other way (x wins), and use a small tolerance rather than an exact zero test, so a port is not a byte-for-byte match.

For “is the origin already inside?” — which this function answers null — call aabbContainsPoint instead of paying for a flag on this path.

Vec2

Vec2

Aabb

RayHit | null