Matter.js Rope Bridge You Can Cut — Free Physics Snippet

Matter.js Rope Bridge You Can Cut · Animations · Plain HTML, CSS & JS · Live preview

CategoryAnimations

What's included

Features

Chained plank bridge
Composites.stack plus Composites.chain.
Pinned ends
World-point constraints on the cliffs.
Swinging candy rope
Tiny circles, stiffness 1.
Swipe cutting
Segment intersection per constraint.
Nested composite search
Composite.allComposites.
Cut and drag modes
Mouse constraint mask toggled.
Blade trail
Fading, shadowed swipe line.
Off-screen cleanup
Removes bodies that fall away.

About this UI Snippet

Ropes, Bridges and Cutting Constraints in Matter.js

Screenshot of the Matter.js Rope Bridge You Can Cut snippet rendered live

Ropes and bridges in Matter.js are chains: bodies joined end-to-end by constraints. Cutting one is simply removing a constraint — and this snippet shows how to find which constraint your swipe crossed.

Building the bridge with Composites

Composites.stack creates a row of 14 plank bodies. Composites.chain(bridge, 0.36, 0, -0.36, 0, options) links each plank to the next — the four numbers are the attachment points as fractions of each body's width and height, here near each plank's right and left ends. Two extra constraints with only pointA (a fixed world point) pin the ends to the cliffs. Planks share a negative collision group so neighbours don't fight each other.

A rope with a candy

The rope is a vertical stack of tiny invisible circles chained top to bottom with stiffness: 1 and short lengths, which makes it nearly inextensible. The top is pinned, the bottom holds a candy, and an initial velocity sets it swinging.

Cutting: line-segment intersection

In cut mode, every pointer move produces a small segment from the previous to the current position. For each constraint labelled link, the snippet computes its two world endpoints (body position plus its offset point, or the fixed pointA) and tests whether the swipe segment crosses it using the classic orientation test. Crossed constraints are removed with Composite.remove(composite, constraint) — walked via Composite.allComposites because they live inside the bridge and rope composites. Gravity takes it from there: the bridge splits and swings down, the candy flies off.

Cut vs drag mode

The MouseConstraint is always present; in cut mode its collision mask is set to 0 so it can't grab anything, and in drag mode it's restored. A fading, shadowed trail drawn in afterRender sells the blade.

Housekeeping

Crates drop onto the bridge to load it, and bodies that fall far below the canvas are removed to keep the simulation light.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Paste this snippet into an AI assistant like Claude and ask it to explain how chain offsets, pins and the cut test work. Ask it to turn it into a Cut the Rope level with a hungry monster to feed, stars to collect, multiple ropes, air bubbles, or a bridge a car must cross before you cut it. It can also help make the rope render as a smooth curve.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a cuttable rope bridge with Matter.js 0.20 (from a CDN) in plain HTML, CSS and JavaScript.

Requirements:
- Two green cliffs with a blue river between them.
- A bridge of 14 wooden planks made with Composites.stack and Composites.chain (attach near plank ends), pinned to the cliffs with world-point constraints, planks in one negative collision group.
- A rope of 12 tiny invisible circles chained top to bottom (stiffness 1), pinned at the top, holding a striped pink candy that starts swinging.
- Label every link constraint; in cut mode, each pointer move forms a segment and any link whose endpoints' line crosses it is removed from its parent composite (search Composite.allComposites). Show a fading white blade trail.
- A Cut / Drag toggle (disable the mouse constraint via its collision mask in cut mode), a Drop crates button, Rebuild, and a cut counter.
- Remove bodies that fall far off-screen, and remove the mouse wheel listeners so the page scrolls.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

Requires
<div class="rb">
  <div class="rb-bar">
    <h2>Rope bridge</h2>
    <div class="rb-seg" role="group" aria-label="Mouse mode">
      <button type="button" data-mode="cut" class="on">✂ Cut</button>
      <button type="button" data-mode="drag">✋ Drag</button>
    </div>
    <button type="button" id="rbCrates">Drop crates</button>
    <button type="button" id="rbReset" class="ghost">Rebuild</button>
    <span class="rb-count" id="rbCount"></span>
  </div>
  <div class="rb-stage" id="rbStage"></div>
  <p class="rb-hint">Cut mode: swipe across a rope or bridge link to slice it. Drag mode: grab planks, crates and the candy.</p>
</div>

Step by step

How to Use

  1. 1
    Swipe to cutSlice through the rope or any bridge link.
  2. 2
    Watch it fallThe bridge splits and swings; crates tumble.
  3. 3
    Drop cratesLoad the bridge before you cut.
  4. 4
    Switch to dragGrab planks, crates or the candy.
  5. 5
    RebuildStart fresh with the cut counter reset.

Real-world uses

Common Use Cases

Cut the Rope-style games
The core mechanic in a few functions.
Learning constraints
How chains, pins and stiffness work.
Interactive illustrations
Hands-on physics for lessons.
Game jam starters
Add goals and a character.
Hit-testing lines
Reuse the segment intersection test.
Related: Matter.js Soft-Body Jelly
More constraint tricks: Matter.js Soft-Body Jelly Blobs.
Related: Matter.js Newton’s Cradle
Constraints as pendulums: Matter.js Newton’s Cradle.

Got questions?

Frequently Asked Questions

Create a column of small bodies with Composites.stack and link them with Composites.chain, using short lengths and high stiffness. Pin the first body with a Constraint that has only pointA (a world position).

Remove it with Composite.remove(parentComposite, constraint). Constraints created by Composites.chain live inside that composite, so search Composite.allComposites(world) to find them.

Take the swipe segment between two pointer positions and each constraint's two world endpoints, then run a segment-intersection test (orientation signs). If they cross, remove that constraint.

xOffsetA, yOffsetA, xOffsetB, yOffsetB: where each link attaches, as fractions of the body's width and height from its centre. 0.5, 0 and -0.5, 0 attach right edge to left edge.

Set mouseConstraint.collisionFilter.mask to 0 so it can't pick anything, and restore it (for example to 0xFFFFFFFF) to re-enable dragging.