Matter.js Slingshot Tower Knockdown Game — Free Physics Game Snippet

Matter.js Slingshot Tower Knockdown Game · Games · Plain HTML, CSS & JS · Live preview

CategoryGames

What's included

Features

Spring constraint sling
Low stiffness with damping.
Release detection
Mouse up, past anchor, moving forward.
Automatic reload
Constraint re-attached to a new stone.
Drag restriction
startdrag rejects non-ammo bodies.
Pyramid tower
Composites.pyramid on a static platform.
Score tracking
Blocks knocked off and shots taken.
Custom sling drawing
Fork drawn in afterRender.
Off-screen cleanup
Old stones removed from the world.

About this UI Snippet

A Slingshot in Matter.js — Springs, Release Detection and Destructible Towers

Screenshot of the Matter.js Slingshot Tower Knockdown Game snippet rendered live

Pull back, let go, watch a tower collapse: slingshot games are the classic physics-engine showcase. The mechanics come down to three ideas — a spring, a way to detect the moment of release, and a structure worth knocking over — and each is a few lines of Matter.js.

The sling is a spring constraint

A Constraint with a very low stiffness (0.05) and near-zero rest length behaves like an elastic band: dragging the stone stretches it, and on release the constraint pulls the stone back toward the anchor with growing speed. damping stops it oscillating forever.

Detecting release

The engine's afterUpdate event checks three conditions every step: the mouse button is up (mouse.button === -1), the stone has passed the anchor, and it's moving forward fast. That is the moment it would snap back — so the constraint's bodyB is set to null, freeing the stone to fly. A new stone is created after a short delay and attached by setting bodyB again.

Only the ammo is draggable

MouseConstraint would happily let you pick up tower blocks. Its startdrag event checks which body was grabbed and releases anything that isn't the current stone.

A tower from a composite

Composites.pyramid builds a triangular stack of blocks from a callback that returns each body. The blocks rest on a static platform; a block counts as knocked off once it falls below the platform, and the HUD counts shots and knocked blocks.

Tuning

The stone's density controls its momentum — raise it for a heavier hit. The sling's stiffness controls launch speed. Block friction decides how easily the tower slides apart.

Housekeeping

Stones that fly off-screen are removed so the world doesn't fill with bodies.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Paste this game into an AI assistant like Claude and ask it to explain how the release detection works frame by frame. Ask it to add a trajectory preview dotted line while aiming, several levels loaded from JSON, target "enemies" that pop when hit hard (using collision impulse), or a limited shot count with stars. It can also help tune stiffness and density for a satisfying launch.

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 slingshot tower-knockdown game with Matter.js 0.20 (from a CDN) in plain HTML, CSS and JavaScript with a sky gradient background.

Requirements:
- A ground, a static wooden platform on a post, and a pyramid of 16 coloured blocks (7 columns wide) on the platform built with Composites.pyramid.
- A stone attached to a fixed anchor by a springy constraint (very low stiffness, near-zero length, small damping), with the sling fork drawn in afterRender.
- Mouse dragging limited to the current stone (reject other bodies in the startdrag event); remove the mouse wheel listeners so the page scrolls.
- In afterUpdate, detect release (mouse up, stone past the anchor and moving forward), detach the constraint, count the shot, and after a short delay create a new stone and re-attach the constraint.
- Count blocks that have fallen below the platform, show knocked-off, total and shots in a HUD, congratulate when the tower is cleared, remove stones that leave the scene, and add a New tower button.

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="sl">
  <div class="sl-hud">
    <div><b id="slKnocked">0</b> / <span id="slTotal">0</span> blocks knocked off</div>
    <div><b id="slShots">0</b> shots</div>
    <button type="button" id="slReset">New tower</button>
  </div>
  <div class="sl-stage" id="slStage"></div>
  <p class="sl-hint" id="slHint">Pull the stone back from the sling and let go.</p>
</div>

Step by step

How to Use

  1. 1
    AimDrag the stone back and down from the sling.
  2. 2
    ReleaseLet go; the sling launches the stone toward the tower.
  3. 3
    ReloadA new stone appears automatically.
  4. 4
    Clear the towerKnock every block below the platform.
  5. 5
    Try againNew tower resets the blocks and shot count.

Real-world uses

Common Use Cases

Casual web games
A complete mini-game loop.
Learning game physics
Springs and events in practice.
Promotional microsites
Playful, shareable interactions.
Teaching projectiles
Launch speed versus distance.
Prototyping
A base for levels and targets.
Related: Matter.js Physics Playground
Throw shapes around freely: Matter.js Drag-and-Throw Physics Playground.
Related: Matter.js Stacking Tower
Build instead of destroy: Matter.js Stacking Tower Game.

Got questions?

Frequently Asked Questions

Attach the projectile to a fixed point with a Constraint that has low stiffness and a tiny length, so it stretches like a spring. Let the user drag the projectile with a MouseConstraint, and when released past the anchor, set the constraint's bodyB to null so the projectile flies free.

In an afterUpdate handler, check that mouse.button is -1 (no button pressed) and that the projectile has passed the anchor while moving forward. That is the moment to detach it.

Listen to the MouseConstraint's startdrag event and, if the body isn't allowed, clear mc.body and mc.constraint.bodyB. You can also use collision filtering on the mouse constraint.

Use Composites.stack or Composites.pyramid with a callback that returns a body for each position, then add the composite (or its bodies) to the world.

Very fast, small bodies can tunnel through thin bodies between steps. Make targets thicker, lower the launch speed, or increase engine.positionIterations.