rigid_body_creation_and_insertion
A rigid-body is created by a World.createRigidBody
method. The initial state of
the rigid-body to create is described by an instance of the RigidBodyDesc
class.
Each rigid-body create by the physics world is given an integer identifier rigidBody.handle
. This identifier
is guaranteed to the different from any identifier of rigid-bodies still existing (or that existed) in the physics
world.
The following example shows several setters that can be called to customize the rigid-body being built. The input values are just random so using this example as-is will not lead to a useful result.
- Example 2D
- Example 3D
// The world that will contain our rigid-bodies.
let world = new RAPIER.World({ x: 0.0, y: -9.81 });
// Builder for a fixed rigid-body.
let example1 = RAPIER.RigidBodyDesc.fixed();
// Builder for a dynamic rigid-body.
let example2 = RAPIER.RigidBodyDesc.dynamic();
// Builder for a kinematic rigid-body controlled at the velocity level.
let example3 = RAPIER.RigidBodyDesc.kinematicVelocityBased();
// Builder for a kinematic rigid-body controlled at the position level.
let example4 = RAPIER.RigidBodyDesc.kinematicPositionBased();
// Builder for a body with a status specified by an enum.
let rigidBodyDesc = new RAPIER.RigidBodyDesc(RAPIER.RigidBodyType.Dynamic)
// The rigid body translation.
// Default: zero vector.
.setTranslation(0.0, 5.0)
// The rigid body rotation.
// Default: no rotation.
.setRotation(5.0)
// The linear velocity of this body.
// Default: zero velocity.
.setLinvel(1.0, 2.0)
// The angular velocity of this body.
// Default: zero velocity.
.setAngvel(2.0)
// The scaling factor applied to the gravity affecting the rigid-body.
// Default: 1.0
.setGravityScale(0.5)
// Whether or not this body can sleep.
// Default: true
.setCanSleep(true)
// Whether or not CCD is enabled for this rigid-body.
// Default: false
.setCcdEnabled(false);
// All done, actually build the rigid-body.
let rigidBody = world.createRigidBody(rigidBodyDesc);
// The integer handle of the rigid-body can be read from the `handle` field.
let rigidBodyHandle = rigidBody.handle;
// The world that will contain our rigid-bodies.
let world = new RAPIER.World({ x: 0.0, y: -9.81, z: 0.0 });
// Builder for a fixed rigid-body.
let example1 = RAPIER.RigidBodyDesc.fixed();
// Builder for a dynamic rigid-body.
let example2 = RAPIER.RigidBodyDesc.dynamic();
// Builder for a kinematic rigid-body controlled at the velocity level.
let example3 = RAPIER.RigidBodyDesc.kinematicVelocityBased();
// Builder for a kinematic rigid-body controlled at the position level.
let example4 = RAPIER.RigidBodyDesc.kinematicPositionBased();
// Builder for a body with a status specified by an enum.
let rigidBodyDesc = new RAPIER.RigidBodyDesc(RAPIER.RigidBodyType.Dynamic)
// The rigid body translation.
// Default: zero vector.
.setTranslation(0.0, 5.0, 1.0)
// The rigid body rotation, given as a quaternion.
// Default: no rotation.
.setRotation({ w: 1.0, x: 0.0, y: 0.0, z: 0.0 })
// The linear velocity of this body.
// Default: zero velocity.
.setLinvel(1.0, 3.0, 4.0)
// The angular velocity of this body.
// Default: zero velocity.
.setAngvel({ x: 3.0, y: 0.0, z: 1.0 })
// The scaling factor applied to the gravity affecting the rigid-body.
// Default: 1.0
.setGravityScale(0.5)
// Whether or not this body can sleep.
// Default: true
.setCanSleep(true)
// Whether or not CCD is enabled for this rigid-body.
// Default: false
.setCcdEnabled(false);
// All done, actually build the rigid-body.
let rigidBody = world.createRigidBody(rigidBodyDesc);
// The integer handle of the rigid-body can be read from the `handle` field.
let rigidBodyHandle = rigidBody.handle;
Typically, the inertia and center of mass are automatically set to the inertia and center of mass resulting from the shapes of the colliders attached to the rigid-body. But they can also be set manually.