How Does Angular Damping Prevent Indefinite Rotation in Planck.js?
This article provides an overview of how angular damping functions within planck.js—a 2D JavaScript physics engine based on Box2D—to control and reduce a rigid body’s rotational velocity over time. You will learn the underlying mathematical concept behind angular damping, how it differs from linear damping, and how to implement it effectively to prevent objects from spinning indefinitely in your simulations.
Understanding Angular Damping in Physics Engines
In a frictionless digital physics simulation, an object given a rotational force (torque) will spin forever due to Newton’s first law of motion. To simulate atmospheric resistance, friction, or general energy loss, planck.js uses a property called angular damping.
Angular damping applies a continuous drag force that resists the rotational motion of a rigid body. Instead of stopping an object abruptly, it gradually bleeds off rotational velocity at every physics step, ensuring that objects eventually come to a natural rest.
The Mathematics Behind the Damping
Unlike dry friction, which applies a constant deceleration force, angular damping in planck.js is typically proportional to the current angular velocity.
At each time step (\(dt\)), the physics engine updates the angular velocity (\(\omega\)) using a formula similar to:
\[\omega_{new} = \omega_{old} \times \frac{1}{1 + dt \times d_{\theta}}\]
Where \(d_{\theta}\) represents the angular damping coefficient.
- A damping value of
0.0means there is no drag, and the body will rotate indefinitely. - A higher damping value increases the resistance, causing the rotation to slow down much faster.
Implementation in Planck.js
You can set the angular damping of a rigid body either during its initial definition or dynamically during runtime.
Setting Damping During Body Creation
When defining a BodyDef object, you can pass the
angularDamping property directly into the
configuration.
const body = world.createBody({
type: 'dynamic',
position: Vec2(0, 0),
angularDamping: 2.0 // Prevents infinite spinning
});Modifying Damping Dynamically
If you need to change the rotational resistance on the fly—for example, if a character enters a thick liquid or a low-gravity zone—you can use the built-in setter method.
// Increase resistance dynamically
body.setAngularDamping(5.0);Angular Damping vs. Linear Damping
It is important to distinguish angular damping from its counterpart, linear damping:
- Angular Damping: Specifically targets and reduces the rotational velocity (spinning around an axis).
- Linear Damping: Specifically targets and reduces the linear velocity (movement from point A to point B).
For realistic behavior, both properties are often used together. For instance, a rolling ball in a top-down game requires linear damping to slow its forward momentum and angular damping to stop its visual spin.