# BoundingBox

Class · category: Math

Source: https://github.com/playcanvas/engine/blob/f059dc005842f76e052cdf44d8370c8c7ec475ac/src/core/shape/bounding-box.js#L48

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](https://api.playcanvas.com/engine/classes/BoundingBox.md#center) and [halfExtents](https://api.playcanvas.com/engine/classes/BoundingBox.md#halfextents). Set it from its extreme corners with
[setMinMax](https://api.playcanvas.com/engine/classes/BoundingBox.md#setminmax) and read them back with [getMin](https://api.playcanvas.com/engine/classes/BoundingBox.md#getmin) and [getMax](https://api.playcanvas.com/engine/classes/BoundingBox.md#getmax). Fit a box to vertex
data with [compute](https://api.playcanvas.com/engine/classes/BoundingBox.md#compute), grow it to enclose another box with [add](https://api.playcanvas.com/engine/classes/BoundingBox.md#add), and move a local box
into world space with [setFromTransformedAabb](https://api.playcanvas.com/engine/classes/BoundingBox.md#setfromtransformedaabb), which is how the engine derives a mesh
instance's world bounds from its mesh's local bounds.

Tests such as [intersects](https://api.playcanvas.com/engine/classes/BoundingBox.md#intersects), [containsPoint](https://api.playcanvas.com/engine/classes/BoundingBox.md#containspoint) and [intersectsRay](https://api.playcanvas.com/engine/classes/BoundingBox.md#intersectsray) return a
boolean and allocate nothing. [closestPoint](https://api.playcanvas.com/engine/classes/BoundingBox.md#closestpoint) writes into an optional result vector, while
[getMin](https://api.playcanvas.com/engine/classes/BoundingBox.md#getmin) and [getMax](https://api.playcanvas.com/engine/classes/BoundingBox.md#getmax) return the box's own cached vectors, which should be treated as
read-only. The constructor copies the vectors it is given.

**Example**

```ts
// 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);
    }
});
```

**Example**

```ts
// 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}`);
}
```

## Constructors

### constructor

```ts
new BoundingBox(center?: Vec3, halfExtents?: Vec3)
```

Create a new BoundingBox instance. The bounding box is axis-aligned.

**Parameters**

- `center` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md), optional): Center of box. The constructor copies this parameter. Defaults to
  (0, 0, 0).
- `halfExtents` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md), optional): Half the distance across the box in each axis. The constructor
  copies this parameter. Defaults to (0.5, 0.5, 0.5).

## Properties

### center

```ts
readonly center: Vec3
```

Center of box.

### halfExtents

```ts
readonly halfExtents: Vec3
```

Half the distance across the box in each axis.

## Methods

### add

```ts
add(other: BoundingBox): void
```

Combines two bounding boxes into one, enclosing both.

**Parameters**

- `other` ([`BoundingBox`](https://api.playcanvas.com/engine/classes/BoundingBox.md)): Bounding box to add.

### clone

```ts
clone(): BoundingBox
```

Returns a clone of the AABB.

**Returns** [`BoundingBox`](https://api.playcanvas.com/engine/classes/BoundingBox.md): A duplicate AABB.

### closestPoint

```ts
closestPoint(point: Vec3, result?: Vec3): Vec3
```

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`](https://api.playcanvas.com/engine/classes/Vec3.md)): Point to find the closest point to.
- `result` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md), optional): The vector to store the result in. If not provided, a new Vec3 is
  created and returned.

**Returns** [`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md): The closest point on the AABB.

**Example**

```ts
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)
```

**Example**

```ts
// Reuse a result vector to avoid allocations in hot paths
const result = new Vec3();
box.closestPoint(point, result);
```

### compute

```ts
compute(vertices: ArrayLike<number>, numVerts?: number): void
```

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.
- `numVerts` (`number`, optional): Number of vertices to use from the beginning of vertices array.
  All vertices are used if not specified.

### containsPoint

```ts
containsPoint(point: Vec3): boolean
```

Test if a point is inside an AABB.

**Parameters**

- `point` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md)): Point to test.

**Returns** `boolean`: True if the point is inside the AABB and false otherwise.

### copy

```ts
copy(src: BoundingBox): void
```

Copies the contents of a source AABB.

**Parameters**

- `src` ([`BoundingBox`](https://api.playcanvas.com/engine/classes/BoundingBox.md)): The AABB to copy from.

### equals

```ts
equals(other: BoundingBox): boolean
```

Reports whether two axis-aligned bounding boxes are equal.

**Parameters**

- `other` ([`BoundingBox`](https://api.playcanvas.com/engine/classes/BoundingBox.md)): The AABB to compare to.

**Returns** `boolean`: True if the AABBs have the same center and half extents, false otherwise.

### getMax

```ts
getMax(): Vec3
```

Return the maximum corner of the AABB.

**Returns** [`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md): Maximum corner.

### getMin

```ts
getMin(): Vec3
```

Return the minimum corner of the AABB.

**Returns** [`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md): Minimum corner.

### intersects

```ts
intersects(other: BoundingBox): boolean
```

Test whether two axis-aligned bounding boxes intersect.

**Parameters**

- `other` ([`BoundingBox`](https://api.playcanvas.com/engine/classes/BoundingBox.md)): Bounding box to test against.

**Returns** `boolean`: True if there is an intersection.

### intersectsBoundingSphere

```ts
intersectsBoundingSphere(sphere: BoundingSphere): boolean
```

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

**Parameters**

- `sphere` ([`BoundingSphere`](https://api.playcanvas.com/engine/classes/BoundingSphere.md)): Bounding Sphere to test.

**Returns** `boolean`: True if the Bounding Sphere is overlapping, enveloping, or inside the
AABB and false otherwise.

### intersectsRay

```ts
intersectsRay(ray: Ray, point?: Vec3): boolean
```

Test if a ray intersects with the AABB.

**Parameters**

- `ray` ([`Ray`](https://api.playcanvas.com/engine/classes/Ray.md)): Ray to test against (direction must be normalized).
- `point` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md), optional): If there is an intersection, the intersection point will be copied
  into here.

**Returns** `boolean`: True if there is an intersection.

### setFromTransformedAabb

```ts
setFromTransformedAabb(aabb: BoundingBox, m: Mat4, ignoreScale?: boolean): void
```

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

**Parameters**

- `aabb` ([`BoundingBox`](https://api.playcanvas.com/engine/classes/BoundingBox.md)): Box to transform and enclose.
- `m` ([`Mat4`](https://api.playcanvas.com/engine/classes/Mat4.md)): Transformation matrix to apply to source AABB.
- `ignoreScale` (`boolean`, optional, default `false`): If true is specified, a scale from the matrix is ignored. Defaults to false.

### setMinMax

```ts
setMinMax(min: Vec3, max: Vec3): void
```

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

**Parameters**

- `min` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md)): The minimum corner of the AABB.
- `max` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md)): The maximum corner of the AABB.

### computeMinMax

```ts
static computeMinMax(vertices: ArrayLike<number>, min: Vec3, max: Vec3, numVerts?: number): 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`](https://api.playcanvas.com/engine/classes/Vec3.md)): Stored computed min value.
- `max` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md)): Stored computed max value.
- `numVerts` (`number`, optional): Number of vertices to use from the beginning of vertices array.
  All vertices are used if not specified.
