# CollisionComponent

Class · extends [`Component`](https://api.playcanvas.com/engine/classes/Component.md) · category: Physics

Source: https://github.com/playcanvas/engine/blob/f059dc005842f76e052cdf44d8370c8c7ec475ac/src/framework/components/collision/component.js#L65

The CollisionComponent enables an [Entity](https://api.playcanvas.com/engine/classes/Entity.md) to act as a collision volume. Use it on its own
to define a trigger volume. Or use it in conjunction with a [RigidBodyComponent](https://api.playcanvas.com/engine/classes/RigidBodyComponent.md) to make a
collision volume that can be simulated using the physics engine.

When an entity is configured as a trigger volume, if an entity with a dynamic or kinematic body
enters or leaves that trigger volume, both entities will receive trigger events.

You should never need to use the CollisionComponent constructor directly. To add a
CollisionComponent to an [Entity](https://api.playcanvas.com/engine/classes/Entity.md), use [Entity#addComponent](https://api.playcanvas.com/engine/classes/Entity.md#addcomponent):

```javascript
const entity = new Entity();
entity.addComponent('collision'); // This defaults to 1x1x1 box-shaped trigger volume
```

To create a 0.5 radius dynamic rigid body sphere:

```javascript
const entity = new Entity();
entity.addComponent('collision', {
    type: 'sphere'
});
entity.addComponent('rigidbody', {
    type: 'dynamic'
});
```

Once the CollisionComponent is added to the entity, you can access it via the
[Entity#collision](https://api.playcanvas.com/engine/classes/Entity.md#collision) property:

```javascript
entity.collision.type = 'cylinder'; // Set the collision volume to a cylinder

console.log(entity.collision.type); // Get the collision volume type and print it
```

Relevant Engine API examples:

- [Compound Collision](https://playcanvas.github.io/#/physics/compound-collision)
- [Falling Shapes](https://playcanvas.github.io/#/physics/falling-shapes)
- [Offset Collision](https://playcanvas.github.io/#/physics/offset-collision)

## Accessors

### angularOffset

```ts
get angularOffset(): Readonly<Quat>
set angularOffset(arg: Readonly<Quat>)
```

Gets the rotational offset of the collision shape from the Entity rotation in local space. Use
the setter to update the collision shape.

### asset

```ts
get asset(): number | Asset<string> | null
set asset(arg: number | Asset<string> | null)
```

Gets the asset or asset id for the model of the mesh collision volume.

### axis

```ts
get axis(): number
set axis(arg: number)
```

Gets the local space axis with which the capsule, cylinder or cone-shaped collision volume's
length is aligned.

### checkVertexDuplicates

```ts
get checkVertexDuplicates(): boolean
set checkVertexDuplicates(arg: boolean)
```

Gets whether checking for duplicate vertices should be enabled when creating collision meshes.

### convexHull

```ts
get convexHull(): boolean
set convexHull(arg: boolean)
```

Gets whether the collision mesh should be treated as a convex hull.

### halfExtents

```ts
get halfExtents(): Readonly<Vec3>
set halfExtents(arg: Readonly<Vec3>)
```

Gets the half-extents of the box-shaped collision volume in the x, y and z axes. Use the
setter to update the collision shape.

### height

```ts
get height(): number
set height(arg: number)
```

Gets the total height of the capsule, cylinder or cone-shaped collision volume from tip to
tip.

### linearOffset

```ts
get linearOffset(): Readonly<Vec3>
set linearOffset(arg: Readonly<Vec3>)
```

Gets the positional offset of the collision shape from the Entity position along the local
axes. Use the setter to update the collision shape.

### model

```ts
get model(): Model | null
set model(arg: Model | null)
```

Gets the model that is added to the scene graph for the mesh collision volume.

### radius

```ts
get radius(): number
set radius(arg: number)
```

Gets the radius of the sphere, capsule, cylinder or cone-shaped collision volumes.

### renderAsset

```ts
get renderAsset(): number | Asset<string> | null
set renderAsset(arg: number | Asset<string> | null)
```

Gets the render asset id of the mesh collision volume.

### type

```ts
get type(): "mesh" | "box" | "capsule" | "compound" | "cone" | "cylinder" | "sphere"
set type(arg: "mesh" | "box" | "capsule" | "compound" | "cone" | "cylinder" | "sphere")
```

Gets the type of the collision volume.

## Methods

### getShapePosition

```ts
getShapePosition(): Vec3
```

Returns the world position for the collision shape, taking into account of any offsets.

**Returns** [`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md): The world position for the collision shape.

### getShapeRotation

```ts
getShapeRotation(): Quat
```

Returns the world rotation for the collision shape, taking into account of any offsets.

**Returns** [`Quat`](https://api.playcanvas.com/engine/classes/Quat.md): The world rotation for the collision.

## Events

### EVENT_COLLISIONEND

```ts
static EVENT_COLLISIONEND: string = 'collisionend'
```

Fired when two rigid bodies stop touching. The handler is passed an [Entity](https://api.playcanvas.com/engine/classes/Entity.md) that
represents the other rigid body involved in the collision.

**Example**

```ts
entity.collision.on('collisionend', (other) => {
    console.log(`${entity.name} stopped touching ${other.name}`);
});
```

### EVENT_COLLISIONSTART

```ts
static EVENT_COLLISIONSTART: string = 'collisionstart'
```

Fired when two rigid bodies start touching. The handler is passed the [ContactResult](https://api.playcanvas.com/engine/classes/ContactResult.md)
object which contains details of the contact between the two rigid bodies.

**Example**

```ts
entity.collision.on('collisionstart', (result) => {
   console.log(`${entity.name} started touching ${result.other.name}`);
});
```

### EVENT_CONTACT

```ts
static EVENT_CONTACT: string = 'contact'
```

Fired when a contact occurs between two rigid bodies. The handler is passed a
[ContactResult](https://api.playcanvas.com/engine/classes/ContactResult.md) object which contains details of the contact between the two rigid
bodies.

**Example**

```ts
entity.collision.on('contact', (result) => {
   console.log(`Contact between ${entity.name} and ${result.other.name}`);
});
```

### EVENT_TRIGGERENTER

```ts
static EVENT_TRIGGERENTER: string = 'triggerenter'
```

Fired when a rigid body enters a trigger volume. The handler is passed an [Entity](https://api.playcanvas.com/engine/classes/Entity.md)
representing the rigid body that entered this collision volume.

**Example**

```ts
entity.collision.on('triggerenter', (other) => {
    console.log(`${other.name} entered trigger volume ${entity.name}`);
});
```

### EVENT_TRIGGERLEAVE

```ts
static EVENT_TRIGGERLEAVE: string = 'triggerleave'
```

Fired when a rigid body exits a trigger volume. The handler is passed an [Entity](https://api.playcanvas.com/engine/classes/Entity.md)
representing the rigid body that exited this collision volume.

**Example**

```ts
entity.collision.on('triggerleave', (other) => {
    console.log(`${other.name} exited trigger volume ${entity.name}`);
});
```

## Inherited from [Component](https://api.playcanvas.com/engine/classes/Component.md)

- `entity: Entity`
- `system: ComponentSystem`
- `get enabled(): boolean` · `set enabled(value: boolean)`
- `fire(name: string, arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any, arg6?: any, arg7?: any, arg8?: any): EventHandler`
- `hasEvent(name: string): boolean`
- `off(name?: string, callback?: HandleEventCallback, scope?: any): EventHandler`
- `on(name: string, callback: HandleEventCallback, scope?: any): EventHandle`
- `once(name: string, callback: HandleEventCallback, scope?: any): EventHandle`
