Build a Chaotic Multi-Stage Pendulum in Matter.js

This guide explains how to construct a multi-stage pendulum simulation using the Matter.js 2D physics engine to demonstrate chaotic dynamics. By linking multiple rigid bodies together using revolute constraints, configuring mass properties, and eliminating damping forces like air resistance, you can observe how slight variations in initial release angles lead to wildly unpredictable, non-linear trajectories.

Understanding the Physics Setup

A simple single pendulum swings with predictable periodic motion. However, adding two or more stages—such as a double or triple pendulum—creates a coupled system where the motion of each segment directly influences the others. Because these systems are deterministic yet highly sensitive to initial conditions, they serve as classic examples of chaotic systems.

To recreate this in Matter.js, you must define:

Initializing the Matter.js Engine

Start by importing the necessary Matter.js modules and initializing the engine, world, and renderer.

const { Engine, Render, Runner, Bodies, Composite, Constraint } = Matter;

const engine = Engine.create({
  positionIterations: 10,
  velocityIterations: 10
});

const world = engine.world;

const render = Render.create({
  element: document.body,
  engine: engine,
  options: {
    width: 800,
    height: 600,
    wireframes: false
  }
});

Render.run(render);
const runner = Runner.create();
Runner.run(runner, engine);

Increasing positionIterations and velocityIterations prevents the constraints from stretching unnaturally under dynamic rotational loads.

Creating the Segments and Constraints

To make a three-stage pendulum, create a static anchor followed by three circular or rectangular bodies representing each link. Connect each stage using Constraint.create.

const originX = 400;
const originY = 150;
const segmentLength = 100;
const bobRadius = 15;

// Physics options: remove air friction to sustain motion
const bobOptions = {
  frictionAir: 0,
  friction: 0,
  restitution: 1,
  density: 0.002
};

// Fixed anchor
const anchor = Bodies.circle(originX, originY, 5, { isStatic: true });

// Pendulum bobs
const bob1 = Bodies.circle(originX + segmentLength, originY, bobRadius, bobOptions);
const bob2 = Bodies.circle(originX + segmentLength * 2, originY, bobRadius, bobOptions);
const bob3 = Bodies.circle(originX + segmentLength * 3, originY, bobRadius, bobOptions);

// Constraints linking the stages
const joint1 = Constraint.create({
  bodyA: anchor,
  bodyB: bob1,
  stiffness: 1,
  length: segmentLength
});

const joint2 = Constraint.create({
  bodyA: bob1,
  bodyB: bob2,
  stiffness: 1,
  length: segmentLength
});

const joint3 = Constraint.create({
  bodyA: bob2,
  bodyB: bob3,
  stiffness: 1,
  length: segmentLength
});

// Add all parts to the world
Composite.add(world, [anchor, bob1, bob2, bob3, joint1, joint2, joint3]);

Tuning the Simulation for True Chaotic Behavior

Default physics simulations introduce damping that quickly absorbs kinetic energy. To keep the chaotic behavior visible and energetic, apply the following adjustments:

  1. Eliminate Friction and Drag: Set frictionAir: 0 on every moving body to eliminate drag. If you want the pendulum to eventually rest without losing the rapid onset of chaos, set frictionAir to a very low value, such as 0.0001.
  2. Maximize Constraint Rigidity: Ensure stiffness: 1 on all constraints. Flexible joints introduce elastic spring dynamics, which changes the mechanical nature of the pendulum from a rigid multi-body system to a soft-body spring mesh.
  3. Mass Distribution: Modifying the density or mass of individual bobs dramatically alters the chaotic pattern. Setting the upper links to be heavier than the lower links often produces faster, more erratic whipping motions.

Visualizing the Chaos

Because chaotic systems exhibit extreme sensitivity to initial conditions, you can demonstrate chaos by instantiating two identical triple pendulums side-by-side with an initial angle offset of just 0.001 radians. Within a few seconds of release, the paths will diverge completely, providing a direct visual representation of deterministic chaos in real-time.