Skip to main content

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: Δa=m−1f\Delta{}a = m^{-1}f
  • Impulses: the velocity change is equal to the impulse divided by the mass: Δv=m−1i\Delta{}v = m^{-1}i

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.

// The last `1` argument makes sure the rigid-body is awake.
r2RigidBody_ResetForces(rigid_body_handle, 1); // Reset the forces to zero.
r2RigidBody_ResetTorques(rigid_body_handle, 1); // Reset the torques to zero.
r2RigidBody_AddForce(rigid_body_handle, r2Vector(0.0, 1000.0), 1);
r2RigidBody_AddTorque(rigid_body_handle, 100.0, 1);
r2RigidBody_AddForceAtPoint(rigid_body_handle, r2Vector(0.0, 1000.0), r2Vector(1.0, 2.0), 1);

r2RigidBody_ApplyImpulse(rigid_body_handle, r2Vector(0.0, 1000.0), 1);
r2RigidBody_ApplyTorqueImpulse(rigid_body_handle, 100.0, 1);
r2RigidBody_ApplyImpulseAtPoint(rigid_body_handle, r2Vector(0.0, 1000.0), r2Vector(1.0, 2.0), 1);

The forces and torques added with r3RigidBody_AddForce, r3RigidBody_AddTorque, and r3RigidBody_AddForceAtPoint are accumulated until they are reset with r3RigidBody_ResetForces and r3RigidBody_ResetTorques. Their current sum can be read with r3RigidBody_UserForce and r3RigidBody_UserTorque. The impulses, on the other hand, modify the velocity of the rigid-body immediately. The points given to r3RigidBody_AddForceAtPoint and r3RigidBody_ApplyImpulseAtPoint are expressed in world-space.

info

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:

  1. The rigid-body is dynamic.

  2. It is strong enough to make the rigid-body move (try a very large value and see if it does something).

  3. 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.

  4. The rigid-body is awake (by waking it up manually with r3RigidBody_WakeUp or setting the last wake_up argument to 1).