webdevpuneet.com

Matter.js Slingshot Game — Levels, Critters and Aim Preview

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

CategoryGames

For supporters

Source Code

Want the source code?

Play the game for free. Become a WebDevPuneet supporter to unlock the complete HTML, CSS and JavaScript source code, the exports (HTML, React, Vue, Angular, Tailwind) and Fork.

Play freeSource code: Patreon

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month, 7 days free), 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

Exact aim preview
Velocity is set from the pull, so the dotted path replays the engine's gravity and air friction and matches the shot.
enddrag release
A release with at least 28px of pull launches the stone; smaller pulls return it to the sling.
Wood, stone and glass
Three densities and frictions, with glass shattering above an impact-speed threshold.
Critters that pop
Impact speed from collisionStart decides whether a critter pops, worth 500 points with confetti and shake.
Three levels
A pillar frame, a two-tower fort and a stepped pyramid, each a function that returns bodies.
Stones and stars
A limited ammo supply, a 300-point bonus per unused stone and a three-star rating.
Hand-drawn scene
Bodies, band, fork, parallax hills and drifting clouds are painted in afterRender.
Clean-up and caps
Spent stones are removed and particles are capped so the world stays light.

About this UI Snippet

A Slingshot Game in Matter.js — Exact Aim Preview, Fragile Glass and Critters to Pop

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. This version turns the mechanics into a small game with three levels, a limited number of stones, green critters to pop, blocks of wood, stone and glass, a dotted aim preview that matches the shot exactly, star ratings and particle effects, all on one canvas with plain JavaScript and Matter.js.

An aim preview that is actually accurate

Instead of letting a spring fling the stone and guessing where it goes, the launch is computed. While you drag, the stone is held at your pointer up to 130px from the anchor. On release, Body.setVelocity gives it a speed of pull distance times a constant (POWER). Because that speed is known, the preview can replay the engine's own per-step maths: multiply by the air friction (1 minus 0.004), add gravity (0.2778 per step), move, and draw a dot every fourth step until the path meets the ground. The dots land where the stone will.

Release detection with enddrag

MouseConstraint fires an enddrag event when you let go. If the dragged body is the stone, the state is "play" and the pull is at least 28px, the spring is detached, the velocity is set and one stone is spent. A smaller pull just lets the spring return it. A new stone is loaded 900ms later with a short grow-in, and startdrag rejects every body that is not the loaded stone.

Three materials

Wood, stone and glass differ in density and fragility. Stone is four times heavier than glass, so it barely moves when hit. Glass shatters when it collides above a speed threshold (lower for another body, higher for the ground), spraying blue shards and giving 50 points, which makes glass floors a way to bring a structure down.

Critters that pop on impact

Each green critter is a circle that pops when something hits it hard enough: the relative speed from collisionStart must exceed 3.6 for a moving body or 7.5 for the ground, so a gentle landing is safe but a falling block is not. A pop gives 500 points, a burst of confetti and a short screen shake. The faces blink, and are drawn along with every other body by hand in afterRender.

Levels, stones and stars

Each level is a function that returns bodies placed relative to the platform: a pillar frame, a two-tower fort with glass and stone floors, and a stepped pyramid built with Composites.pyramid. A level is won when no critters remain and lost when the stones run out and the scene settles. Finishing with two or more stones left earns three stars, and every unused stone is worth 300 bonus points.

Housekeeping

Spent stones are removed once they leave the scene or have lain around for nine seconds, particles and floating text are capped, and the background, clouds and hills are painted last with destination-over.

Step by step

How to Use

  1. 1
    Press PlayA card explains the goal: pop every green critter before the stones run out. The stones left are shown as dots in the HUD.
  2. 2
    Drag the stone backPull it back from the sling, up to 130px. A dotted line shows the exact path the stone will take.
  3. 3
    ReleaseThe stone launches at a speed proportional to the pull. Pull further for more power and change the angle for a different arc.
  4. 4
    Use the materialsGlass shatters at a touch, wood holds, and stone is heavy. Knock out supports so the structure drops onto the critters.
  5. 5
    Clear the levelPop all critters to win, earning 500 each, 300 per unused stone, and up to three stars. Next level changes the structure.
  6. 6
    RetryRetry level resets the structure and restores the score from the start of that level.

Real-world uses

Common Use Cases

Complete mini-game
Start from a full game with levels, ammo, scoring and a results card, then reskin the critters, blocks and background for your own theme.
Teaching projectile motion
Because the dotted path replays the same gravity and drag the engine uses, students can see launch speed, angle and distance connected before they release.
Promotional microsites
Turn a campaign into a shareable game by swapping the critters for your mascot or product, and the results card for a discount or sign-up prompt.
Level design prototyping
Add a level as a function placing blocks relative to the platform, then test how materials and layout change the difficulty in seconds.
Learning Matter.js events
A compact reference for startdrag and enddrag, collisionStart with relative speed, custom rendering in afterRender and destination-over backgrounds.

Got questions?

Frequently Asked Questions

The launch velocity is set directly from the pull (distance times POWER), so the preview repeats Matter's own per-step update: multiply the velocity by one minus the air friction, add 0.2778 of gravity, then move. Plotting every fourth position until it reaches the ground gives a path that matches the real flight.

A spring's launch speed depends on stiffness, damping, mass and frame timing, which makes a trajectory impossible to predict exactly. Computing velocity from the pull gives predictable shots and an accurate preview, while the spring only holds the stone at rest.

Add an object to LEVELS with a stones count and a build function that returns bodies, using block(kind, dx, bottom, w, h) and critter(dx, bottom) to place things relative to the platform. The HUD, win check and star rating pick it up automatically.

In collisionStart the relative speed between the two bodies is measured. A critter pops above 3.6 against a moving body or 7.5 against the ground, and glass shatters above 4.2 or 6.5. Raise the numbers for sturdier objects.

Setting render.visible to false on each body and painting them in afterRender gives full control: faces on critters, grain on wood, cracks on stone and shine on glass. The polygon vertices from Matter are used for the shapes, so collisions and visuals stay in sync.