Cannon Fair is a free cannon smash game that runs in the browser: tap where you want to hit, and a toy cannon knocks cans, crates and ice off a carnival stall. It’s the sixth game in our series of small Three.js games, and the first built on a physics engine instead of grid rules. This post covers the physics setup, how a tap becomes a shot, how ice and explosive barrels work, and how we checked that every level can be cleared.
Three.js for drawing, cannon-es for physics
Three.js draws the scene, and cannon-es, a small JavaScript physics engine, moves everything. Each target is a rigid body: a cylinder for cans and barrels, a box for crates, planks, ice and stone. The platform and any pedestals are static bodies with zero mass. Every frame, each mesh copies the position and rotation of its body.
All the physics lives in one module with no Three.js in it. The game, the level builder and the verification script all import it, so they run exactly the same rules.
A fixed time step
Physics engines give different results with different step sizes, and browsers draw frames at different rates. So the game never steps physics by the frame time. It adds the frame time to an accumulator and takes as many fixed 1/60-second steps as fit, carrying the remainder to the next frame. A slow phone takes more steps per frame and a fast screen takes fewer, but the simulation itself is the same.
Towers that stand still
A stack of a dozen cans placed exactly on top of each other should stand perfectly still, but a physics engine settling contacts can make it wobble or slowly slide apart. The fix is to put every body to sleep when the level loads. Sleeping bodies don’t move until something touches them, so a tower stays put until a ball arrives. Once hit, bodies wake up, tumble and go back to sleep when they slow down.
One surprise: with the sweep-and-prune broadphase, a ball fired into a stack of sleeping bodies passed straight through without touching them. The simpler broadphase, which checks every pair, worked first time. With at most a few dozen bodies, checking every pair costs nothing noticeable.
A tap becomes a shot
When you tap, the game casts a ray from the camera through that point. If it hits a target, the platform or a pedestal, that hit point is where the ball should go. If it hits nothing, it uses a plane through the middle of the stall.
Then it works out a launch velocity that passes through that exact point. The flight time is the distance divided by a fixed ball speed. From there the velocity is the displacement divided by the time, plus an upward part that cancels out the drop from gravity over that time. The result is a shot that lands precisely where you tapped, with a gentle arc that gets higher for faraway targets.
Ice, barrels and clearing
Ice blocks and barrels listen for collisions. Each contact reports how hard the two bodies met, and when that passes a threshold, the block is marked to shatter or the barrel to explode. These events are handled after the physics step finishes, because removing bodies in the middle of a step causes trouble.
An explosion pushes every body within a few metres away from the barrel, harder the closer it is, with a little extra upward lift. It wakes everything it touches, and a second barrel in range goes off too, which is how a chain reaction clears a whole stall.
A target counts as cleared once it drops below the platform, or as soon as it shatters. The level is won when none are left, and lost when the last ball has landed, everything has settled and targets remain.
Checking every level can be cleared
Physics levels can’t be solved by working through every possible move, the way our grid puzzles can, because there are endless places to aim. Instead, the level builder runs a search in Node.js, using the same physics module.
For a given run of shots, it replays the level from the start, firing each shot once the scene settles. Then it tries a set of candidate aim points: the centre of each remaining target, plus a slightly lower point on tall ones, lowest first. It keeps the few runs that leave the fewest targets standing and goes one shot deeper. The first run to clear the platform sets the level’s best, which is the target for three stars. The player gets two or three more balls than that.
A verification script replays each stored run and checks it still clears the level in exactly that many balls. Then a headless browser plays all 20 levels through the real game. Because players fire at different moments, the same shots don’t always land identically in the browser, so the test fires the stored shots and then keeps aiming at whatever’s left, as a player would. Every level was cleared within its ball limit.
The carnival look
The stall is a wooden top on red and white stripes, on a pier post over the sea. Behind it are a beach, striped tents, palm trees and a slowly turning ferris wheel, all built from simple low-poly shapes. Cans get bright colours with metal lids, crates get dark bands and outlined edges, ice is a see-through pale blue, and barrels are red with gold bands. Shattering ice throws out small shards, explosions throw sparks and shake the camera, and the cannon recoils with a puff of sparks on every shot.
The camera sits just behind and above the cannon, and its field of view widens until the whole stall and the tallest stack fit the screen, so wide stalls work on phones held upright.
You can see the result by playing Cannon Fair.