Matter.World.add Parameters in Matter.js

This article explains the parameters accepted by the Matter.World.add function in the Matter.js 2D physics engine. It breaks down the function signature, details the two primary arguments it accepts, and lists the specific physics object types that can be registered into a simulation world.


Function Signature

In Matter.js, Matter.World inherits from Matter.Composite. The Matter.World.add method uses the following signature:

Matter.World.add(world, object)

The method accepts two arguments: the target world composite and the object (or array of objects) to be simulated. It returns the modified world instance to allow method chaining.


Parameter 1: world


Parameter 2: object

The object argument accepts several specific Matter.js types:

1. Rigid Bodies (Matter.Body)

Single rigid bodies created using Matter.Body.create() or helper methods from Matter.Bodies (such as rectangle, circle, polygon, or fromVertices).

2. Constraints (Matter.Constraint)

Joints, springs, elastic connections, or pins connecting two bodies (or one body and a fixed world point).

3. Mouse Constraints (Matter.MouseConstraint)

A specialized constraint wrapper that links canvas mouse interactions directly to bodies within the world.

4. Composites (Matter.Composite)

Higher-level containers or groups containing multiple bodies, constraints, or other nested composites (such as car models, chains, or ragdolls).

5. Arrays of Valid Objects

Instead of calling Matter.World.add repeatedly, you can pass an array containing any combination of bodies, constraints, and composites in a single call.