What Is Linear Damping in Planck.js?
Linear damping is a crucial property in the planck.js 2D physics engine that simulates fluid resistance, gradually slowing down a moving body over time. Unlike friction, which requires contact with another surface, linear damping acts as an omnipresent drag force—similar to air resistance or moving through water. By adjusting this value, developers can control how long objects retain their kinetic energy, allowing for more realistic movement or specialized gameplay mechanics like ice sliding or heavy mud traversal.
Understanding Linear Damping
In physics engines like planck.js (a JavaScript rewrite of Box2D), simulation follows Newton’s laws of motion. Without any external forces or damping, an object in motion would stay in motion indefinitely.
Linear damping applies a force opposite to the body’s current linear velocity vector. The higher the damping value, the faster the object will come to a halt.
Key Characteristics of Linear Damping
- Velocity-Based: The damping force is proportional to the object’s current speed. Faster objects experience more drag than slower ones.
- No Contact Required: It affects the body regardless of whether it is colliding with or touching other fixtures.
- Frame-Rate Independent: Planck.js calculates damping over time steps, ensuring consistent behavior across different hardware performance levels.
How Damping Affects Movement
When you apply linear damping to a Body in planck.js, it
directly alters how forces and impulses behave.
| Damping Value | Behavioral Effect | Common Use Cases |
|---|---|---|
| 0.0 (Default) | Perfect vacuum. The body never slows down unless it hits something. | Space shooters, frictionless ice simulations. |
| 0.1 – 1.0 | Low resistance. The body glides smoothly but eventually comes to a rest. | Standard top-down character movement, rolling balls. |
| 2.0 – 5.0 | Medium resistance. Heavy drag is applied; movement feels sluggish. | Under-water environments, heavy objects. |
| 10.0+ | Extreme resistance. The body stops almost instantly when forces cease. | Menu UIs, top-down driving games to prevent sliding. |
The Math Behind the Movement
Planck.js updates the linear velocity \(v\) at each time step \(t\) using the damping coefficient \(d\):
\[v_{new} = v_{old} \times \frac{1}{1 + t \times d}\]
As a result, an object’s velocity decays exponentially rather than linearly, creating a smooth, natural-looking deceleration.
Implementing Linear Damping in Planck.js
You can define linear damping either during the initialization of a body definition or dynamically during runtime using the body’s methods.
Alternative 1: Initialization
const body = world.createBody({
type: 'dynamic',
position: Vec2(0, 0),
linearDamping: 2.5 // Sets the drag directly
});Alternative 2: Runtime Adjustment
// Fetch an existing body and update its drag dynamically
body.setLinearDamping(0.5);
// Check the current damping value
const currentDamping = body.getLinearDamping();By balancing linear damping with applied forces (such as
body.applyForce() or
body.applyLinearImpulse()), you can achieve precise,
responsive controls tailored to your game’s specific environment.