What Is Matter.Composites in Matter.js?
The Matter.Composites module in the Matter.js 2D physics
engine is a utility library used to quickly generate complex, multi-body
physical structures and geometric arrangements. Instead of requiring
developers to manually calculate coordinates, instantiate dozens of
individual bodies, and link them with custom constraints, this module
provides pre-packaged factory methods to generate common physics presets
such as ropes, stacks, cloth meshes, and vehicles in just a few lines of
code.
Core Purpose
In Matter.js, a Composite is simply a container that
holds bodies, constraints, and even nested composites. The
Matter.Composites module builds directly on this concept by
offering procedural generators that produce fully assembled
Composite objects configured for immediate addition to the
physics world.
Key Applications and Factory Methods
- Grids and Pyramids (
stack,pyramid): Creates organized arrays of bodies. Thestackmethod places bodies in rows and columns with configurable gaps, which is commonly used to build walls, block towers, or particle grids. Thepyramidmethod creates triangular arrangements ideal for bowling pins or destructible structures. - Chains and Ropes (
chain): Automatically connects an array of sequential bodies using distance constraints. This is primarily used to build ropes, swinging pendulums, suspension bridges, or hanging signs. - Soft Bodies and Cloth (
softBody,mesh): Generates flexible, deformable assemblies. Themeshmethod connects bodies in a 2D grid using cross-braced constraints, simulating cloth or net physics. ThesoftBodymethod creates a similar grid wrapped in an outer hull, simulating jelly-like, squishy objects that compress and bounce back into shape. - Vehicles (
car): Instantiates a functional compound vehicle structure consisting of a main chassis, two wheels, and axle constraints configured to allow wheel rotation relative to the body. - Mechanical Demonstrations
(
newtonsCradle): Creates a functional Newton's Cradle toy, providing an out-of-the-box demonstration of momentum and energy conservation.
Why Use Matter.Composites
By abstracting the math and boilerplate required to bind bodies and
springs together, Matter.Composites dramatically
accelerates game development and physics prototyping. It ensures that
complex structures behave stably out of the box by using optimized
default stiffness, damping, and collision filtering settings.