Skip to main content

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 and its rotational part as an unit quaternion (in 3D) or a unit complex number (in 2D). Both are combined into an isometry stored in the standard Bevy Transform component.

The position of a rigid-body can be set when creating it. It can also be set after its creation as illustrated below.

warning

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.

/* Set the position when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic()
// The rigid body translation.
// Default: zero vector.
.translation(vector![0.0, 5.0])
// The rigid body rotation.
// Default: no rotation.
.rotation(5.0)
// The rigid body position. Will override `.translation(...)` and `.rotation(...)`.
// Default: the identity isometry.
.position(Isometry::new(vector![1.0, 2.0], 0.4))
// All done, actually build the rigid-body.
.build();
use nalgebra::UnitComplex;
/* Set the position after the rigid-body creation. */
let rigid_body = rigid_body_set.get_mut(rigid_body_handle).unwrap();
// The `true` argument makes sure the rigid-body is awake.
rigid_body.set_translation(vector![0.0, 5.0], true);
rigid_body.set_rotation(UnitComplex::new(0.2), true);
assert_eq!(*rigid_body.translation(), vector![0.0, 5.0]);
assert_eq!(rigid_body.rotation().angle(), 0.2);

rigid_body.set_position(Isometry::new(vector![1.0, 2.0], 0.4), true);
assert_eq!(
*rigid_body.position(),
Isometry::new(vector![1.0, 2.0], 0.4)
);
commands
.spawn(RigidBody::Dynamic)
.insert(TransformBundle::from(Transform::from_xyz(0.0, 5.0, 0.0)))
/* Change the position inside of a system. */
fn modify_body_translation(mut positions: Query<&mut Transform, With<RigidBody>>) {
for mut position in positions.iter_mut() {
position.translation.y += 0.1;
}
}
/* Set the position when the rigid-body is created. */
let rigidBodyDesc = RAPIER.RigidBodyDesc.dynamic()
// The rigid body translation.
// Default: zero vector.
.setTranslation(0.0, 5.0)
// The rigid body rotation.
// Default: no rotation.
.setRotation(5.0);
let rigidBody = world.createRigidBody(rigidBodyDesc);
/* Set the position after the rigid-body creation. */
// The `true` argument makes sure the rigid-body is awake.
rigidBody.setTranslation({ x: 0.0, y: 5.0 }, true);
rigidBody.setRotation(0.2, true);

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 methods:

  • RigidBody::set_next_kinematic_rotation
  • RigidBody::set_next_kinematic_translation

These methods will let the physics pipeline compute the fictitious velocity of the position-based kinematic body for more realistic interactions with other rigid-bodies. These methods 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.

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 modify its Transform (changing its velocity won’t have any effect). This will let the physics engine compute the fictitious velocity of the kinematic body for more realistic intersections with other rigid-bodies.

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 methods:

  • RigidBody.setNextKinematicRotation
  • RigidBody.setNextKinematicTranslation

These methods will let the physics pipeline compute the fictitious velocity of the position-based kinematic body for more realistic interactions with other rigid-bodies. These methods 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.