Class: RigidBodyComponent

pc.RigidBodyComponent

The rigidbody component, when combined with a pc.CollisionComponent, allows your entities to be simulated using realistic physics. A rigidbody component will fall under gravity and collide with other rigid bodies. Using scripts, you can apply forces and impulses to rigid bodies.

Constructor

new RigidBodyComponent(system, entity)

Create a new RigidBodyComponent
Parameters:
Name Type Description
system pc.RigidBodyComponentSystem The ComponentSystem that created this component
entity pc.Entity The entity this component is attached to
Properties:
Name Type Description
mass Number The mass of the body. This is only relevant for pc.BODYTYPE_DYNAMIC bodies, other types have infinite mass. Defaults to 1.
linearVelocity pc.Vec3 Defines the speed of the body in a given direction.
angularVelocity pc.Vec3 Defines the rotational speed of the body around each world axis.
linearDamping Number Controls the rate at which a body loses linear velocity over time. Defaults to 0.
angularDamping Number Controls the rate at which a body loses angular velocity over time. Defaults to 0.
linearFactor pc.Vec3 Scaling factor for linear movement of the body in each axis. Defaults to 1 in all axes.
angularFactor pc.Vec3 Scaling factor for angular movement of the body in each axis. Defaults to 1 in all axes.
friction Number The friction value used when contacts occur between two bodies. A higher value indicates more friction. Should be set in the range 0 to 1. Defaults to 0.5.
restitution Number Influences the amount of energy lost when two rigid bodies collide. The calculation multiplies the restitution values for both colliding bodies. A multiplied value of 0 means that all energy is lost in the collision while a value of 1 means that no energy is lost. Should be set in the range 0 to 1. Defaults to 0.
group Number The collision group this body belongs to. Combine the group and the mask to prevent bodies colliding with each other. Defaults to 1.
mask Number The collision mask sets which groups this body collides with. It is a bitfield of 16 bits, the first 8 bits are reserved for engine use. Defaults to 65535.
type String The rigid body type determines how the body is simulated. Can be:
  • pc.BODYTYPE_STATIC: infinite mass and cannot move.
  • pc.BODYTYPE_DYNAMIC: simulated according to applied forces.
  • pc.BODYTYPE_KINEMATIC: infinite mass and does not respond to forces but can still be moved by setting their velocity or position.
Defaults to pc.BODYTYPE_STATIC.
Source:

Extends

Methods

(private) _updateKinematic(dt)

Kinematic objects maintain their own linear and angular velocities. This method updates their transform based on their current velocity. It is called in every frame in the main physics update loop, after the simulation is stepped.
Parameters:
Name Type Description
dt Number Delta time for the current frame.
Source:

activate()

Forcibly activate the rigid body simulation
Source:

applyForce(x, yopt, zopt, pxopt, pyopt, pzopt)

Apply an force to the body at a point. By default, the force is applied at the origin of the body. However, the force can be applied at an offset this point by specifying a world space vector from the body's origin to the point of application. This function has two valid signatures. You can either specify the force (and optional relative point) via 3D-vector or numbers.
Parameters:
Name Type Attributes Description
x pc.Vec3 | Number A 3-dimensional vector representing the force in world-space or the x-component of the force in world-space.
y pc.Vec3 | Number <optional>
An optional 3-dimensional vector representing the relative point at which to apply the impulse in world-space or the y-component of the force in world-space.
z Number <optional>
The z-component of the force in world-space.
px Number <optional>
The x-component of a world-space offset from the body's position where the force is applied.
py Number <optional>
The y-component of a world-space offset from the body's position where the force is applied.
pz Number <optional>
The z-component of a world-space offset from the body's position where the force is applied.
Source:
Examples
// Apply an approximation of gravity at the body's center
this.entity.rigidbody.applyForce(0, -10, 0);
// Apply an approximation of gravity at 1 unit down the world Z from the center of the body
this.entity.rigidbody.applyForce(0, -10, 0, 0, 0, 1);
// Apply a force at the body's center
// Calculate a force vector pointing in the world space direction of the entity
var force = this.entity.forward.clone().scale(100);

// Apply the force
this.entity.rigidbody.applyForce(force);
// Apply a force at some relative offset from the body's center
// Calculate a force vector pointing in the world space direction of the entity
var force = this.entity.forward.clone().scale(100);

// Calculate the world space relative offset
var relativePos = new pc.Vec3();
var childEntity = this.entity.findByName('Engine');
relativePos.sub2(childEntity.getPosition(), this.entity.getPosition());

// Apply the force
this.entity.rigidbody.applyForce(force, relativePos);

applyImpulse(x, yopt, zopt, pxopt, pyopt, pzopt)

Apply an impulse (instantaneous change of velocity) to the body at a point. This function has two valid signatures. You can either specify the impulse (and optional relative point) via 3D-vector or numbers.
Parameters:
Name Type Attributes Default Description
x pc.Vec3 | Number A 3-dimensional vector representing the impulse in world-space or the x-component of the impulse in world-space.
y pc.Vec3 | Number <optional>
An optional 3-dimensional vector representing the relative point at which to apply the impulse in the local-space of the entity or the y-component of the impulse to apply in world-space.
z Number <optional>
The z-component of the impulse to apply in world-space.
px Number <optional>
0 The x-component of the point at which to apply the impulse in the local-space of the entity.
py Number <optional>
0 The y-component of the point at which to apply the impulse in the local-space of the entity.
pz Number <optional>
0 The z-component of the point at which to apply the impulse in the local-space of the entity.
Source:
Examples
// Apply an impulse along the world-space positive y-axis at the entity's position.
var impulse = new pc.Vec3(0, 10, 0);
entity.rigidbody.applyImpulse(impulse);
// Apply an impulse along the world-space positive y-axis at 1 unit down the positive
// z-axis of the entity's local-space.
var impulse = new pc.Vec3(0, 10, 0);
var relativePoint = new pc.Vec3(0, 0, 1);
entity.rigidbody.applyImpulse(impulse, relativePoint);
// Apply an impulse along the world-space positive y-axis at the entity's position.
entity.rigidbody.applyImpulse(0, 10, 0);
// Apply an impulse along the world-space positive y-axis at 1 unit down the positive
// z-axis of the entity's local-space.
entity.rigidbody.applyImpulse(0, 10, 0, 0, 0, 1);

applyTorque(x, yopt, zopt)

Apply torque (rotational force) to the body. This function has two valid signatures. You can either specify the torque force with a 3D-vector or with 3 numbers.
Parameters:
Name Type Attributes Description
x pc.Vec3 | Number A 3-dimensional vector representing the torque force in world-space or the x-component of the torque force in world-space.
y Number <optional>
The y-component of the torque force in world-space.
z Number <optional>
The z-component of the torque force in world-space.
Source:
Examples
// Apply via vector
var torque = new pc.Vec3(0, 10, 0);
entity.rigidbody.applyTorque(torque);
// Apply via numbers
entity.rigidbody.applyTorque(0, 10, 0);

applyTorqueImpulse(x, yopt, zopt)

Apply a torque impulse (rotational force applied instantaneously) to the body. This function has two valid signatures. You can either specify the torque force with a 3D-vector or with 3 numbers.
Parameters:
Name Type Attributes Description
x pc.Vec3 | Number A 3-dimensional vector representing the torque impulse in world-space or the x-component of the torque impulse in world-space.
y Number <optional>
The y-component of the torque impulse in world-space.
z Number <optional>
The z-component of the torque impulse in world-space.
Source:
Examples
// Apply via vector
var torque = new pc.Vec3(0, 10, 0);
entity.rigidbody.applyTorqueImpulse(torque);
// Apply via numbers
entity.rigidbody.applyTorqueImpulse(0, 10, 0);

(private) createBody()

If the Entity has a Collision shape attached then create a rigid body using this shape. This method destroys the existing body.
Source:

isActive() → {Boolean}

Returns true if the rigid body is currently actively being simulated. i.e. not 'sleeping'
Source:
Returns:
True if the body is active
Type
Boolean

isKinematic() → {Boolean}

Returns true if the rigid body is of type pc.BODYTYPE_KINEMATIC
Source:
Returns:
True if kinematic
Type
Boolean

isStatic() → {Boolean}

Returns true if the rigid body is of type pc.BODYTYPE_STATIC
Source:
Returns:
True if static
Type
Boolean

isStaticOrKinematic() → {Boolean}

Returns true if the rigid body is of type pc.BODYTYPE_STATIC or pc.BODYTYPE_KINEMATIC
Source:
Returns:
True if static or kinematic
Type
Boolean

(private) syncBodyToEntity()

Update the Entity transform from the rigid body. This is called internally after the simulation is stepped, to keep the Entity transform in sync with the rigid body transform.
Source:

(private) syncEntityToBody()

Set the rigid body transform to be the same as the Entity transform. This must be called after any Entity transformation functions (e.g. pc.Entity#setPosition) are called in order to update the rigid body to match the Entity.
Source:

teleport(x, y, zopt, rxopt, ryopt, rzopt)

Teleport an entity to a new world-space position, optionally setting orientation. This function should only be called for rigid bodies that are dynamic. This function has three valid signatures. The first takes a 3-dimensional vector for the position and an optional 3-dimensional vector for Euler rotation. The second takes a 3-dimensional vector for the position and an optional quaternion for rotation. The third takes 3 numbers for the position and an optional 3 numbers for Euler rotation.
Parameters:
Name Type Attributes Description
x pc.Vec3 | Number A 3-dimensional vector holding the new position or the new position x-coordinate.
y pc.Vec3 | pc.Quat | Number A 3-dimensional vector or quaternion holding the new rotation or the new position y-coordinate.
z Number <optional>
The new position z-coordinate.
rx Number <optional>
The new Euler x-angle value.
ry Number <optional>
The new Euler y-angle value.
rz Number <optional>
The new Euler z-angle value.
Source:
Examples
// Teleport the entity to the origin
entity.rigidbody.teleport(pc.Vec3.ZERO);
// Teleport the entity to the origin
entity.rigidbody.teleport(0, 0, 0);
// Teleport the entity to world-space coordinate [1, 2, 3] and reset orientation
var position = new pc.Vec3(1, 2, 3);
entity.rigidbody.teleport(position, pc.Vec3.ZERO);
// Teleport the entity to world-space coordinate [1, 2, 3] and reset orientation
entity.rigidbody.teleport(1, 2, 3, 0, 0, 0);