@web-engine-dev/math / AABB
Class: AABB
An Axis-Aligned Bounding Box (AABB). Defined by minimum and maximum corner points. Immutable by design - all operations return new instances.
Example
// Create from center and size
const box = AABB.fromCenterSize(Vec3.zero(), new Vec3(10, 10, 10));
// Check if point is inside
if (box.containsPoint(point)) { ... }
// Expand to include another box
const combined = box.expandByBox(otherBox);Constructors
Constructor
new AABB(
min?,max?):AABB
Parameters
min?
Vec3 = ...
max?
Vec3 = ...
Returns
AABB
Properties
max
readonlymax:Vec3
min
readonlymin:Vec3
Accessors
center
Get Signature
get center():
Vec3
Returns
extents
Get Signature
get extents():
Vec3
Returns
size
Get Signature
get size():
Vec3
Returns
surfaceArea
Get Signature
get surfaceArea():
number
Returns
number
volume
Get Signature
get volume():
number
Returns
number
Methods
closestPoint()
closestPoint(
point):Vec3
Returns the closest point on or in the AABB to the given point.
Parameters
point
Returns
containsBox()
containsBox(
box):boolean
Checks if this AABB fully contains another AABB.
Parameters
box
AABB
Returns
boolean
containsPoint()
containsPoint(
point):boolean
Checks if this AABB contains a point.
Parameters
point
Returns
boolean
distanceSqToPoint()
distanceSqToPoint(
point):number
Returns the squared distance from a point to the AABB. Returns 0 if the point is inside.
Parameters
point
Returns
number
distanceToPoint()
distanceToPoint(
point):number
Returns the distance from a point to the AABB.
Parameters
point
Returns
number
equals()
equals(
box,epsilon?):boolean
Parameters
box
AABB
epsilon?
number = 0
Returns
boolean
expand()
expand(
amount):AABB
Expands the AABB in all directions by a scalar.
Parameters
amount
number
Returns
AABB
expandByBox()
expandByBox(
box):AABB
Expands the AABB to include another AABB.
Parameters
box
AABB
Returns
AABB
expandByPoint()
expandByPoint(
point):AABB
Expands the AABB to include a point.
Parameters
point
Returns
AABB
getCorners()
getCorners():
Vec3[]
Returns
Vec3[]
getCornersOut()
getCornersOut(
out):Vec3[]
Writes the 8 corners of the box to a preallocated array. Zero-allocation alternative to getCorners() for hot paths.
Parameters
out
Vec3[]
Array of exactly 8 Vec3 objects to write to
Returns
Vec3[]
out for chaining
intersection()
intersection(
box):AABB
Computes the intersection of this AABB with another. Returns empty AABB if they don't intersect.
Parameters
box
AABB
Returns
AABB
intersectsBox()
intersectsBox(
box):boolean
Checks if this AABB intersects another AABB.
Parameters
box
AABB
Returns
boolean
intersectsRay()
intersectsRay(
origin,direction): [number,number] |null
Ray-AABB intersection test.
Parameters
origin
direction
Returns
[number, number] | null
[tMin, tMax] if intersection, null otherwise.
isEmpty()
isEmpty():
boolean
Returns
boolean
isFinite()
isFinite():
boolean
Returns
boolean
scale()
scale(
factor):AABB
Scales the AABB around its center.
Parameters
factor
number
Returns
AABB
toString()
toString():
string
Returns
string
transform()
transform(
matrix):AABB
Transforms the AABB by a matrix. Returns a new AABB that bounds the transformed corners.
Parameters
matrix
Returns
AABB
transformOut()
transformOut(
matrix,out):object
Transforms the AABB by a matrix and writes result to out. Zero-allocation alternative to transform() for hot paths.
Uses optimized AABB-OBB transformation that avoids transforming all 8 corners. Instead, it computes the new AABB directly from the matrix columns.
Parameters
matrix
Transformation matrix
out
Output AABB (min and max Vec3 must be mutable)
max
min
Returns
object
out for chaining
max
max:
Vec3
min
min:
Vec3
translate()
translate(
offset):AABB
Translates the AABB by a vector.
Parameters
offset
Returns
AABB
createCornersArray()
staticcreateCornersArray():Vec3[]
Creates a preallocated array of 8 Vec3 for use with getCornersOut(). Call once during initialization and reuse.
Returns
Vec3[]
createMutable()
staticcreateMutable():object
Creates a mutable AABB for use with transformOut(). The returned object has mutable min/max Vec3.
Returns
object
max
max:
Vec3
min
min:
Vec3
empty()
staticempty():AABB
Returns
AABB
fromCenterExtents()
staticfromCenterExtents(center,extents):AABB
Parameters
center
extents
Returns
AABB
fromCenterSize()
staticfromCenterSize(center,size):AABB
Parameters
center
size
Returns
AABB
fromMinMax()
staticfromMinMax(min,max):AABB
Parameters
min
max
Returns
AABB
fromPoints()
staticfromPoints(points):AABB
Parameters
points
Vec3[]
Returns
AABB
infinite()
staticinfinite():AABB
Returns
AABB