webdevpuneet.com

Matter.js Stacking Tower Game — Perfect Drops and Wind

Matter.js Stacking Tower 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), 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

Real pendulum swing
Position from sin and cos of an angle, with accumulated phase so speed changes never jump.
Perfect combos
A centred, level landing earns 25 points times the combo, with a glow and sparks.
Wind
Gusts push falling floors only, shown by a meter and a leaning drop guide.
Stable stacking
Extra solver iterations, high friction, zero restitution and sleeping bodies.
Rising camera
Eased render.bounds keeps the tower top in view as it grows.
Dusk to night sky
Gradient, twinkling stars and moon change with altitude, painted with destination-over.
Lives, score and best
Three lives, floors and combos scored, and the best saved in localStorage.
Hand-drawn floors
Gradient floors with lit windows that narrow as the tower grows.

About this UI Snippet

A Physics Stacking Game in Matter.js — Pendulum Drops, Perfect Combos, Wind and a Rising Camera

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

Stacking games look simple: drop a block and don't let the tower fall. Underneath they depend on real physics, because floors that land off-centre tip and a tall stack sways. Matter.js handles that, and this snippet adds the game around it: a pendulum that swings each new floor, a Perfect-landing combo, wind that pushes falling floors sideways, lives, a score and best saved in localStorage, particles, and a camera that climbs from dusk into a starry sky. It is all plain JavaScript on one canvas.

A pendulum that is not a physics body

While it hangs, the next floor is only a position: an angle that follows a sine of phase, converted to x = centre + sin(theta) * length and y = pivot + cos(theta) * length, with the floor tilted against the swing. Keeping it out of the physics world means nothing can knock it. The phase is accumulated each step, so speeding the swing up as the tower grows never makes the floor jump, and the swing gets wider as well as faster.

Dropping honestly

On click, tap or Space the floor becomes a real rectangle at the same position and tilt and falls straight down. Carrying over the swing's sideways speed looks realistic but drifts the block far from the faint drop guide over a long fall, so timing is the skill. The fall speed is also capped (MAX_FALL, 6px per step): a long drop at full speed hits the stack hard enough to shake a tall tower apart. Floors also get narrower as the tower grows, down to 70px.

Perfect landings and combos

Once a floor is asleep, or slow after the first 30 steps, it counts as landed and judge() compares it with the floor beneath (or the base). Within 8px of centre and nearly level it is Perfect: the combo rises, the floor glows, gold sparks fire and the bonus is 25 times the combo on top of 10 points. Any other landing resets the combo and kicks up dust.

Wind

From the fifth floor, a gust target is picked every few seconds and the wind eases toward it. It is applied as a small Body.applyForce to floors that have not landed, so it only matters while a floor is falling; the drop guide leans with the gust and a meter in the corner shows the strength. Wind never touches landed floors, which keeps tall towers stable.

Stable stacking

Tall stacks are a stress test for a 2D engine, so the snippet raises positionIterations and velocityIterations, uses high static friction and zero restitution, and turns on enableSleeping so settled floors stop jittering. A floor that falls below the base platform or off its sides costs a life.

A camera that climbs

With hasBounds, the renderer draws only what is inside render.bounds, whose vertical position eases toward the tower top. Everything else is painted by hand in afterRender: floors with lit windows, the podium, height lines every 5 m, and the background last with destination-over, where the sky fades from dusk to deep space and the stars twinkle more with altitude.

Step by step

How to Use

  1. 1
    Press Start buildingThe card explains the goal. Press the button or Space to begin, and the first floor swings on its rope above the tower.
  2. 2
    Time the dropClick, tap or press Space. The floor falls straight down; the faint column below it shows where it will land.
  3. 3
    Land it centredWithin 8px of the floor below and level counts as Perfect: a glow, gold sparks and a combo bonus that grows with each one.
  4. 4
    Watch the windFrom the fifth floor the wind meter appears. A strong gust pushes the falling floor sideways, so aim against it.
  5. 5
    Keep your livesA floor that falls off the base costs one of three hearts and resets your combo. Climb as the camera rises into the stars.
  6. 6
    Beat your bestYour best score is saved on this device, so there is always a record to beat.

Real-world uses

Common Use Cases

Casual web games
Ship a one-button game for any device: a pendulum drop, Perfect combos and a saved best score give it a reason to replay.
Game-jam starter
Build on the existing lives, wind and combos with power-ups, themes or block shapes instead of starting the loop and camera from scratch.
Matter.js stability lessons
Study the combination that keeps tall stacks still: extra iterations, friction, zero restitution and sleeping, with landing detection on top.
Branded mini-game
Swap floors for your product boxes or logo blocks, the skyline for your brand colours, and the results card for a call to action.
Balance and centre-of-mass demos
Show why off-centre blocks tip, using the Perfect threshold and the narrower floors to make the effect easy to see.

Got questions?

Frequently Asked Questions

Raise engine.positionIterations and velocityIterations, use high friction and frictionStatic, zero restitution, and enable sleeping so settled bodies stop jittering. Keep wind or other forces off bodies that have already landed.

So nothing can collide with it while it hangs. On drop you create the body at the same position and angle and let it fall, which keeps the drop guide honest. Accumulating the pendulum phase also lets you change the speed smoothly.

When a floor lands, judge() finds the landed floor directly below it, or the base, and compares x positions. A difference under 8px with an angle under 0.08 radians is Perfect, which raises the combo and adds 25 points per combo level. Other landings reset the combo.

Every step, falling floors get Body.applyForce with a horizontal force of wind times mass times a small constant. The wind value eases toward a new random target every few seconds. Landed floors are skipped, so towers do not slide apart.

Create the renderer with hasBounds: true and move render.bounds.min.y and max.y every frame, easing toward a target based on the tower's highest point. Draw custom graphics with the same vertical offset through setTransform.