# OrientedBox

Class · category: Math

Source: https://github.com/playcanvas/engine/blob/970d89f6d3bc6667f4e1f88153abd6e7e82ed52c/src/core/shape/oriented-box.js#L36

An oriented bounding box is a box that can be rotated and translated in 3D space. It is defined
by a world transform and half extents. Unlike an axis-aligned bounding box, an OBB can be
oriented arbitrarily.

The constructor takes a [Mat4](https://api.playcanvas.com/engine/classes/Mat4.md) holding the box's position and rotation, then its half
extents. Scale is assumed to be one, so size the box with the half extents rather than the
matrix. [worldTransform](https://api.playcanvas.com/engine/classes/OrientedBox.md#worldtransform) can be reassigned at any time and the setter copies the matrix, so
assigning an entity's world transform each frame gives a box that follows it. The tests
[containsPoint](https://api.playcanvas.com/engine/classes/OrientedBox.md#containspoint), [intersectsRay](https://api.playcanvas.com/engine/classes/OrientedBox.md#intersectsray) and [intersectsBoundingSphere](https://api.playcanvas.com/engine/classes/OrientedBox.md#intersectsboundingsphere) return a
boolean and allocate nothing.

**Example**

```ts
// A 2 x 1 x 4 box that follows an entity
const box = new OrientedBox(entity.getWorldTransform(), new Vec3(1, 0.5, 2));

// later, after the entity has moved
box.worldTransform = entity.getWorldTransform();
if (box.containsPoint(player.getPosition())) {
    // the player is inside the box
}
```

## Constructors

### constructor

```ts
new OrientedBox(worldTransform?: Mat4, halfExtents?: Vec3)
```

Create a new OrientedBox instance.

**Parameters**

- `worldTransform` ([`Mat4`](https://api.playcanvas.com/engine/classes/Mat4.md), optional): Transform that has the orientation and position of the box.
  Scale is assumed to be one. Defaults to identity matrix.
- `halfExtents` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md), optional): Half the distance across the box in each local axis. Defaults
  to (0.5, 0.5, 0.5).

## Accessors

### worldTransform

```ts
get worldTransform(): Mat4
set worldTransform(value: Mat4)
```

Gets the world transform of the OBB.

## Methods

### containsPoint

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

Test if a point is inside an OBB.

**Parameters**

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

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

### intersectsBoundingSphere

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

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

**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 this OBB
and false otherwise.

### intersectsRay

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

Test if a ray intersects with the OBB.

**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.
