Simulating a Clockwork Automaton in Matter.js

This guide explains how to design and simulate a functional mechanical clockwork automaton powered by spring motors using the Matter.js 2D physics engine. You will learn how to create a regulated energy source through virtual coiled springs, design an escapement mechanism to prevent uncontrolled unwinding, transmit power through gear trains, and convert rotational motion into lifelike automaton movements using mechanical linkages.

1. Simulating the Spring Motor (Mainspring)

Matter.js does not provide a native spiral torsion spring, but you can simulate a mainspring's energy storage and torque release mathematically or physically:

Matter.Events.on(engine, 'beforeUpdate', () => {
    if (storedSpringWind > 0) {
        mainArbor.torque = springConstant * (storedSpringWind / maxWind);
        storedSpringWind -= mainArbor.angularVelocity * dampingFactor;
    }
});

2. Building the Escapement Mechanism

Without an escapement, a spring motor discharges instantly in a rapid spin. An escapement meters energy delivery in precise ticks.

  1. Escape Wheel: Construct a rigid star-shaped body using Matter.Bodies.fromVertices with ratchet-style asymmetrical teeth. Pin its center to the world using a rigid constraint (stiffness: 1, length: 0).
  2. Pallet Fork (Anchor): Create a curved lever with two pallet arms positioned above the escape wheel. Pin its center with another rotational constraint.
  3. Oscillator (Balance Wheel or Pendulum): Attach a pendulum mass or a wheel stabilized by a bi-directional spring constraint.
  4. Action: As the mainspring turns the escape wheel, a tooth pushes against a pallet stone, impulses the oscillator, and locks against the opposite pallet. The return oscillation frees the tooth, advancing the wheel by one increment.

3. Constructing the Gear Train

Power must be stepped down or up to drive different parts of the automaton:

Matter.Events.on(engine, 'afterUpdate', () => {
    followerGear.angularVelocity = -driverGear.angularVelocity * (driverRadius / followerRadius);
});

4. Designing Automaton Linkages

Clockwork figures move using mechanical linkages driven by cams, cranks, and eccentrics connected to the gear train:

5. Tuning Stability and Performance

Clockwork mechanisms rely on tight clearances and continuous contact, which challenge real-time physics solvers. Apply the following settings to ensure smooth operation: