Matter.js Engine.merge Internal State Updates

When combining two physics engines in Matter.js using Matter.Engine.merge(engineA, engineB), the physics engine unifies their configurations, composite hierarchies, and runtime state into the primary engine instance. This article details the exact internal states modified during this merge, covering engine-level configurations, collision caches, and world composite structures.

Engine Configuration and Properties

The operation invokes Matter.Common.extend(engineA, engineB), copying top-level properties and settings from engineB directly onto engineA. The internal properties updated include:

Collision Pairs and Cache Invalidation

To maintain physical consistency and prevent ghost collisions between entities that previously existed in separate coordinate spaces, the internal cache of engineA is reset:

World Composite Hierarchy

The physical entities managed by both engines are consolidated inside the destination engine's root world:

Broadphase and Detector Re-synchronization

Although Engine.merge modifies the static structure immediately, the collision detector's internal spatial index is updated during the next Matter.Engine.update call: