Matter.js Stacking Tower Game — Free Physics Stacker Snippet

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

CategoryGames

What's included

Features

Swinging drop mechanic
Pendulum position, handed to physics.
Momentum carry-over
Swing velocity applied on drop.
Stable stacking
Extra iterations, friction and sleeping.
Landing detection
Sleep state or low speed.
Rising camera
Eased render.bounds.
Lives and game over
Three missed blocks end the run.
Height guides
Lines every 5 m and a best marker.
Drop guide
Faint column under the hanging block.

About this UI Snippet

Building a Physics Stacking Game — Swinging Blocks, Landing Detection and a Rising Camera

Screenshot of the Matter.js Stacking Tower Game snippet rendered live

Stacking games look simple — drop a block, don't let the tower fall — but they rely on real physics: blocks that land off-centre tip, and a tall tower sways. Matter.js handles that, and this snippet adds the game layer around it.

The swinging block isn't a physics body (yet)

While it hangs, the next block is just a position animated along a sine wave across the top of the view, with a slight tilt that follows the swing. Keeping it out of the physics world means nothing can knock it. On drop it becomes a real body at the same position and tilt and falls straight down. Carrying over the swing's sideways velocity sounds realistic, but over a long fall it drifts the block far from where the guide pointed, which feels unfair — so timing is the whole skill, and the slight tilt adds just enough wobble on landing.

Stable stacking needs some tuning

Tall stacks of boxes are a stress test for any 2D engine. This snippet raises positionIterations and velocityIterations, uses high static friction and zero restitution, and turns on enableSleeping so settled blocks stop jittering. The swing speeds up as the tower grows.

Knowing when a block has landed

A block counts as landed once it's asleep or moving slower than a small threshold. Only landed blocks contribute to the tower's height, taken from the lowest bounds.min.y. A block that falls below the base platform or off its sides costs one of three lives.

A camera that climbs

With hasBounds, the renderer draws only what's inside render.bounds. Each frame the bounds' vertical position eases toward a target that keeps the tower's top in the lower half of the view, and the custom drawing (guide lines every 5 m, the best-height marker, the hanging block and its rope) uses the same offset through setTransform.

Controls

Click or tap the canvas, or press Space, to drop. A faint column shows where the block will fall.

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 the hand-off from animated block to physics body. Ask it to add perfect-drop bonuses when a block lands centred, wind that pushes the tower, different block shapes, a combo counter or sounds on impact. It can also help tune stability for very tall towers.

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 physics stacking tower game with Matter.js 0.20 (from a CDN) in plain HTML, CSS and JavaScript.

Requirements:
- A tall canvas with a static base platform; enable sleeping and raise position/velocity iterations.
- The next block (random width, rotating hue) swings across the top on a sine wave with slight tilt, drawn with a rope and a faint drop guide; it is not a physics body until dropped.
- Click, tap or Space drops it: create a rectangle body at the same position and angle (no sideways velocity) with high friction.
- A block is landed when asleep or slow; the tower height comes from the highest landed block. Blocks that fall off cost one of three lives; game over shows blocks and height.
- A camera using hasBounds that eases upward to keep the tower top in view, with dashed guide lines every 5 m and a gold best-height marker.
- A HUD with height, blocks, best and hearts, plus Restart; the swing speeds up as the tower grows.

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="st">
  <div class="st-hud">
    <div><span>Height</span><b id="stHeight">0 m</b></div>
    <div><span>Blocks</span><b id="stBlocks">0</b></div>
    <div><span>Best</span><b id="stBest">0 m</b></div>
    <div><span>Lives</span><b id="stLives">♥♥♥</b></div>
    <button type="button" id="stRestart">Restart</button>
  </div>
  <div class="st-stage" id="stStage">
    <div class="st-over" id="stOver" hidden>
      <strong>Tower down!</strong>
      <span id="stOverText"></span>
      <button type="button" id="stAgain">Build again</button>
    </div>
  </div>
  <p class="st-hint">Click, tap or press <kbd>Space</kbd> to drop the swinging block. Stack as high as you can — 3 dropped blocks and it's over.</p>
</div>

Step by step

How to Use

  1. 1
    Time the swingWatch the block swing on its rope.
  2. 2
    Drop itClick, tap or press Space.
  3. 3
    Keep it balancedOff-centre blocks tip the tower.
  4. 4
    Climb higherThe camera rises; the swing gets faster.
  5. 5
    Beat your bestA gold line marks your record height.

Real-world uses

Common Use Cases

Casual web games
A one-button game for any device.
Game jam starters
Add themes, power-ups or combos.
Learning Matter.js
Sleeping, bounds and custom rendering.
Marketing mini-games
Branded blocks for a campaign page.
Classroom demos
Centre of mass and balance.
Related: Matter.js Hill Climb Car
Another camera-follow game: Matter.js Hill Climb Car Game.
Related: Matter.js Slingshot
Knock towers down instead: Matter.js Slingshot Tower Knockdown.

Got questions?

Frequently Asked Questions

Increase engine.positionIterations and velocityIterations, use high friction and frictionStatic, zero restitution, and enable sleeping so settled bodies stop jittering.

Check body.isSleeping (with enableSleeping on) or treat body.speed below a small threshold as resting.

Create the renderer with hasBounds: true and move render.bounds.min.y and max.y each frame, easing toward a target based on the tower's highest point.

So nothing can collide with it while it hangs. On drop, create the body at the same position and angle and let it fall straight down so it lands where the guide shows.

Take the smallest bounds.min.y among landed blocks and convert the distance from the base top into metres with a pixels-per-metre scale.