Visualizations
301 / 500

301 · Machine learning

Ghost Racers

Sixty neural-net cars evolve to race a track, then face one you draw.

Each car sees through seven range sensors plus its speedometer, and a tiny network (8 inputs, 10 tanh units) outputs steering and throttle. A genetic algorithm scores each car by how far it gets in 20 seconds, keeps the best six and breeds the rest by tournament selection, uniform crossover and Gaussian mutation, turning mutation up when progress stalls. Evolution runs headlessly several generations a second while the latest generation is replayed as sixty rank-colored ghosts, with the champion's sensor rays and live network on top and every replay's trails piling into a long exposure. The track is a centerline in a distance field, so a sensor ray is a few sphere-tracing steps, a crash is one lookup, and any loop you draw becomes a valid track instantly.

Try it. Draw a closed loop to build your own track: the current population is tested on it first (its zero-shot score shows whether the champion generalized or overfit) and then keeps evolving there. N makes a new random circuit, R restarts evolution from scratch and F speeds up the replay.

  • Neuroevolution with a genetic algorithm
  • Sphere tracing a distance field
  • Long-exposure trails
  • Catmull-Rom and Chaikin track smoothing

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a top-down racing game where cars teach themselves to drive by neuroevolution, with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a closed track: pick about 12 points around an ellipse at random radii, smooth them into a loop with a Catmull-Rom spline, and give the road a fixed width. Draw it as a thick dark stroke with bright edges and a start line.
- A car has a position, heading and speed. Each step it casts five rays (ahead, plus or minus 35 and 80 degrees) and measures how far each goes before leaving the road, meaning more than half the road width from the centerline.
- The driver is a tiny neural network: the five ray lengths and the speed go in, 8 tanh hidden units, two outputs for steering and throttle. Its weights are one flat array.
- Run 50 cars at once from the start line. A car stops when it leaves the road or makes no progress for a couple of seconds. Its fitness is how far along the track it got.
- When every car has stopped, keep the best five unchanged and fill the rest of the next generation with mutated copies (add small Gaussian noise to some weights) of cars picked from the top half.
- Draw the cars, highlight the best one, and show the generation.

Once that works, make it beautiful:
- Draw the best car's rays and color them by distance.
- Leave fading trails so you can see the racing line form across generations.
- Run several generations per frame without drawing them, and replay only the latest one.
- Let me draw my own loop with the mouse and see whether the champion can drive a track it has never seen.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as a distance field for much faster ray casting, crossover between parents, or training on several tracks at once to stop overfitting.
PreviousDemoparty IntroA one-minute demoscene production, beat-synced to a tune synthesized from code. NextDancing LinksKnuth's Algorithm X tiles boards with wooden pentominoes while its links visibly dance.

Related visualizations

  • Evolution Strategy LandersMachine learning Fifty ghost landers learn soft touchdowns by natural evolution strategies.
  • CPPN GardenMachine learning Breed images grown by tiny neural networks by picking the ones you like.
  • Neural EcosystemEmergence Herbivores and predators with tiny neural brains evolve in a pastel petri dish.
  • Growing Neural CellsMachine learning Cells running one tiny learned rule grow a gecko from a single seed and heal its wounds.
  • Evolving WalkersMachine learning Creatures learn to walk by evolution, one generation at a time.
  • Drift RacerGames Pacejka tire physics on a generated circuit, with a drifting AI that plans ahead.
  • NEAT SpeciesMachine learning Networks grow their own topology to balance two poles, while species rise and fall.
  • Symbolic RegressionMachine learning Genetic programming breeds formulas until it rediscovers the law behind the data.
  • Optimizer Topo RaceMachine learning SGD, Adam, Lion and friends race downhill across a vintage topographic map.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs