Matter.js Composite vs Compound Body Explained

In Matter.js, both compound bodies and composites are used to group elements together, but they serve entirely different architectural and physical purposes. A compound body is a single rigid physical body constructed from multiple shapes that move together as an indivisible unit, whereas a composite is an organizational container used to hold and manage multiple independent bodies, constraints, and even other composites. Understanding the distinction between the two is essential for managing physics simulations, collision behavior, and performance in Matter.js.

What Is a Compound Body?

A compound body is an instance of Matter.Body formed by combining multiple smaller body parts into one rigid object using Matter.Body.create({ parts: [...] }).

What Is a Composite?

A composite (Matter.Composite) is a high-level data structure designed to group multiple simulation objects. In fact, the top-level simulation environment (engine.world) is itself a composite.

Key Differences

1. Rigidity vs. Flexibility

A compound body is strictly rigid. If you build a vehicle as a compound body, the chassis and wheels are welded together permanently, meaning the wheels cannot rotate around an axle. In contrast, building a vehicle using a composite allows you to create the chassis and wheels as separate bodies connected by constraints, enabling wheel rotation and suspension movement.

2. Collision and Physics Processing

In a compound body, internal parts do not collide with one another. Matter.js calculates collision vertices for all parts and treats the outcome as an interaction with a single body. In a composite, every body inside the container is treated as an individual actor in the broadphase and narrowphase collision pipeline, meaning they can collide with each other unless explicitly disabled via collision filters.

3. Management and Manipulation

You apply forces, torques, and velocities directly to a compound body, and all constituent parts react instantly in unison. With a composite, you cannot directly apply a single physical force to the composite itself; you must apply forces to individual bodies within the composite or iterate over its collection.

When to Use Which