Skip to content

Body Functions

Angular Three Cannon provides various body functions to create different types of physics bodies. These functions are used to add physical properties to your 3D objects.

Available Body Functions

All body functions are available from angular-three-cannon/body:

import {
injectBox,
injectSphere,
injectPlane,
injectCylinder,
injectHeightfield,
injectParticle,
injectConvexPolyhedron,
injectTrimesh,
injectCompound
} from 'angular-three-cannon/body';

Usage

The general pattern for using these functions is:

import { Component, ElementRef, viewChild } from '@angular/core';
import { injectBox } from 'angular-three-cannon/body';
import { NgtMesh } from 'angular-three';
@Component({
selector: 'app-physics-box',
standalone: true,
template: `
<ngt-mesh #mesh>
<ngt-box-geometry />
<ngt-mesh-standard-material />
</ngt-mesh>
`,
})
export class PhysicsBox {
mesh = viewChild.required<ElementRef<THREE.Mesh>>('mesh');
boxBody = injectBox(
() => ({
mass: 1,
position: [0, 5, 0],
args: [1, 1, 1],
}),
this.mesh
);
}

Body Functions

FunctionDescriptionSpecific Options
injectBoxCreates a box-shaped bodyargs: [width, height, depth]
injectSphereCreates a spherical bodyargs: [radius]
injectPlaneCreates an infinite planeNo specific options
injectCylinderCreates a cylindrical bodyargs: [radiusTop, radiusBottom, height, numSegments]
injectHeightfieldCreates a heightfield bodyargs: [data, options]
injectParticleCreates a particle (point mass)No specific options
injectConvexPolyhedronCreates a convex polyhedronargs: [vertices, faces]
injectTrimeshCreates a triangular mesh bodyargs: [vertices, indices]
injectCompoundCreates a compound bodyshapes: Array<{ type, args, position?, rotation? }>

Common Options

All body functions accept a set of common options:

OptionTypeDescription
massnumberThe mass of the body (0 for static bodies)
position[x: number, y: number, z: number]Initial position of the body
rotation[x: number, y: number, z: number]Initial rotation of the body (in radians)
velocity[x: number, y: number, z: number]Initial velocity of the body
angularVelocity[x: number, y: number, z: number]Initial angular velocity of the body
linearDampingnumberLinear damping of the body (0 = no damping, 1 = full damping)
angularDampingnumberAngular damping of the body
fixedRotationbooleanIf true, body will not rotate
collisionFilterGroupnumberThe collision group the body belongs to
collisionFilterMasknumberWhich groups this body can collide with
triggerbooleanIf true, body acts as a trigger (no collision response)
onCollidefunctionCallback function when collision occurs
onCollideBeginfunctionCallback function when collision begins
onCollideEndfunctionCallback function when collision ends

Advanced Usage

You can dynamically update body properties using the returned API:

import { Component, ElementRef, viewChild, signal } from '@angular/core';
import { injectBox } from 'angular-three-cannon/body';
import { NgtMesh } from 'angular-three';
@Component({
selector: 'app-physics-box',
standalone: true,
template: `
<ngt-mesh #mesh>
<ngt-box-geometry />
<ngt-mesh-standard-material />
</ngt-mesh>
<button (click)="jump()">Jump</button>
`,
})
export class PhysicsBox {
mesh = viewChild.required<ElementRef<THREE.Mesh>>('mesh');
boxBody = injectBox(
() => ({
mass: 1,
position: [0, 5, 0],
args: [1, 1, 1],
}),
this.mesh
);
jump() {
const api = this.boxBody();
if (api) {
api.applyImpulse([0, 5, 0], [0, 0, 0]);
}
}
}

This example shows how to apply an impulse to make the box “jump” when a button is clicked.