collider_position
The position of a collider represents its location (translation) in 2D or 3D world-space as well as its orientation (rotation).
Please read carefully the paragraph after the next example. It explains how the collider position (and the action of setting this position) behaves differently when it is attached to a rigid-body.
It is possible to set this position when the collider is created or after its creation:
- Example 2D
- Example 3D
/* Set the collider position when the collider is created. */
let colliderDesc = RAPIER.ColliderDesc.ball(0.5)
.setTranslation(1.0, 2.0)
.setRotation(0.4);
let collider = world.createCollider(colliderDesc);
/* Set the collider position after the collider creation. */
collider.setTranslation({ x: 1.0, y: 2.0 });
collider.setRotation(0.4);
/* Set the collider position when the collider is created. */
let colliderDesc = RAPIER.ColliderDesc.ball(0.5)
.setTranslation(1.0, 2.0, 3.0)
.setRotation({ w: 1.0, x: 0.0, y: 0.0, z: 0.0 });
let collider = world.createCollider(colliderDesc);
/* Set the collider position after the collider creation. */
collider.setTranslation({ x: 1.0, y: 2.0, z: 3.0 });
collider.setRotation({ w: 1.0, x: 0.0, y: 0.0, z: 0.0 });
If a collider is attached to a rigid-body, its position is automatically updated by the physics pipeline when a rigid-body is moved by the physics pipeline. If a change to the rigid-body position is made by the user then the collider position will be updated during the next timestep.
Therefore, directly setting the position of a collider attached to a rigid-body will have no lasting effect. Instead, it is possible to set the position of the collider relative to the rigid-body it is attached to:
- Example 2D
- Example 3D
let rigidBodyDesc = RAPIER.RigidBodyDesc.dynamic();
let rigidBody = world.createRigidBody(rigidBodyDesc);
let colliderDesc = RAPIER.ColliderDesc.ball(0.5)
.setTranslation(1.0, 2.0);
// Attach the collider to the rigid-body. The collider's position wrt. the rigid-body
// is automatically set to the collider current position when this method is called.
let collider = world.createCollider(colliderDesc, rigidBody);
/* Set the collider position wrt. its parent after the collider creation. */
collider.setTranslationWrtParent({ x: 1.0, y: 2.0 });
let rigidBodyDesc = RAPIER.RigidBodyDesc.dynamic();
let rigidBody = world.createRigidBody(rigidBodyDesc);
let colliderDesc = RAPIER.ColliderDesc.ball(0.5)
.setTranslation(1.0, 2.0, 3.0);
// Attach the collider to the rigid-body. The collider's position wrt. the rigid-body
// is automatically set to the collider current position when this method is called.
let collider = world.createCollider(colliderDesc, rigidBody);
/* Set the collider position wrt. its parent after the collider creation. */
collider.setTranslationWrtParent({ x: 1.0, y: 2.0, z: 3.0 });