RigidBodyComponent

RigidBodyComponent

Adds rigid body dynamics the entity. To be used with the PhysicsSystem. If the entity or its children have ColliderComponents, they are added as collision shapes to the rigid body.

Constructor

new RigidBodyComponent(settingsopt)

Parameters:
Name Type Attributes Description
settings Object <optional>
Properties
Name Type Attributes Default Description
mass number <optional>
1
isKinematic boolean <optional>
false
velocity Vector3 <optional>
angularVelocity Vector3 <optional>
linearDamping number <optional>
0.01
angularDamping number <optional>
0.05

Members

(static) FREEZE_ALL :number

Freeze rotation and motion along all axes.

(static) FREEZE_NONE :number

No constraints.

(static) FREEZE_POSITION :number

Freeze motion along all axes.

(static) FREEZE_POSITION_X :number

Freeze motion along the X-axis.

(static) FREEZE_POSITION_Y :number

Freeze motion along the Y-axis.

(static) FREEZE_POSITION_Z :number

Freeze motion along the Z-axis.

(static) FREEZE_ROTATION :number

Freeze rotation along all axes.

(static) FREEZE_ROTATION_X :number

Freeze rotation along the X-axis.

(static) FREEZE_ROTATION_Y :number

Freeze rotation along the Y-axis.

(static) FREEZE_ROTATION_Z :number

Freeze rotation along the Z-axis.

(static) INTERPOLATE

Transform is smoothed based on the Transform of the previous frame.

(static) NONE

No rigid body smoothing.

(static) numCylinderSegments :number

Cannon.js uses ConvexPolyhedron shapes for collision checking sometimes (for example, for cylinders). Therefore it needs a number of segments to use.

_colliderEntities :Array

All the attached colliders.

_constraints :number

Constraint the movement of the rigid body. Set it to RigidBodyComponent.FREEZE_NONE, RigidBodyComponent.FREEZE_POSITION_X, RigidBodyComponent.FREEZE_POSITION_Y, RigidBodyComponent.FREEZE_POSITION_Z, RigidBodyComponent.FREEZE_ROTATION_X, RigidBodyComponent.FREEZE_ROTATION_Y, RigidBodyComponent.FREEZE_ROTATION_Z, RigidBodyComponent.FREEZE_POSITION, RigidBodyComponent.FREEZE_ROTATION or RigidBodyComponent.FREEZE_ALL.

angularDamping :number

cannonBody :CANNON.Body

constraints :number

Constraint the movement of the rigid body. Set it to RigidBodyComponent.FREEZE_NONE, RigidBodyComponent.FREEZE_POSITION_X, RigidBodyComponent.FREEZE_POSITION_Y, RigidBodyComponent.FREEZE_POSITION_Z, RigidBodyComponent.FREEZE_ROTATION_X, RigidBodyComponent.FREEZE_ROTATION_Y, RigidBodyComponent.FREEZE_ROTATION_Z, RigidBodyComponent.FREEZE_POSITION, RigidBodyComponent.FREEZE_ROTATION or RigidBodyComponent.FREEZE_ALL.

interpolation :number

How smoothing of the rigid body movement should be done. Set it to RigidBodyComponent.NONE or RigidBodyComponent.INTERPOLATE.

isKinematic :number

linearDamping :number

mass :number

sleepingThreshold :number

sleepingTimeLimit :number

Methods

applyForce(force, relativePointopt)

Apply a force to a point on the body in world space.

Parameters:
Name Type Attributes Description
force Vector3

The force vector, oriented in world space.

relativePoint Vector3 <optional>

Where to apply the force. This point is relative to the Body, oriented in World space. Defaults to the zero vector (the center of mass).

applyForceLocal(force, relativePointopt)

Apply a local force to the body in local body space.

Parameters:
Name Type Attributes Description
force Vector3

The force vector, oriented in local space.

relativePoint Vector3 <optional>

Where to apply the force. This point is relative to the Body, oriented in local space. Defaults to the zero vector (the center of mass).

Example
var localThrusterForce = new Vector3(0, 0, 1); // Thrust in forward direction of ship
var localPosition = new Vector3(0, 0, -1); // Applies to the back part of the ship
shapeShip.rigidBodyComponent.applyForce(localThrusterForce, localPosition);

applyForceWorld(force, worldPoint)

Apply a world-oriented force to a world point.

Parameters:
Name Type Description
force Vector3

The force vector, oriented in world space.

worldPoint Vector3

Where to apply the force, in world space.

Example
var direction = new Vector3();
direction
    .copy(entity.transformComponent.worldTransform.translation)
    .sub(bombEntity.transformComponent.worldTransform.translation)
    .normalize()
    .scale(100);
entity.applyForceWorld(direction, entity.transformComponent.worldTransform.translation);

applyImpulse(impulse, relativePoint)

Apply an impulse to the body.

Parameters:
Name Type Description
impulse Vector3

The impulse vector, oriented in world space.

relativePoint Vector3

Where the impulse should be applied

applyImpulseLocal(impulse, relativePoint)

Apply an impulse to the center of mass of the body.

Parameters:
Name Type Description
impulse Vector3

The force vector, oriented in local space.

relativePoint Vector3

applyTorque(torque)

Apply a torque to a point on the body in world space.

Parameters:
Name Type Description
torque Vector3

The torque vector, oriented in world space.

applyTorqueLocal(torque)

Apply a torque to the body in local body space.

Parameters:
Name Type Description
torque Vector3

The torque vector, oriented in local body space.

clone()

Creates a new instance indentical to this component.

destroy()

Removes the body from the physics engine

destroyJoint()

getAngularVelocity(targetVector)

Parameters:
Name Type Description
targetVector Vector3

getInterpolatedPosition(targetVector)

Get the interpolated position from the rigid body. Use this for rendering. The resulting vector is a linear interpolation between the current and previous physics position, that matches the current rendering frame.

Parameters:
Name Type Description
targetVector Vector3

getInterpolatedQuaternion(targetQuat)

Get the interpolated quaternion from the rigid body. Use this for rendering. The resulting quaternion is a spherical interpolation between the current and previous physics position, that matches the current rendering frame.

Parameters:
Name Type Description
targetQuat Quaternion

getPosition(targetVector)

Parameters:
Name Type Description
targetVector Vector3

getQuaternion(targetQuat)

Parameters:
Name Type Description
targetQuat Quaternion

getVelocity(targetVector)

Parameters:
Name Type Description
targetVector Vector3

initialize()

Initialize the Cannon.js body available in the .cannonBody property. This is useful if the intention is to work with the CANNON.Body instance directly after the component is created.

initializeJoint()

setAngularVelocity(angularVelocity)

Parameters:
Name Type Description
angularVelocity Vector3

setPosition(position)

Parameters:
Name Type Description
position Vector3

setQuaternion(quaternion)

Parameters:
Name Type Description
quaternion Quaternion

setTransformToEntity()

Sync the transform from the physics body to the entity

setVelocity(velocity)

Parameters:
Name Type Description
velocity Vector3