Reflection Madness — Marble Game With a Q-Learning Autopilot
A browser marble game on an endless procedural downhill course, where a tabular Q-learning agent learns to steer from raycast sensors. Built with Three.js and cannon-es.
Reflection Madness is a browser physics game in which a marble rolls down an endless, procedurally generated isometric course, and a small Q-learning agent plays it by default. It is worth a look as a compact, readable example of reinforcement learning applied to a continuous physics task without any ML library: the whole learner is a lookup table and a few hundred lines of JavaScript.


What it does
The marble starts at the top of a course built from tiled height columns, and new track is generated ahead of it as it rolls. Difficulty rises with distance: banked turns, switchbacks and plateaus come first, then wave ramps, split-route bridges and jumps over chasms, grouped into four stages in the HUD.
By default an autopilot drives. It reads a few sensors, picks one of six steering actions, falls, respawns and updates its Q-table as it goes. A telemetry panel reports position, speed, stage, lives, episode and fall counts, explore or exploit mode, the current state, Q-table size and cumulative reward. Pressing I, or clicking the chip icon, hands the marble to the keyboard (WASD or arrows, Space or Shift for turbo); doing it again returns control to the agent. A setting in the address bar starts in manual mode.
How it works
It is plain JavaScript on Vite, with Three.js for rendering and cannon-es for physics. There are no image assets; the retro grid and stripe textures are drawn on canvases.
Track generation. The course is built from 16x16 chunks with a seeded random generator. Each chunk is a grid of heights where an empty cell is a hole, and its first row copies the previous chunk's last row so the surface stays continuous. Seven motif builders fill the rest. The wave ramp, bridge and chasm appear once the course passes 120, 300 and 600 units. Chunks are created when the marble nears the last one, and the oldest is dropped once 12 are active.
Colliders. Every tile is drawn as a box column, but tiles don't each get a physics body. Neighbouring tiles of equal height are merged into larger static box colliders, which keeps the physics world small.
Marble. Input becomes a force plus a rolling torque. Linear and angular speed are clamped, a small downward force keeps the marble grounded, and it respawns when it falls 12 units below its last checkpoint.
Agent. Each frame the agent observes the angle to the next checkpoint, downward raycasts to the left and right to find cliff edges, a raycast 4.5 units ahead for gaps, and a speed bucket. These fold into a discrete state, eight angle buckets crossed with left hazard, right hazard, gap and speed, and it chooses among forward, steer left, steer right, hard left, hard right and brake. Selection is epsilon-greedy, starting at 0.35 and decaying by 0.985 per episode, and the update is one-step Q-learning with alpha 0.25 and gamma 0.90.
Rewards favour downhill progress on solid ground, reaching checkpoints and a moderate speed, and penalise driving toward an edge or gap. A fall costs -500 and returns the marble to the last spot where it sat centred on solid ground for over a second. After three falls the episode ends, epsilon decays, the table is saved in the browser, and the course regenerates with the next seed.
What's notable
- The learner is tiny and inspectable. The Q-table is a plain map keyed by state, saved and restored in the browser, and it can be exported or reset from the console. Watching the table grow in the telemetry panel makes exploration visible.
- The sensors are drawn in the world. The probe rays render yellow or cyan over solid ground and red over a drop, so a failure is visible the moment the agent loses a life.
- Collider merging. Visuals stay tile-based while physics uses merged boxes, a deliberate trade to keep the physics world small as chunks stream in.
- Continuous, seeded chunks. Shared seam rows and a seeded generator give continuous terrain that is reproducible per seed.
In the demo
The recording runs about 20 seconds. The autopilot drives stepped switchbacks and banked turns with its probes visible and telemetry updating. The cursor then clicks the chip icon to switch the agent off, and a short W then A keypress rolls the marble by hand with the probes hidden. Clicking the icon again returns control to the agent, which falls, respawns at its last safe spot and carries on while the Q-table grows.
State
A working prototype, built between 9 and 28 September 2026 and MIT licensed. Known gaps:
- As shipped, the telemetry panel doesn't refresh: it looks for its data in the wrong place and stays at placeholder zeros. The demo connects the two with one line in the page console.
- A fresh browser starts with an empty Q-table, so in a short session the agent is still exploring, falls often and doesn't leave Stage 1. The later motifs and a well-trained policy are not shown in the recording.
- The scene is dark by design, with navy fog and background.