Engine API Reference - v2.23.0-beta.17
    Preparing search index...

    Class BoundingBox

    Axis-Aligned Bounding Box. An AABB is commonly used for fast overlap tests in collision detection, spatial indexing and frustum culling.

    A box is stored as a center and halfExtents. Set it from its extreme corners with setMinMax and read them back with getMin and getMax. Fit a box to vertex data with compute, grow it to enclose another box with add, and move a local box into world space with setFromTransformedAabb, which is how the engine derives a mesh instance's world bounds from its mesh's local bounds.

    Tests such as intersects, containsPoint and intersectsRay return a boolean and allocate nothing. closestPoint writes into an optional result vector, while getMin and getMax return the box's own cached vectors, which should be treated as read-only. The constructor copies the vectors it is given.

    // Enclose every mesh instance of a render component in one box
    const bounds = new BoundingBox();
    entity.render.meshInstances.forEach((meshInstance, i) => {
    if (i === 0) {
    bounds.copy(meshInstance.aabb);
    } else {
    bounds.add(meshInstance.aabb);
    }
    });
    // Pick against a box; the ray's direction must be normalized
    const hit = new Vec3();
    if (bounds.intersectsRay(ray, hit)) {
    console.log(`Hit at ${hit}`);
    }
    Index
    • Create a new BoundingBox instance. The bounding box is axis-aligned.

      Parameters

      • Optionalcenter: Vec3

        Center of box. The constructor copies this parameter. Defaults to (0, 0, 0).

      • OptionalhalfExtents: Vec3

        Half the distance across the box in each axis. The constructor copies this parameter. Defaults to (0.5, 0.5, 0.5).

      Returns BoundingBox

    center: Vec3 = ...

    Center of box.

    halfExtents: Vec3 = ...

    Half the distance across the box in each axis.

    • Return the point on the AABB closest to a given point. If the point is inside the AABB, the point itself is returned.

      Parameters

      • point: Vec3

        Point to find the closest point to.

      • Optionalresult: Vec3 = ...

        The vector to store the result in. If not provided, a new Vec3 is created and returned.

      Returns Vec3

      The closest point on the AABB.

      const box = new BoundingBox(new Vec3(0, 0, 0), new Vec3(1, 1, 1));
      const point = new Vec3(2, 0, 0);
      const closest = box.closestPoint(point); // Returns Vec3(1, 0, 0)
      // Reuse a result vector to avoid allocations in hot paths
      const result = new Vec3();
      box.closestPoint(point, result);
    • Compute the size of the AABB to encapsulate all specified vertices.

      Parameters

      • vertices: ArrayLike<number>

        The vertices used to compute the new size for the AABB.

      • OptionalnumVerts: number

        Number of vertices to use from the beginning of vertices array. All vertices are used if not specified.

      Returns void

    • Test if a point is inside an AABB.

      Parameters

      • point: Vec3

        Point to test.

      Returns boolean

      True if the point is inside the AABB and false otherwise.

    • Reports whether two axis-aligned bounding boxes are equal.

      Parameters

      Returns boolean

      True if the AABBs have the same center and half extents, false otherwise.

    • Test if a Bounding Sphere is overlapping, enveloping, or inside this AABB.

      Parameters

      Returns boolean

      True if the Bounding Sphere is overlapping, enveloping, or inside the AABB and false otherwise.

    • Test if a ray intersects with the AABB.

      Parameters

      • ray: Ray

        Ray to test against (direction must be normalized).

      • Optionalpoint: Vec3

        If there is an intersection, the intersection point will be copied into here.

      Returns boolean

      True if there is an intersection.

    • Set an AABB to enclose the specified AABB if it were to be transformed by the specified 4x4 matrix.

      Parameters

      • aabb: BoundingBox

        Box to transform and enclose.

      • m: Mat4

        Transformation matrix to apply to source AABB.

      • ignoreScale: boolean = false

        If true is specified, a scale from the matrix is ignored. Defaults to false.

      Returns void

    • Sets the minimum and maximum corner of the AABB. Using this function is faster than assigning min and max separately.

      Parameters

      • min: Vec3

        The minimum corner of the AABB.

      • max: Vec3

        The maximum corner of the AABB.

      Returns void

    • Compute the min and max bounding values to encapsulate all specified vertices.

      Parameters

      • vertices: ArrayLike<number>

        The vertices used to compute the new size for the AABB.

      • min: Vec3

        Stored computed min value.

      • max: Vec3

        Stored computed max value.

      • OptionalnumVerts: number = ...

        Number of vertices to use from the beginning of vertices array. All vertices are used if not specified.

      Returns void