collider_position
The position of a collider represents its location (translation) in 2D or 3D world-space as well as its orientation (rotation).
Both are combined in a Bevy Transform
component.
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. */
commands
.spawn(Collider::cuboid(0.5, 0.5))
.insert(TransformBundle::from(Transform::from_xyz(1.0, 2.0, 0.0)));
// Attach the collider to the rigid-body. The collider is attached as its
// children, so the collider’s `Transform` components sets its position
// relative to the parent rigid-body.
commands
.spawn((RigidBody::Dynamic, GlobalTransform::default()))
.with_children(|children| {
children
.spawn(Collider::cuboid(0.5, 0.5))
.insert(TransformBundle::from(Transform::from_xyz(1.0, 2.0, 0.0)));
});
/* Set the collider position when the collider is created. */
commands
.spawn(Collider::cuboid(0.5, 0.5, 0.5))
.insert(TransformBundle::from(Transform::from_xyz(1.0, 2.0, 3.0)));
/* Set the collider position inside of a system. */
fn modify_collider_position(mut positions: Query<&mut Transform, With<Collider>>) {
for mut position in positions.iter_mut() {
position.translation.x = 2.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, setting the Transform
of a collider attached to a rigid-body will modify the position of the
collider relative to the rigid-body it is attached to:
- Example 2D
- Example 3D
/* Set the collider position inside of a system. */
fn modify_collider_position(mut positions: Query<&mut Transform, With<Collider>>) {
for mut position in positions.iter_mut() {
position.translation.x = 2.0;
}
}
// Attach the collider to the rigid-body. The collider is attached as its
// children, so the collider’s `Transform` components sets its position
// relative to the parent rigid-body.
commands
.spawn(RigidBody::Dynamic)
.with_children(|children| {
children
.spawn(Collider::cuboid(0.5, 0.5, 0.5))
.insert(TransformBundle::from(Transform::from_xyz(1.0, 2.0, 0.0)));
});