webdevpuneet.com

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

Copy & paste

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>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

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.

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
Build the core mechanic in a few functions, where a swipe tests every constraint for segment intersection and removes the ones it crosses.
Constraint and chain learning
See how Composites.chain joins planks, how world-point constraints pin the cliffs and how stiffness changes the swing.
Interactive science illustrations
Give lessons hands-on physics, where learners cut a rope and immediately see the candy swing and fall.
Game jam starters
Add a goal, a character and levels around the swinging candy rope, which already includes a fading blade trail.
Line hit-testing reuse
Reuse the segment intersection test for any swipe-through interaction, such as slicing fruit or breaking connecting lines.
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.