A 3D axis-aligned bounding box (AABB) class for spatial calculations and collision detection.

The AABB represents a rectangular box aligned with the coordinate axes, defined by minimum and maximum points. It provides efficient methods for spatial queries, transformations, and merging operations commonly used in 3D graphics and physics simulations.

const aabb = new AABB();
aabb.addPosition(Vector3.fromValues(1, 2, 3));
aabb.addPosition(Vector3.fromValues(4, 5, 6));
console.log(aabb.centerPoint); // Center of the bounding box

Constructors

  • Creates a new AABB instance in vanilla (uninitialized) state.

    The AABB is initially in "vanilla" state with invalid bounds until positions are added or bounds are explicitly set.

    Returns AABB

Accessors

  • get centerPoint(): MutableVector3
  • Gets the center point of the bounding box.

    The center point is calculated as the midpoint between the minimum and maximum points. This value is cached and only recalculated when the bounds change.

    Returns MutableVector3

    The center point of the AABB

    const aabb = new AABB();
    aabb.minPoint = Vector3.fromValues(0, 0, 0);
    aabb.maxPoint = Vector3.fromValues(4, 6, 8);
    console.log(aabb.centerPoint); // Vector3(2, 3, 4)
  • get lengthCenterToCorner(): number
  • Gets the distance from the center point to any corner of the bounding box.

    This represents the radius of a sphere that would completely contain the AABB when centered at the AABB's center point. Useful for sphere-based culling operations.

    Returns number

    The distance from center to corner

    const aabb = new AABB();
    aabb.minPoint = Vector3.fromValues(-1, -1, -1);
    aabb.maxPoint = Vector3.fromValues(1, 1, 1);
    console.log(aabb.lengthCenterToCorner); // sqrt(3) ≈ 1.732
  • get maxPoint(): Vector3
  • Gets the maximum point of the bounding box.

    Returns Vector3

    The maximum point as a read-only Vector3

  • set maxPoint(val): void
  • Sets the maximum point of the bounding box.

    Setting the maximum point will invalidate cached values and mark the AABB as non-vanilla.

    Parameters

    • val: Vector3

      The new maximum point vector

    Returns void

  • get minPoint(): Vector3
  • Gets the minimum point of the bounding box.

    Returns Vector3

    The minimum point as a read-only Vector3

  • set minPoint(val): void
  • Sets the minimum point of the bounding box.

    Setting the minimum point will invalidate cached values and mark the AABB as non-vanilla.

    Parameters

    • val: Vector3

      The new minimum point vector

    Returns void

  • get sizeX(): number
  • Gets the width of the bounding box along the X-axis.

    Returns number

    The difference between maximum and minimum X coordinates

    const aabb = new AABB();
    aabb.minPoint = Vector3.fromValues(1, 0, 0);
    aabb.maxPoint = Vector3.fromValues(5, 0, 0);
    console.log(aabb.sizeX); // 4
  • get sizeY(): number
  • Gets the height of the bounding box along the Y-axis.

    Returns number

    The difference between maximum and minimum Y coordinates

    const aabb = new AABB();
    aabb.minPoint = Vector3.fromValues(0, 2, 0);
    aabb.maxPoint = Vector3.fromValues(0, 8, 0);
    console.log(aabb.sizeY); // 6
  • get sizeZ(): number
  • Gets the depth of the bounding box along the Z-axis.

    Returns number

    The difference between maximum and minimum Z coordinates

    const aabb = new AABB();
    aabb.minPoint = Vector3.fromValues(0, 0, -2);
    aabb.maxPoint = Vector3.fromValues(0, 0, 3);
    console.log(aabb.sizeZ); // 5

Methods

  • Expands the AABB to include the given position.

    If this is the first position added to a vanilla AABB, it will set both min and max points to this position. Otherwise, it will expand the bounds as necessary to encompass the new position.

    Parameters

    • positionVector: Vector3

      The position to include in the bounding box

    Returns Vector3

    The input position vector for convenience

    const aabb = new AABB();
    aabb.addPosition(Vector3.fromValues(1, 2, 3));
    aabb.addPosition(Vector3.fromValues(4, 1, 2));
    // AABB now encompasses both points
  • Expands the AABB to include a position from an array at the specified index.

    This is a more efficient way to add positions when working with packed vertex data or other array-based position representations.

    Parameters

    • array: number[]

      The array containing position data

    • index: number

      The starting index in the array (x, y, z values at index, index+1, index+2)

    Returns number[]

    The input array for convenience

    const positions = [1, 2, 3, 4, 5, 6]; // Two 3D positions
    const aabb = new AABB();
    aabb.addPositionWithArray(positions, 0); // Add position (1, 2, 3)
    aabb.addPositionWithArray(positions, 3); // Add position (4, 5, 6)
  • Creates a deep copy of this AABB instance.

    All internal state including bounds, cached values, and dirty flags are copied to the new instance.

    Returns AABB

    A new AABB instance that is an exact copy of this one

    const originalAABB = new AABB();
    originalAABB.addPosition(Vector3.fromValues(1, 1, 1));
    const clonedAABB = originalAABB.clone();
  • Copies all components and state from another AABB into this instance.

    This is a more efficient alternative to creating a new instance when you want to overwrite the current AABB's state.

    Parameters

    • aabb: AABB

      The source AABB to copy from

    Returns AABB

    This AABB instance for method chaining

    const sourceAABB = new AABB();
    const targetAABB = new AABB();
    targetAABB.copyComponents(sourceAABB);
  • Resets this AABB to its initial vanilla state.

    Clears all bounds, cached values, and marks the AABB as vanilla. After initialization, the AABB will need new positions added to become valid.

    Returns void

    const aabb = new AABB();
    aabb.addPosition(Vector3.fromValues(1, 1, 1));
    aabb.initialize(); // Reset to vanilla state
    console.log(aabb.isVanilla()); // true
  • Checks whether this AABB is in vanilla (uninitialized) state.

    A vanilla AABB has not had any positions added and contains invalid bounds. Most operations on vanilla AABBs will return early or produce undefined results.

    Returns boolean

    True if this AABB is vanilla, false otherwise

    const aabb = new AABB();
    console.log(aabb.isVanilla()); // true
    aabb.addPosition(Vector3.fromValues(1, 1, 1));
    console.log(aabb.isVanilla()); // false
  • Merges this AABB with another AABB to create a combined bounding volume.

    The resulting AABB will encompass both the original and the merged AABB. If either AABB is vanilla, special handling is applied.

    Parameters

    • aabb: AABB

      The AABB to merge with this one

    Returns boolean

    True if the merge was successful, false if the input AABB is vanilla

    const aabb1 = new AABB();
    const aabb2 = new AABB();
    aabb1.addPosition(Vector3.fromValues(0, 0, 0));
    aabb2.addPosition(Vector3.fromValues(5, 5, 5));
    aabb1.mergeAABB(aabb2); // aabb1 now encompasses both volumes
  • Returns a string representation of the AABB with full precision.

    Includes minimum and maximum points, center point, and center-to-corner distance.

    Returns string

    A detailed string representation of the AABB

    const aabb = new AABB();
    aabb.addPosition(Vector3.fromValues(1, 2, 3));
    console.log(aabb.toString());
    // Output: "AABB_min: (1, 2, 3)\nAABB_max: (1, 2, 3)\n..."
  • Returns a string representation of the AABB with rounded numbers for readability.

    Similar to toString() but with approximate values that are easier to read. Useful for debugging and logging where exact precision is not required.

    Returns string

    A string representation with approximated numeric values

    const aabb = new AABB();
    aabb.addPosition(Vector3.fromValues(1.23456789, 2.3456789, 3.456789));
    console.log(aabb.toStringApproximately());
    // Output with rounded values for better readability
  • Transforms an AABB by a matrix and stores the result in an output AABB.

    This method transforms all 8 corners of the input AABB using the given matrix, then calculates the axis-aligned bounding box that encompasses all transformed corners. This is necessary because matrix transformations can rotate the AABB, requiring recalculation of the axis-aligned bounds.

    Parameters

    • matrix: Matrix44

      The transformation matrix to apply

    • aabb: AABB

      The source AABB to transform

    • outAabb: AABB

      The output AABB to store the result

    Returns AABB

    The output AABB for method chaining

    const matrix = Matrix44.rotateY(Math.PI / 4); // 45-degree rotation
    const sourceAABB = new AABB();
    const resultAABB = new AABB();
    sourceAABB.addPosition(Vector3.fromValues(1, 1, 1));
    AABB.multiplyMatrixTo(matrix, sourceAABB, resultAABB);