rigid_body_position
The position of a rigid-body 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 as a unit quaternion (R3Rotation) in 3D, or as an angle in radians (R2Rotation) in 2D. Both are combined into a pose (the R3Pose structure).
The position of a rigid-body can be set when creating it. It can also be set after its creation as illustrated below.
Directly changing the position of a rigid-body is equivalent to teleporting it: this is a not a physically realistic action! Teleporting a dynamic or kinematic bodies may result in odd behaviors especially if it teleports into a space occupied by other objects. For dynamic bodies, forces, impulses, or velocity modification should be preferred. For kinematic bodies, see the discussion after the examples below.
- Example 2D
- Example 3D
/* Set the position when the rigid-body is created. */
R2RigidBodyDesc rigid_body = r2DynamicRigidBodyDesc();
// The rigid body translation.
// Default: zero vector.
rigid_body.position.translation = r2Vector(0.0, 5.0);
// The rigid body rotation.
// Default: no rotation.
rigid_body.position.rotation = r2Rotation(5.0);
// The rigid body position. Will override the translation and rotation set above.
// Default: the identity pose.
rigid_body.position = r2Pose(r2Vector(1.0, 2.0), r2Rotation(0.4));
/* Set the position after the rigid-body creation. */
// The last `1` argument makes sure the rigid-body is awake.
r2RigidBody_SetTranslation(rigid_body_handle, r2Vector(0.0, 5.0), 1);
r2RigidBody_SetRotation(rigid_body_handle, r2Rotation(0.2), 1);
R2Vector translation = r2RigidBody_Translation(rigid_body_handle);
R2Rotation rotation = r2RigidBody_Rotation(rigid_body_handle);
assert(translation.x == 0.0 && translation.y == 5.0);
assert(rotation.angle == (R2Real)0.2);
r2RigidBody_SetPosition(rigid_body_handle, r2Pose(r2Vector(1.0, 2.0), r2Rotation(0.4)), 1);
R2Pose position = r2RigidBody_Position(rigid_body_handle);
assert(position.translation.x == 1.0 && position.translation.y == 2.0);
assert(position.rotation.angle == (R2Real)0.4);
/* Set the position when the rigid-body is created. */
R3RigidBodyDesc rigid_body = r3DynamicRigidBodyDesc();
// The rigid body translation.
// Default: zero vector.
rigid_body.position.translation = r3Vector(0.0, 5.0, 1.0);
// The rigid body rotation.
// Default: no rotation.
rigid_body.position.rotation = r3RotationFromAxisAngle(r3Vector(1.0, 0.0, 0.0), 0.2);
// The rigid body position. Will override the translation and rotation set above.
// Default: the identity pose.
rigid_body.position = r3Pose(r3Vector(1.0, 2.0, 3.0), r3RotationFromAxisAngle(r3Vector(1.0, 0.0, 0.0), 0.2));
/* Set the position after the rigid-body creation. */
R3Rotation rotation = r3RotationFromAxisAngle(r3Vector(1.0, 0.0, 0.0), 0.2);
// The last `1` argument makes sure the rigid-body is awake.
r3RigidBody_SetTranslation(rigid_body_handle, r3Vector(0.0, 5.0, 1.0), 1);
r3RigidBody_SetRotation(rigid_body_handle, rotation, 1);
R3Vector current_translation = r3RigidBody_Translation(rigid_body_handle);
R3Rotation current_rotation = r3RigidBody_Rotation(rigid_body_handle);
assert(current_translation.x == 0.0 && current_translation.y == 5.0 && current_translation.z == 1.0);
assert(current_rotation.x == rotation.x && current_rotation.y == rotation.y &&
current_rotation.z == rotation.z && current_rotation.w == rotation.w);
R3Pose pose = r3Pose(r3Vector(1.0, 2.0, 3.0), r3RotationFromAxisAngle(r3Vector(0.0, 1.0, 0.0), 0.4));
r3RigidBody_SetPosition(rigid_body_handle, pose, 1);
R3Pose current_pose = r3RigidBody_Position(rigid_body_handle);
assert(current_pose.translation.x == 1.0 && current_pose.translation.y == 2.0 &&
current_pose.translation.z == 3.0);
assert(current_pose.rotation.x == pose.rotation.x && current_pose.rotation.y == pose.rotation.y &&
current_pose.rotation.z == pose.rotation.z && current_pose.rotation.w == pose.rotation.w);
In order to move a dynamic rigid-body it is strongly discouraged to set its position directly as it may results in weird behaviors: it's as if the rigid-body teleports itself, which is a non-physical behavior. For dynamic bodies, it is recommended to either set its velocity or to apply forces or impulses.
For velocity-based kinematic bodies, it is recommended to set its velocity instead of setting its position directly. For position-based kinematic bodies, it is recommended to use the special functions:
r3RigidBody_SetNextKinematicRotationr3RigidBody_SetNextKinematicTranslationr3RigidBody_SetNextKinematicPosition(for both at once)
These functions will let the physics pipeline compute the fictitious velocity of the position-based kinematic body for
more realistic interactions with other rigid-bodies. These functions won't immediately modify the position of the
kinematic body itself. The position of the kinematic body will be automatically set to these values during the next
physics pipeline update (the pending pose can be read with r3RigidBody_NextPosition).