Class Body

Base class for all body types.

Example

const shape = new CANNON.Sphere(1)
const body = new CANNON.Body({
mass: 1,
shape,
})
world.addBody(body)

Hierarchy (view full)

Constructors

  • Parameters

    • Optional options: {
          allowSleep?: boolean;
          angularDamping?: number;
          angularFactor?: Vec3;
          angularVelocity?: Vec3;
          collisionFilterGroup?: number;
          collisionFilterMask?: number;
          collisionResponse?: boolean;
          fixedRotation?: boolean;
          isTrigger?: boolean;
          linearDamping?: number;
          linearFactor?: Vec3;
          mass?: number;
          material?: Material;
          position?: Vec3;
          quaternion?: Quaternion;
          shape?: Shape;
          sleepSpeedLimit?: number;
          sleepTimeLimit?: number;
          type?: BodyType;
          velocity?: Vec3;
      }
      • Optional allowSleep?: boolean

        If true, the body will automatically fall to sleep.

        Default

        true
        
      • Optional angularDamping?: number

        How much to damp the body angular velocity each step. It can go from 0 to 1.

        Default

        0.01
        
      • Optional angularFactor?: Vec3

        Use this property to limit the rotational motion along any world axis. (1,1,1) will allow rotation along all axes while (0,0,0) allows none.

      • Optional angularVelocity?: Vec3

        Angular velocity of the body, in world space. Think of the angular velocity as a vector, which the body rotates around. The length of this vector determines how fast (in radians per second) the body rotates.

      • Optional collisionFilterGroup?: number

        The collision group the body belongs to.

        Default

        1
        
      • Optional collisionFilterMask?: number

        The collision group the body can collide with.

        Default

        -1
        
      • Optional collisionResponse?: boolean

        Whether to produce contact forces when in contact with other bodies. Note that contacts will be generated, but they will be disabled - i.e. "collide" events will be raised, but forces will not be altered.

      • Optional fixedRotation?: boolean

        Set to true if you don't want the body to rotate. Make sure to run .updateMassProperties() if you change this after the body creation.

        Default

        false
        
      • Optional isTrigger?: boolean

        When true the body behaves like a trigger. It does not collide with other bodies but collision events are still triggered.

        Default

        false
        
      • Optional linearDamping?: number

        How much to damp the body velocity each step. It can go from 0 to 1.

        Default

        0.01
        
      • Optional linearFactor?: Vec3

        Use this property to limit the motion along any world axis. (1,1,1) will allow motion along all axes while (0,0,0) allows none.

      • Optional mass?: number

        The mass of the body.

        Default

        0
        
      • Optional material?: Material

        The physics material of the body. It defines the body interaction with other bodies.

      • Optional position?: Vec3

        World space position of the body.

      • Optional quaternion?: Quaternion

        World space orientation of the body.

      • Optional shape?: Shape

        Add a Shape to the body.

      • Optional sleepSpeedLimit?: number

        If the speed (the norm of the velocity) is smaller than this value, the body is considered sleepy.

        Default

        0.1
        
      • Optional sleepTimeLimit?: number

        If the body has been sleepy for this sleepTimeLimit seconds, it is considered sleeping.

        Default

        1
        
      • Optional type?: BodyType

        One of: Body.DYNAMIC, Body.STATIC and Body.KINEMATIC.

      • Optional velocity?: Vec3

        World space velocity of the body.

    Returns Body

Properties

aabb: AABB

World space bounding box of the body and its shapes.

aabbNeedsUpdate: boolean

Indicates if the AABB needs to be updated before use.

allowSleep: boolean

If true, the body will automatically fall to sleep.

Default

true
angularDamping: number

How much to damp the body angular velocity each step. It can go from 0 to 1.

Default

0.01
angularFactor: Vec3

Use this property to limit the rotational motion along any world axis. (1,1,1) will allow rotation along all axes while (0,0,0) allows none.

angularVelocity: Vec3

Angular velocity of the body, in world space. Think of the angular velocity as a vector, which the body rotates around. The length of this vector determines how fast (in radians per second) the body rotates.

boundingRadius: number

Total bounding radius of the Body including its shapes, relative to body.position.

collisionFilterGroup: number

The collision group the body belongs to.

Default

1
collisionFilterMask: number

The collision group the body can collide with.

Default

-1
collisionResponse: boolean

Whether to produce contact forces when in contact with other bodies. Note that contacts will be generated, but they will be disabled - i.e. "collide" events will be raised, but forces will not be altered.

entity?: Entity
fixedRotation: boolean

Set to true if you don't want the body to rotate. Make sure to run .updateMassProperties() if you change this after the body creation.

Default

false
force: Vec3

Linear force on the body in world space.

id: number

Identifier of the body.

index: number

Position of body in World.bodies. Updated by World and used in ArrayCollisionMatrix.

inertia: Vec3

The inertia of the body.

initAngularVelocity: Vec3

Initial angular velocity of the body.

initPosition: Vec3

Initial position of the body.

initQuaternion: Quaternion

Initial quaternion of the body.

initVelocity: Vec3

Initial velocity of the body.

interpolatedPosition: Vec3

Interpolated position of the body.

interpolatedQuaternion: Quaternion

Interpolated orientation of the body.

invInertia: Vec3
invInertiaSolve: Vec3
invInertiaWorld: Mat3
invInertiaWorldSolve: Mat3
invMass: number
invMassSolve: number
isTrigger: boolean

When true the body behaves like a trigger. It does not collide with other bodies but collision events are still triggered.

Default

false
linearDamping: number

How much to damp the body velocity each step. It can go from 0 to 1.

Default

0.01
linearFactor: Vec3

Use this property to limit the motion along any world axis. (1,1,1) will allow motion along all axes while (0,0,0) allows none.

mass: number

The mass of the body.

Default

0
material: Material

The physics material of the body. It defines the body interaction with other bodies.

object3d?: Object3D<Object3DEventMap>
position: Vec3

World space position of the body.

previousPosition: Vec3
previousQuaternion: Quaternion
quaternion: Quaternion

World space orientation of the body.

shapeOffsets: Vec3[]

Position of each Shape in the body, given in local Body space.

shapeOrientations: Quaternion[]

Orientation of each Shape, given in local Body space.

shapes: Shape[]

List of Shapes that have been added to the body.

sleepSpeedLimit: number

If the speed (the norm of the velocity) is smaller than this value, the body is considered sleepy.

Default

0.1
sleepState: BodySleepState

Current sleep state.

sleepTimeLimit: number

If the body has been sleepy for this sleepTimeLimit seconds, it is considered sleeping.

Default

1
timeLastSleepy: number
torque: Vec3

World space rotational force on the body, around center of mass.

type: BodyType

One of: Body.DYNAMIC, Body.STATIC and Body.KINEMATIC.

velocity: Vec3

World space velocity of the body.

vlambda: Vec3
wakeUpAfterNarrowphase: boolean
wlambda: Vec3
world: World

Reference to the world the body is living in.

AWAKE: 0

AWAKE

DYNAMIC: 1

A dynamic body is fully simulated. Can be moved manually by the user, but normally they move according to forces. A dynamic body can collide with all body types. A dynamic body always has finite, non-zero mass.

KINEMATIC: 4

A kinematic body moves under simulation according to its velocity. They do not respond to forces. They can be moved manually, but normally a kinematic body is moved by setting its velocity. A kinematic body behaves as if it has infinite mass. Kinematic bodies do not collide with other static or kinematic bodies.

SLEEPING: 2

SLEEPING

SLEEPY: 1

SLEEPY

STATIC: 2

A static body does not move during simulation and behaves as if it has infinite mass. Static bodies can be moved manually by setting the position of the body. The velocity of a static body is always zero. Static bodies do not collide with other static or kinematic bodies.

idCounter: number

Methods

  • Add a shape to the body with a local offset and orientation.

    Parameters

    Returns Body

    The body object, for chainability.

  • Apply force to a point of the body. This could for example be a point on the Body surface. Applying force this way will add to Body.force and Body.torque.

    Parameters

    • force: Vec3

      The amount of force to add.

    • Optional relativePoint: Vec3

      A point relative to the center of mass to apply the force on.

    Returns void

  • Apply impulse to a point of the body. This could for example be a point on the Body surface. An impulse is a force added to a body during a short period of time (impulse = force * time). Impulses will be added to Body.velocity and Body.angularVelocity.

    Parameters

    • impulse: Vec3

      The amount of impulse to add.

    • Optional relativePoint: Vec3

      A point relative to the center of mass to apply the force on.

    Returns void

  • Apply force to a local point in the body.

    Parameters

    • localForce: Vec3
    • Optional localPoint: Vec3

      A local point in the body to apply the force on.

    Returns void

  • Apply locally-defined impulse to a local point in the body.

    Parameters

    • localImpulse: Vec3
    • Optional localPoint: Vec3

      A local point in the body to apply the force on.

    Returns void

  • Apply torque to the body.

    Parameters

    • torque: Vec3

      The amount of torque to add.

    Returns void

  • Get world velocity of a point in the body.

    Parameters

    Returns Vec3

    The result vector.

  • Check if any event listener of the given type is added

    Parameters

    • type: string

    Returns boolean

  • Check if an event listener is added

    Parameters

    • type: string
    • listener: Function

    Returns boolean

  • Move the body forward in time.

    Parameters

    • dt: number

      Time step

    • quatNormalize: boolean

      Set to true to normalize the body quaternion

    • quatNormalizeFast: boolean

      If the quaternion should be normalized using "fast" quaternion normalization

    Returns void

  • Convert a world point to local body frame.

    Parameters

    Returns Vec3

  • Convert a local body point to world frame.

    Parameters

    Returns Vec3

  • Remove a shape from the body.

    Parameters

    Returns Body

    The body object, for chainability.

  • Force body sleep

    Returns void

  • Called every timestep to update internal sleep timer and change sleep state if needed.

    Parameters

    • time: number

      The world time in seconds

    Returns void

  • Updates the .aabb

    Returns void

  • Update the bounding radius of the body. Should be done if any of the shapes are changed.

    Returns void

  • Update .inertiaWorld and .invInertiaWorld

    Parameters

    • Optional force: boolean

    Returns void

  • Should be called whenever you change the body shape or mass.

    Returns void

  • If the body is sleeping, it should be immovable / have infinite mass during solve. We solve it by having a separate "solve mass".

    Returns void

  • Convert a world vector to local body frame.

    Parameters

    Returns Vec3

  • Convert a local body point to world frame.

    Parameters

    Returns Vec3

  • Wake the body up.

    Returns void

Events

COLLIDE_EVENT_NAME: string

Dispatched after two bodies collide. This event is dispatched on each of the two bodies involved in the collision. collide

Param: body

The body that was involved in the collision.

Param: contact

The details of the collision.

sleepEvent: {
    type: string;
}

Dispatched after a body has fallen asleep. sleep

Type declaration

  • type: string
sleepyEvent: {
    type: string;
}

Dispatched after a body has gone in to the sleepy state. sleepy

Type declaration

  • type: string
wakeupEvent: {
    type: string;
}

Dispatched after a sleeping body has woken up. wakeup

Type declaration

  • type: string