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).
Both are 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.
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
commands
.spawn(RigidBody::Dynamic)
.insert(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;
}
}
commands
.spawn(RigidBody::Dynamic)
.insert(Transform::from_xyz(0.0, 0.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;
}
}
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 modify its Transform (changing its velocity won’t have
any effect). This won't teleport the kinematic body immediately: the modified Transform is used as its next
kinematic position, which lets the physics engine compute the fictitious velocity of the kinematic body for more
realistic interactions with other rigid-bodies.