Bipedal Robot with Hip Constraints in Matter.js

This article explains how to build a functional bipedal walking robot in the Matter.js 2D physics engine using alternating hip constraints. You will learn how to define the rigid bodies for the torso and legs, connect them via revolute hip joints using Matter.js constraints, and actuate an alternating walking cycle driven by torque and phase-shifted kinematic routines to achieve stable forward locomotion.

1. Structure and Rigid Bodies

A basic bipedal model requires at least three primary rigid bodies: a central torso and two independent leg segments (left and right). To simplify balance, each leg can initially be modeled as a single rigid capsule or rounded rectangle, though knee segments can be added later.

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

// Create the torso
const torso = Bodies.rectangle(400, 200, 40, 100, {
    density: 0.005,
    collisionFilter: { group: -1 } // Prevent self-collision
});

// Create left and right legs
const legOptions = {
    density: 0.002,
    friction: 0.9,
    collisionFilter: { group: -1 }
};

const leftLeg = Bodies.rectangle(390, 280, 20, 100, legOptions);
const rightLeg = Bodies.rectangle(410, 280, 20, 100, legOptions);

Using negative collision group numbers (group: -1) ensures the robot's limbs interact with the ground and obstacles without tangling or colliding with each other.

2. Creating the Hip Constraints

Matter.js simulates joints using Constraint.create. To make hips that pivot around specific points on the torso, attach the top of each leg to the bottom left and bottom right of the torso with zero-length constraints.

const hipLeft = Constraint.create({
    bodyA: torso,
    pointA: { x: -10, y: 40 },
    bodyB: leftLeg,
    pointB: { x: 0, y: -45 },
    stiffness: 0.9,
    length: 0
});

const hipRight = Constraint.create({
    bodyA: torso,
    pointA: { x: 10, y: 40 },
    bodyB: rightLeg,
    pointB: { x: 0, y: -45 },
    stiffness: 0.9,
    length: 0
});

Composite.add(world, [torso, leftLeg, rightLeg, hipLeft, hipRight]);

3. Implementing Alternating Gait Actuation

Matter.js constraints do not contain built-in motorized angle actuators. To produce an alternating swing, apply sinusoidal angular torques or target rotations directly to the hips via the engine's update loop (beforeUpdate).

By applying a sine wave to one leg and a cosine (or 180-degree phase-shifted) wave to the other, you create an alternating gait cycle where one leg swings forward to plant itself while the other pushes backward.

let stepCounter = 0;
const gaitSpeed = 0.05;
const stepAmplitude = 0.003; // Angular torque magnitude

Events.on(engine, 'beforeUpdate', () => {
    stepCounter += gaitSpeed;

    // Calculate phase-shifted torques for alternating steps
    const leftTorque = Math.sin(stepCounter) * stepAmplitude;
    const rightTorque = Math.sin(stepCounter + Math.PI) * stepAmplitude;

    // Apply torque relative to the torso to swing the limbs
    leftLeg.torque = leftTorque;
    rightLeg.torque = rightTorque;

    // Apply equal and opposite reaction torque to the torso
    torso.torque = -(leftTorque + rightTorque);
});

4. Tuning Center of Mass and Stability

A common issue with bipedal simulations is tipping over due to inverted pendulum dynamics. Stabilize the robot using the following adjustments: