Tony Morinello

All work

Interactive

MarbleRunBuilder

Build a marble run piece by piece, then drop the marbles and listen to it play.

Role
Sole designer and developer
Built with
JavaScript, Canvas, Matter.js, Web Audio

Visit the site

Walkthrough: Marble Run Builder, silent, 5 steps
The Marble Run Builder start screen: eight track cards and Start Your Own, with colored shapes resting on the title and the cards.

Marble Run Builder is a physics playground for building marble runs in a browser. You drag pieces onto a board, drop marbles, and hear them hit bells and chimes on the way down. It’s laid out for a tablet held sideways. I made it for my son, and for kids in general.

The problem

My son loves real marble runs. A marble run is a toy you build, knock down and build again. On a screen, that takes physics that behaves the same way from one run to the next, touch controls where a drag always moves a piece, and a way to save a board you like.

What I built

The tray holds forty-one pieces in six groups: thirty-one kinds, plus mirrored versions of the ones that face one way. They run from ramps, funnels and loops to lifts, cannons, spinners, bells, xylophone bars and a windmill. The newest are a conveyor belt, a one-way gate, a flip-flop that sends marbles left and right in turn, and a goal cup that counts what lands in it. Pieces snap to a grid of 17 by 9 squares. Tapping a piece turns it, or changes its note if it’s a bell or a bar, and holding one that faces one way mirrors it. Dragging moves a piece, and dropping it back on the tray removes it. A tap in the sky drops a marble, in one of eight colors, up to twenty at a time, and marbles can be grabbed and flung.

There are eight built-in tracks to start from (Big Cascade, Zigzag Falls, Loop City, Round & Round, Music Mountain, Spin Factory, Fair Share and Melody Lane), or an empty board. Once a player has built something, the start screen also offers to pick up where they left off.

Every piece is a physics body in Matter.js. Sound comes from the browser’s audio engine; there isn’t a single audio file. Boards save on their own a second after each change, and into named runs with thumbnails when a player wants to keep one. A saved run can be renamed, copied, saved over or deleted, and the last five boards a player moved away from wait under Recent autosaves. If a run can’t be saved because storage is full, the game says so and suggests deleting an old save. Turned to portrait, the game pauses its physics, quiets the sound and asks for the tablet to be turned sideways.

Undo and redo reach back ten changes, and opening a track or a saved run can be undone too. A run can be paused, or restarted with fresh marbles. Share this board turns the whole board into a link that opens it on another device. A first-time player gets five short tips over the board, each ticked off by doing it, and How to play on the start screen brings them back.

The game looks like the toy at the top of this site: flat shapes in bright colors, outlined in the dark of the stage they sit on. Its start screen has a copy of that toy, running on the game’s own physics engine. Served over HTTPS, it can be added to an iPad’s Home Screen and played offline.

Key decisions

Fixed-step physics, apart from drawing

The simulation steps at a fixed 120 times a second, separate from drawing, so a slow frame doesn’t change how a marble falls. Each frame runs at most four steps. After a long stall the game drops the time it couldn’t simulate instead of trying to catch up all at once. The cost is that the run slows down for a moment during a stall.

One fixed size, scaled to fit

The game is laid out at 1133 by 744, an iPad mini’s landscape size in CSS pixels, and scaled to fill the window. Pinch-zoom and long-press menus are switched off so a drag always moves a piece. That rules out zooming the page, and portrait mode stops play.

Sound made in code, with a cap on voices

Every sound is synthesized with Web Audio, and the audio starts on the first tap, which iOS requires. Rolling sound comes from a pool of eight voices, handed each frame to the loudest marbles, with pitch and volume following each marble’s speed. Impacts get ten voices and chimes six, and past those caps a hit goes unheard. Everything runs through a compressor, so a board full of marbles can’t clip. The caps keep a pile-up from rattling like a machine gun, and they keep the audio work from growing with the number of marbles.

Notes that sound good in any order

Tapping a bell or a xylophone bar steps it through six notes: C, D, E, G, A and the C above. That’s a pentatonic scale, so any order a child sets them in sounds good together. Each note also has its own color and number of dots, so the notes can be told apart without color.

Saves stay in the browser

Boards are saved to the browser’s local storage as documents with a version number. A failed write is caught instead of breaking the game. Every board goes through the same check before it loads, whether it comes from a save or a link, so a damaged one can only fail to open. The cost is that a saved board lives on one device, in one browser. A share link is the way to move one: it carries the whole board, five characters a piece, and each kind of piece keeps its letter for good, so old links keep working.

A closer look

The start screen offers eight built-in tracks and Start Your Own, with shapes from the toy resting on the title and the cards.

On Round & Round, marbles run the loop, ride the lift and fly past the cannons.

Pieces dragged from the tray snap onto the grid. The tray shows one group at a time, so the second loop comes from Curves. This course uses ramps, straight track and two loops.

A tap in the sky drops a marble onto the new course. Here one marble is climbing each of its two loops.

The save window stores the course as Marble Run 1, with its thumbnail and the date.

How I checked it

A debug overlay, switched on with the D key or by adding ?debug to the address, shows frame time, the number of physics bodies and marbles, and how many rolling sounds are playing. The page also exposes the world, the board and the save module to the browser console as a handle for debugging and testing. Every built-in track also runs headlessly in Node. It has to do what it was built to do, such as reach every piece, never stall, or send marbles into both goal cups, and it has to match a recorded trace of where every marble was. A change that moves any marble by a hundredth of a pixel fails that check. The walkthrough on this page is a real recording of the game, made from a local copy. The board holds 20 marbles at most; dropping another removes the oldest. I’ve tested it on an iPad.