rigid_body_gravity
Gravity is such a common force that it is implemented as a special case (even if it could easily be implemented
by the user using force application). PhysicsWorld::gravity (or as an argument to the PhysicsPipeline::step method) and can be modified at will.RapierConfiguration::gravity of the component RapierConfiguration and can be modified at
will.World. It can be modified by modifying the
field World.gravity.r3SetGravity (and read with r3Gravity) and can be modified at will.PhysicsWorld.gravity property (initialized by the gravity argument of the PhysicsWorld constructor, and zero by default) or as an argument to the PhysicsPipeline.step method, and can be modified at will.PhysicsWorld.wake_up_all.
Because fixed and kinematic bodies are immune to forces, they are not affected by gravity.
A rigid-body with no mass will not be affected by gravity either. So if your rigid-body doesn't fall when you expected it to, make sure it has a mass set explicitly, or has at least one collider with non-zero density attached to it.
It is possible to change the way gravity affects a specific rigid-body by setting the rigid-body's gravity scale
to a value other than 1.0. The magnitude of the gravity applied to this body will be multiplied by this scaling
factor. Therefore, a gravity scale set to 0.0 will disable gravity for the rigid-body whereas a gravity scale set to
2.0 will make it twice as strong. A negative value will flip the direction of the gravity for this rigid-body.
This gravity scale factor can be set when the rigid-body is created or after its creation:
/* Set the gravity scale when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic()
// Divide by 2 the strength of gravity for this rigid-body.
.gravity_scale(0.5)
.build();
/* Set the gravity scale after the rigid-body creation. */
let rigid_body = &mut world.bodies[rigid_body_handle];
// The `true` argument makes sure the rigid-body is awake.
rigid_body.set_gravity_scale(0.5, true);
assert_eq!(rigid_body.gravity_scale(), 0.5);
/* Set the gravity scale when the rigid-body is created. */
commands.spawn(RigidBody::Dynamic).insert(GravityScale(2.0));
/* Set the gravity scale inside of a system. */
fn modify_body_gravity_scale(mut grav_scale: Query<&mut GravityScale>) {
for mut grav_scale in grav_scale.iter_mut() {
grav_scale.0 = 2.0;
}
}
/* Set the gravity scale when the rigid-body is created. */
let rigidBodyDesc = RAPIER.RigidBodyDesc.dynamic()
.setGravityScale(2.0);
let rigidBody = world.createRigidBody(rigidBodyDesc);
/* Set the gravity scale after the rigid-body creation. */
rigidBody.setGravityScale(2.0, true);
/* Set the gravity scale when the rigid-body is created. */
R2RigidBodyDesc rigid_body = r2DynamicRigidBodyDesc();
// Divide by 2 the strength of gravity for this rigid-body.
rigid_body.gravityScale = 0.5;
/* Set the gravity scale after the rigid-body creation. */
// The last `1` argument makes sure the rigid-body is awake.
r2RigidBody_SetGravityScale(rigid_body_handle, 0.5, 1);
assert(r2RigidBody_GravityScale(rigid_body_handle) == 0.5);
# Set the gravity scale when the rigid-body is created.
rigid_body = (
rp.RigidBody.dynamic()
# Divide by 2 the strength of gravity for this rigid-body.
.gravity_scale(0.5)
.build()
)
# Set the gravity scale after the rigid-body creation.
rigid_body = world.rigid_bodies[rigid_body_handle]
# Setting this property automatically wakes the rigid-body up.
rigid_body.gravity_scale = 0.5
assert rigid_body.gravity_scale == 0.5