Matter.js Timing Delta and Integration Accuracy
Modifying the engine timing delta in Matter.js directly alters the
discrete time step used to calculate body positions and velocities,
dictating the precision of the physics simulation. The
delta parameter controls the numerical integration
scheme—Matter.js's implementation of semi-implicit Euler integration.
While lower delta values reduce truncation errors, prevent tunneling,
and enhance constraint stability at the expense of higher CPU usage,
larger or variable deltas introduce integration drift, jitter, and
simulation collapse.
The Role of Delta in Numerical Integration
Matter.js updates bodies sequentially: it calculates forces, updates velocities based on acceleration, and then updates positions based on those new velocities. This process relies on a discrete approximation of continuous calculus:
- Velocity Update: \(v(t + \Delta t) = v(t) + a(t) \cdot \Delta t\)
- Position Update: \(x(t + \Delta t) = x(t) + v(t + \Delta t) \cdot \Delta t\)
The variable \(\Delta t\)
corresponds to engine.timing.delta (defaulting to \(16.666\text{ ms}\), representing a 60 Hz
display refresh rate). Numerical integration accumulates a local
truncation error per step proportional to \(O(\Delta t)\) or \(O(\Delta t^2)\). Modifying
delta scales these integration errors proportionally.
Consequences of Decreasing Delta (Smaller Timesteps)
Reducing the delta value (e.g., from \(16.666\text{ ms}\) to \(8.333\text{ ms}\)) improves integration accuracy significantly:
- Mitigation of Tunneling: High-speed bodies travel less distance per frame. This ensures broadphase and narrowphase collision checks detect overlaps before an object passes entirely through a barrier.
- Enhanced Constraint and Joint Rigidity: Matter.js solves constraints iteratively. Smaller position displacements per step mean constraints (springs, distance constraints, and contact manifolds) experience smaller corrective impulses, virtually eliminating visual rubber-banding and oscillation.
- Energy Conservation: Smaller steps approximate continuous trajectories more closely, drastically reducing artificial kinetic energy generation during complex collisions.
The trade-off for higher accuracy is increased CPU demand, as more sub-steps must be evaluated per real-world second.
Consequences of Increasing Delta (Larger Timesteps)
Increasing the delta (e.g., greater than \(20\text{ ms}\)) degrades simulation fidelity:
- Truncation and Overshooting: Large steps cause accelerations to be applied over too long a duration, leading bodies to overshoot their true physical paths.
- Tunneling: Fast-moving objects move farther in a single tick than their own collision bounds, bypassing thin walls entirely.
- Explosive Instability: When bodies deeply penetrate one another due to an oversized step, the collision solver generates massive separation impulses. This introduces rapid energy growth, causing bodies to violently launch away from contact points.
Fixed vs. Variable Delta
Using a variable delta derived dynamically from frame render times
(such as browser requestAnimationFrame deltas) compromises
deterministic behavior. A sudden frame-rate drop spikes the delta,
introducing large integration errors that disrupt established equilibria
(such as stacked boxes).
To preserve accuracy, the engine delta should remain constant. If
frame rates drop or higher precision is needed, decouple rendering from
simulation by executing multiple fixed-delta updates per frame
(sub-stepping) rather than adjusting delta to match elapsed
wall-clock time.