Matter.js Constraint Stiffness 1.0 vs 0.1 Explained
In Matter.js, the stiffness property dictates how
rigidly a constraint maintains its resting length when subjected to
external forces. This article breaks down the mechanical and visual
differences between configuring a constraint with a
stiffness value of 1.0 versus 0.1, explaining how the
engine resolves positional offsets, how the bodies behave in real-time
simulations, and when to use each setting.
How Constraint Stiffness Works in Matter.js
In Matter.js, constraints act as virtual springs or linkages between
two bodies, or between one body and a fixed world coordinate. The
stiffness property accepts a value ranging from
0 to 1 (though values greater than 1 can be
used with multiple engine solver iterations). This value represents the
fraction of positional error the solver corrects during each tick:
- Correction Rate: A value of 1.0 attempts to resolve 100% of the displacement error in a single step, whereas a value of 0.1 resolves roughly 10% per step.
- Elastic Response: Lower values introduce elasticity, while higher values create rigidity.
Behavior of Stiffness: 1.0 (Rigid Linkage)
When a constraint's stiffness is set to 1.0, it behaves like an inflexible, solid rod.
- Immediate Correction: The physics solver enforces
the defined
lengthof the constraint with maximum priority. If a body attached to the constraint experiences an impulse, the constraint immediately pulls or pushes back without stretching. - No Springiness: The connection displays virtually zero oscillation or "bungee" effect. Attached bodies move synchronously according to the constraint's pivot points.
- High-Stress Scenarios: Under extreme forces or
collisions involving heavy masses, a stiffness of 1.0 may exhibit slight
jitter or deformation if the engine's
constraintIterationssetting is too low, but it will never intentionally stretch. - Common Use Cases: Ragdoll limbs, structural trusses, mechanical arms, pendulums, and fixed-distance linkages.
Behavior of Stiffness: 0.1 (Soft Spring)
When a constraint's stiffness is set to 0.1, it behaves like a loose, elastic spring or rubber band.
- Delayed Correction: Because the solver resolves only a small portion of the positional displacement per tick, the constraint yields easily when external forces (such as gravity or collisions) pull against it.
- Visible Elongation: The distance between the
connected points will noticeably stretch well beyond the original
lengthproperty when under tension. - Oscillation and Bouncing: Bodies connected with a
stiffness of 0.1 will bounce back and forth around the resting length.
Unless the
dampingproperty is increased, this oscillation will persist for a noticeable duration. - Soft Acceleration: Rather than abruptly jerking connected bodies, a stiffness of 0.1 transfers momentum gradually, resulting in smoother, organic motion.
- Common Use Cases: Vehicle suspension systems, bungee cords, elastic ropes, soft-body approximations, and floating UI elements.
Direct Comparison
| Feature | Stiffness: 1.0 | Stiffness: 0.1 |
|---|---|---|
| Physical Analogy | Solid steel rod | Loose spring or rubber band |
| Positional Yield | Nearly zero stretch | Significant elongation under load |
| Oscillation | None (instant equilibrium) | High (springs back and forth) |
| Force Transfer | Immediate and rigid | Gradual and cushioned |
| Damping Requirement | Rarely needed | Often required to control endless bounce |