Matter.js Constraint Stiffness Explained

In Matter.js, constraints are used to link two physics bodies or fix a single body to a specific point in the simulation world. This article explains the role of the stiffness property on a constraint, detailing how its numerical values govern physical behavior ranging from loose, elastic springs to rigid, unyielding rods, and how it interacts with the physics engine solver.

What Stiffness Controls

The stiffness property defines the rigidity or elasticity of a constraint. When two bodies connected by a constraint move relative to each other, the engine calculates a restorative force to pull them back to the constraint's specified length. The stiffness property dictates how strongly and quickly that restorative force is applied.

In mechanical terms, setting stiffness is equivalent to setting the spring constant (\(k\)) in Hooke's Law:

The Stiffness Value Range

The stiffness property accepts a floating-point number, typically ranging from 0 to 1:

Pairing Stiffness with Damping

When using lower stiffness values to simulate springs, bodies will naturally oscillate back and forth around the resting length. To prevent endless bouncing or instability, stiffness is commonly configured alongside the damping property:

Solver Iterations and Perceived Rigidity

Even when stiffness is set to 1.0, chains of constraints or heavy masses may still show slight stretching. This happens because Matter.js solves constraints iteratively. To achieve true rigidity in complex setups, you may need to increase engine.constraintIterations in your engine configuration alongside setting stiffness: 1.