rigid_body_forces_and_impulses
In addition to gravity, it is possible to add custom forces (or torques) or apply impulses (or torque impulses) to dynamic rigid-bodies in order to make them move in specific ways. Forces affect the rigid-body's acceleration whereas impulses affect the rigid-body's velocity. They are both based on the familiar equations:
- Forces: the acceleration change is equal to the force divided by the mass:
- Impulses: the velocity change is equal to the impulse divided by the mass:
Forces can be added, and impulses can be applied, to a rigid-body after it has been created. Added forces are persistent across simulation steps, and can be cleared manually.
rigid_body = world.rigid_bodies[rigid_body_handle]
# The rigid-body is woken up, unless `wake_up=False` is given.
rigid_body.reset_forces() # Reset the forces to zero.
rigid_body.reset_torques() # Reset the torques to zero.
rigid_body.add_force((0.0, 1000.0, 0.0))
rigid_body.add_torque((100.0, 0.0, 0.0))
rigid_body.add_force_at_point((0.0, 1000.0, 0.0), (1.0, 2.0, 3.0))
rigid_body.apply_impulse((0.0, 1000.0, 0.0))
rigid_body.apply_torque_impulse((100.0, 0.0, 0.0))
rigid_body.apply_impulse_at_point((0.0, 1000.0, 0.0), (1.0, 2.0, 3.0))
The forces and torques added with RigidBody.add_force, RigidBody.add_torque, and RigidBody.add_force_at_point
are accumulated until they are reset with RigidBody.reset_forces and RigidBody.reset_torques. Their current sum is
given by the RigidBody.user_force and RigidBody.user_torque properties. The impulses, on the other hand, modify the
velocity of the rigid-body immediately. The points given to add_force_at_point and apply_impulse_at_point are
expressed in world-space.
Keep in mind that a dynamic rigid-body with a zero mass won't be affected by a linear force/impulse, and a rigid-body with a zero angular inertia won't be affected by torques/torque impulses. So if your force doesn't appear to do anything, make sure that:
-
The rigid-body is dynamic.
-
It is strong enough to make the rigid-body move (try a very large value and see if it does something).
-
The rigid-body has a non-zero mass or angular inertia either because they were set explicitly, or because they were computed automatically from colliders with non-zero densities.
-
The rigid-body is awake (by waking it up manually with
RigidBody.wake_upor keeping thewake_upargument to its default valueTrue).