collider_position
The position of a collider represents its location (translation) in 2D or 3D world-space as well as its orientation (rotation).
Its translational part is represented as a vector (R3Vector) and its rotational part (R3Rotation) as an unit quaternion (in 3D) or an angle (in 2D). Both are combined into a pose (the R3Pose type).
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. */
R2ColliderDesc collider = r2BallColliderDesc(0.5);
collider.position.translation = r2Vector(1.0, 2.0);
collider.position.rotation = r2Rotation(0.4);
// Set both translation and rotation at once.
collider.position = r2Pose(r2Vector(1.0, 2.0), r2Rotation(0.4));
/* Set the collider position after the collider creation. */
r2Collider_SetTranslation(collider_handle, r2Vector(1.0, 2.0));
r2Collider_SetRotation(collider_handle, r2Rotation(0.4));
// Set both the translation and rotation at once.
r2Collider_SetPosition(collider_handle, r2Pose(r2Vector(1.0, 2.0), r2Rotation(0.4)));
R2Vector translation = r2Collider_Translation(collider_handle);
assert(translation.x == 1.0 && translation.y == 2.0);
assert(fabs(r2Collider_Rotation(collider_handle).angle - 0.4) < 1.0e-6);
/* Set the collider position when the collider is created. */
R3ColliderDesc collider = r3BallColliderDesc(0.5);
collider.position.translation = r3Vector(1.0, 2.0, 3.0);
collider.position.rotation = r3RotationFromAxisAngle(r3Vector(0.0, 0.0, 1.0), 0.4);
// Set both translation and rotation at once.
collider.position =
r3Pose(r3Vector(1.0, 2.0, 3.0), r3RotationFromAxisAngle(r3Vector(0.0, 0.0, 1.0), 0.4));
/* Set the collider position after the collider creation. */
R3Rotation rotation = r3RotationFromAxisAngle(r3Vector(0.0, 0.0, 1.0), 0.4);
r3Collider_SetTranslation(collider_handle, r3Vector(1.0, 2.0, 3.0));
r3Collider_SetRotation(collider_handle, rotation);
// Set both the translation and rotation at once.
r3Collider_SetPosition(collider_handle, r3Pose(r3Vector(1.0, 2.0, 3.0), rotation));
R3Vector translation = r3Collider_Translation(collider_handle);
assert(translation.x == 1.0 && translation.y == 2.0 && translation.z == 3.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 (with r3Collider_SetPosition, r3Collider_SetTranslation, or r3Collider_SetRotation) 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: this is the position field of its description, which can be modified after its creation with r3Collider_SetPositionWrtParent:
- Example 2D
- Example 3D
R2RigidBodyDesc rigid_body = r2DynamicRigidBodyDesc();
R2RigidBodyHandle rigid_body_handle = r2InsertRigidBody(world, &rigid_body);
R2ColliderDesc collider = r2BallColliderDesc(0.5);
collider.position.translation = r2Vector(1.0, 2.0);
// Attach the collider to the rigid-body. The description's position is
// the collider's position wrt. the rigid-body.
R2ColliderHandle collider_handle = r2InsertCollider(rigid_body_handle, &collider);
/* Set the collider position wrt. its parent after the collider creation. */
r2Collider_SetPositionWrtParent(collider_handle, r2TranslationPose(r2Vector(1.0, 2.0)));
R2Vector translation = r2Collider_PositionWrtParent(collider_handle).translation;
assert(translation.x == 1.0 && translation.y == 2.0);
R3RigidBodyDesc rigid_body = r3DynamicRigidBodyDesc();
R3RigidBodyHandle rigid_body_handle = r3InsertRigidBody(world, &rigid_body);
R3ColliderDesc collider = r3BallColliderDesc(0.5);
collider.position.translation = r3Vector(1.0, 2.0, 3.0);
// Attach the collider to the rigid-body. The description's position is
// the collider's position wrt. the rigid-body.
R3ColliderHandle collider_handle = r3InsertCollider(rigid_body_handle, &collider);
/* Set the collider position wrt. its parent after the collider creation. */
r3Collider_SetPositionWrtParent(collider_handle, r3TranslationPose(r3Vector(1.0, 2.0, 3.0)));
R3Vector translation = r3Collider_PositionWrtParent(collider_handle).translation;
assert(translation.x == 1.0 && translation.y == 2.0 && translation.z == 3.0);