What it does
You create a rope for a body with game.ropeSwing.create(world, body, options), giving it the surfaces it may hook and
wrap round, the same shapes as the world's static bodies. fire(target) sends the rope towards a point and hooks the
first hookable surface along the line within range. The body then hangs on a one-sided joint to the rope's last bend,
so the rope pulls only when it is taut: the body swings as a real pendulum, and flies on with its momentum when you
release(). reel pulls it in or lets it out at a speed you set.
As the body swings, the rope wraps round the corners of the convex surfaces it sweeps past and unwraps as it swings
back, so it can loop round a pillar and hang from the far side. That is the classic ninja rope. Turn wrapping off and
it is a straight grapple arm instead, which lets go when terrain crosses it; give it a mantle reach and a hook on the
top of a ledge climbs the body up over the edge.
Call step(dt) after each world step. The rope reports what happened as events (hook, miss, wrap and unwrap with the
corner, release, jam, mantle), and line() returns its polyline from the hook through each bend to the body, for your
game to draw.
Features
- Fire and hook — the first hookable surface along the line within
range; a miss lets go of the old rope. - A real pendulum — the body swings on the physics world and keeps its momentum on release.
- Corner wrapping — wraps round convex surfaces and boxes as it swings and unwraps on the way back, up to
bendscorners. - Reel in and pay out — at
reelSpeed, down to theshortestlength, with paying out optional. - Mantling — with a
mantlereach, a hook on a ledge's top climbs the body over it inmantleTime. - Two presets —
ninja(range 420, wraps, pays out) andgrapple(range 340, straight, reels in only, mantles within 64). - Events and a line — everything that happened since the last
drain(), and the rope's polyline to draw. - Geometry helpers —
segmentEntersandsurfaceCorners, the tests it uses itself.
Good for
- Rope-swinging platformers and courses — swing across pits and gaps;
ninja-rope-2dis a course game built on it. - Artillery games with a ninja rope — a rope that wraps round the terrain, in the style of Worms.
- Grappling-hook action games — fire, reel in and pull up onto ledges with the grapple preset.
- Climbing games — reach a ledge, mantle over it, and fire again.
Make it yours
Each rope takes its options when it is made; the pack has none of its own. Start from a preset and change any part:
range, shortest, reelSpeed, wrap, payOut, mantle, mantleTime and bends, plus the surfaces it may
hook, any of which can be marked as not hookable.
Two different uses: a ninja rope with wrapping and paying out on and a long range, for a character swinging round a
cave of pillars and letting out rope to drop lower; and a grapple arm with wrapping off, reeling in only, and a
mantle reach of 64, for a platformer hero who hooks the top of a ledge and climbs onto it.
A quick look
A ninja rope fired, reeled with the keys, stepped after the world and released:
const rope = game.ropeSwing.create(world, runner, { ...game.ropeSwing.presets.ninja, surfaces });
rope.fire({ x: aimX, y: aimY }); // hooks the first hookable surface along the line, within range
game.step('rope', dt => {
rope.reel(keys.up ? 1 : keys.down ? -1 : 0, dt);
world.step(dt);
rope.step(dt); // after the world: wrap, unwrap, set the length
for (const e of rope.drain()) if (e.kind === 'wrap') clink();
});
rope.release(); // flies on with its momentum
Works with
physics-2d— the world and the body the rope swings: a headless world, or the game's own.ninja-rope-2d— rope-swinging courses with gates, hazards and a goal, built on this pack.physics-rope— for ropes with weight that hang, drape and are cut.
Good to know
- The rope is straight between bends and has no weight of its own: it is a tether, not a hanging rope, which is
physics-rope. - Wrapping is found from the corners the rope sweeps over in a step, so a rope swinging very fast past a thin surface can wrap a step late.
- Surfaces are convex polygons and boxes.
- The body is pulled straight towards its last bend: it does not slide round a corner it touches; the rope does.






