Skip to content

@web-engine-dev/math


@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

ts
// 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

readonly max: Vec3


min

readonly min: Vec3

Accessors

center

Get Signature

get center(): Vec3

Returns

Vec3


extents

Get Signature

get extents(): Vec3

Returns

Vec3


size

Get Signature

get size(): Vec3

Returns

Vec3


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

Vec3

Returns

Vec3


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

Vec3

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

Vec3

Returns

number


distanceToPoint()

distanceToPoint(point): number

Returns the distance from a point to the AABB.

Parameters

point

Vec3

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

Vec3

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

Vec3

direction

Vec3

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

Mat4

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

Mat4

Transformation matrix

out

Output AABB (min and max Vec3 must be mutable)

max

Vec3

min

Vec3

Returns

object

out for chaining

max

max: Vec3

min

min: Vec3


translate()

translate(offset): AABB

Translates the AABB by a vector.

Parameters

offset

Vec3

Returns

AABB


createCornersArray()

static createCornersArray(): Vec3[]

Creates a preallocated array of 8 Vec3 for use with getCornersOut(). Call once during initialization and reuse.

Returns

Vec3[]


createMutable()

static createMutable(): 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()

static empty(): AABB

Returns

AABB


fromCenterExtents()

static fromCenterExtents(center, extents): AABB

Parameters

center

Vec3

extents

Vec3

Returns

AABB


fromCenterSize()

static fromCenterSize(center, size): AABB

Parameters

center

Vec3

size

Vec3

Returns

AABB


fromMinMax()

static fromMinMax(min, max): AABB

Parameters

min

Vec3

max

Vec3

Returns

AABB


fromPoints()

static fromPoints(points): AABB

Parameters

points

Vec3[]

Returns

AABB


infinite()

static infinite(): AABB

Returns

AABB

Proprietary software. All rights reserved.