matter.js registry pattern

  • Initialize engine: create physics engine with Engine.create()
  • Initiailze renderer: create renderer with Render.create({...}), where you pass in the engine instance and a very other options.
  • Creating entities/bodies/composites: Matter.js provides a collection of basic entities like rectangle and pyramid, created using Bodies.rectangle(x, y, ...).
  • Registering entities to core “world”: with instantiated objects, the add method is used to add a list of entities directly, using World.add(..., [pyramid, rect1, rect2]).

So Matter.js takes on a very modular environment construction setup, where core components like entities/bodies/composites are offered through the JS equivalent of Python modules, and are instantiated like regular classes. Then, these objects are glued together by telling a central world and engine about them through methods like add().

Composite helpers

This is a set of functions responsible for binding up collections of composites in specific ways. The functions take some necessary values, along with a callback function that returns newly created composites. This callback is executed internally to create the new composites with specific values in accordance with the type of builder.

Note: while this is the most abstract composite creation scheme I’ve seen in matter.js, it’s not quite as flexible as a general creation scheme. The callback is executed in pretty restrictive ways e.g. creating rectangles at grid vertex positions, or creating a linear chain of bodies and associated constraints to bind pairs together.

  • stack: