How Matter.js Iterations Affect Performance
In Matter.js, iterations determine how many times the physics engine calculates collision resolution and constraint solving per frame. Increasing iteration settings improves physical accuracy, stabilizes rigid body stacking, and reduces visual artifacts like jitter and tunneling. However, because each iteration adds repetitive mathematical calculations across all active bodies, scaling these values up directly increases CPU load and can cause severe frame drops if not balanced properly.
How Iterations Work in Matter.js
Matter.js relies on an iterative impulse solver. During each engine update, bodies that collide or are bound by constraints undergo multiple passes to converge toward a realistic physical state. The engine provides three primary configuration properties:
engine.positionIterations(Default: 6): The number of passes dedicated to fixing overlapping bodies and resolving positional penetration.engine.velocityIterations(Default: 4): The number of passes used to compute impulse responses, friction, and bounce trajectories.engine.constraintIterations(Default: 2): The number of passes dedicated to maintaining distances and stiffness between bodies connected by constraints.
The Benefits of Increasing Iterations
Raising these values provides three main physical improvements:
- Stiffer and More Stable Stacks: Low iterations cause stacks of bodies to drift, oscillate, or compress artificially under gravity. Higher position and velocity iterations allow the engine to distribute normal forces correctly, keeping stacked boxes or shapes rigid.
- Reduced Object Tunneling: When objects move rapidly, low position iterations can allow bodies to clip into or pass through one another. More solver passes catch and resolve overlaps before bodies permanently penetrate boundaries.
- Rigid Constraints: Constraints (such as springs, ropes, or joints) can feel rubbery or stretch beyond their defined length under stress. Higher constraint iterations maintain accurate distances without unwanted elasticity.
The Performance Cost
Physics calculation in a game loop must complete well within the target frame budget (typically under 16.6 milliseconds for 60 FPS). Increasing iteration counts creates the following performance bottlenecks:
- Linear Complexity Scaling: The computational cost of the solver phase scales linearly with the number of iterations multiplied by the number of active collision pairs and constraints. Doubling iterations roughly doubles solver execution time.
- CPU-Bound Bottlenecks: Matter.js runs on a single JavaScript thread. Heavy iteration counts monopolize the main thread, delaying rendering, event handling, and other application logic.
- Compounding Overhead with Complex Scenes: A high iteration count may run smoothly with 10 bodies, but performance degrades rapidly once dozens of active bodies interact simultaneously, as every new collision pair undergoes the expanded iteration count.
Best Practices for Optimization
To maintain steady frame rates while achieving the necessary physical realism, use these optimization strategies:
- Target Specific Iteration Types: Do not increase
all iteration counts uniformly. If stacked objects are sinking, increase
only
positionIterations. If springs are stretching, increase onlyconstraintIterations. - Use Substepping Instead of High Iterations: In scenarios involving fast-moving objects, running the engine update multiple times with smaller delta time steps often yields better stability and collision detection than excessively cranking up iterations on a single large step.
- Thicken Static Boundaries: Instead of increasing iterations to prevent fast bodies from penetrating walls, increase the physical thickness of the wall bodies.
- Sleep Inactive Bodies: Enable sleeping
(
engine.enableSleeping = true) to take resting bodies out of the solver loop entirely, freeing up CPU overhead for objects that require active calculations.