Charlie KrugThe Build Log

← All posts

A car that topples forward can fool a genetic algorithm

Darwin's Garage breeds scrap-built cars in real 2D physics. The hard part is teaching evolution that falling forward is not driving.

A heap of metal can improve its score by falling nose-first. That is technically progress, but it is not the kind of progress I wanted evolution to discover.

Darwin's Garage breeds cars in the browser. Twenty-four randomly assembled vehicles tackle the same 720-meter course, the better ones become parents, and their descendants try again. Generation zero mostly flails. On the shipped seed, its best car reaches 58 meters; by generation 20, a descendant completes the course.

I built it because genetic algorithms are often presented as a fitness line creeping upward. That graph hides the interesting part: random variation plus selection can produce a competent design that nobody drew. Watching a lopsided polygon acquire useful wheels makes the mechanism much harder to mistake for magic.

The car is plain data

Each genome is a JSON-serializable description of a polygon chassis with five to eight vertices, plus wheels attached to some of those vertices. A wheel has a radius and torque. There is no physics object inside the genome, just numbers that can be copied and changed.

For each candidate, the app turns those numbers into Matter.js bodies and runs a fixed-time simulation. Tournament selection picks parents from the better performers. Crossover splices their chassis and wheel genes, while mutation nudges vertex positions, wheel attachments, radii, and torque. The result is not a canned sequence of increasingly car-like drawings. Every shape on the floor went through the same physics test.

Keeping the genome separate from the simulation also makes replays practical. A shared URL carries the complete genome, and a checksum rejects a link that was truncated in transit. The simulation uses a seed and fixed timesteps, so playback speed changes how quickly I watch a decided run, not the forces that decide it. The same data produces the same crash on another device, which is a surprisingly useful property for both sharing and debugging.

Fitness is where the opinion lives

Selection cannot tell a car from a clever accident. It only knows the number I give it. Horizontal distance is the main signal, with adjustments for broken chassis pieces and staying upright. The course supplies another part of the definition: a flat run-up gives barely functional cars room to move, rubble rejects shapes that merely topple once, and the final climb rewards designs that can keep traction.

This is the central tuning problem. If the score is too sparse, most random cars tie at zero and selection has nothing useful to amplify. If it rewards raw forward motion too generously, a tall chassis that falls once can beat a small car that genuinely drives. Mutation has a similar balance: too little produces near-clones, while too much erases useful structures before they can spread.

That is why the fitness curve is not just decoration. The best and average lines show whether the population is learning broadly or whether one freak result got lucky. All 40 generations are kept, so the slider can return to any earlier population and replay its best member or any of its less successful relatives.

There is no server behind this, and there is not yet a track editor or saved custom course. The evolution, physics, Canvas rendering, audio, and replay packing all run locally in one static app. That keeps a shared car self-contained, but v1 evolves against the course I shipped.

Try it

Open Darwin's Garage, let the first generations land, then drag the slider between generation zero and generation 20. Pick one bad car from the floor before replaying the best one, and watch which pieces of geometry actually survive the rubble. The genome, evolution, and simulation code is on GitHub.

Darwin's Garage is live. Free, in your browser, no signup.

This post is part of the build log: every app my automated factory ships gets written up here, honestly. Browse everything at apps.charliekrug.com. Comments are open below.

Comments

Loading comments…