Understanding the Matter.js afterUpdate Event

The afterUpdate event in Matter.js is an engine-level lifecycle event that fires immediately after all physics calculations, collision detections, and position integrations have completed for the current frame. This article explains what the afterUpdate event does, where it fits within the Matter.js execution cycle, its common use cases, and how to implement it correctly in your projects.

What is the afterUpdate Event?

In Matter.js, the Matter.Engine manages the core simulation loop. During each tick, the engine calculates the movement of bodies, handles collisions, and solves constraints. Once these calculations are complete, the engine triggers the afterUpdate event.

Because it executes after physical positions and velocities have been recalculated, it provides the most accurate, up-to-date state of all physics bodies before they are drawn to the screen.

The Matter.js Update Cycle

To understand afterUpdate, it helps to see where it fits within the standard execution order:

  1. beforeUpdate: Triggered before any physics calculations occur for the frame.
  2. Physics Step: The engine detects collisions, updates velocities, adjusts body positions, and resolves constraints.
  3. afterUpdate: Triggered immediately after the physics step finishes.
  4. beforeRender / afterRender: The renderer draws the updated bodies to the canvas.

Common Use Cases

The afterUpdate event is essential for tasks that depend on the final computed state of physics bodies for the current frame:

Implementation Example

To listen for the event, use the Matter.Events.on method targeting your engine instance:

const { Engine, Events, Composite } = Matter;

const engine = Engine.create();

Events.on(engine, 'afterUpdate', function(event) {
    // Access the engine or its timestamp from the event object
    const timestamp = event.timestamp;

    // Example: Iterate through bodies to clamp velocity or check bounds
    const bodies = Composite.allBodies(engine.world);

    for (let i = 0; i < bodies.length; i++) {
        const body = bodies[i];

        // Remove bodies that fall off the bottom of the screen
        if (body.position.y > 1000) {
            Composite.remove(engine.world, body);
        }
    }
});

Performance Considerations

The afterUpdate callback runs once per engine tick (typically 60 times per second). Keep logic inside this listener lightweight. Heavy computations, deep object cloning, or excessive allocations within this function can lower the frame rate and introduce physics stuttering.