The Ant Colony — 10,000 Ants, Pheromones, Emergent Highways
10,000 ants depositing pheromones on a 2D field. Optimal paths between nest and food emerge from pure local rules in under 30 seconds. No central pathfinding. Block a path and watch them re-route.
What this is
5,000–10,000 ants on a 2D field, rendered via WebGL2 instancing in a single draw call. Each ant has a position, heading, and a state (searching or returning). Searching ants smell food pheromone in three forward sectors and steer toward the strongest. Returning ants smell home pheromone. Both lay their own pheromone behind. Pheromones diffuse and evaporate each frame. Click to drop food. Shift-click to drop a nest. Drag to draw a wall. Within 30 seconds, the dominant trails are visible.
Why this is mind-blowing
There is no pathfinding code. No A*, no Dijkstra. Each ant looks at three pixels in front of it and turns toward the brighter one. From that single rule, repeated 10,000 times per frame, you get optimal paths between nest and food. Block a path with the poison tool — they re-route in seconds. This is what "emergent intelligence" looks like.
Build an ant colony optimization sandbox in one HTML file. 5,000–10,000
ants rendered via WebGL instancing at 60fps. Each ant follows pheromone
gradients and lays its own. Two pheromone types: home (laid by returning
ants) and food (laid by ants carrying food). Place food sources, place a
nest, watch optimal paths emerge from pure local rules in under 30
seconds. No central pathfinding. Sliders for evaporation rate, ant count.
Poison-path tool to draw walls and watch them re-route.
Paste this into Claude, Cursor, or Copilot. Change one thing that matters to you.
What I learned shipping it
- Two pheromone channels — one for 'this way home', one for 'this way to food' — is the entire algorithm. Marco Dorigo published it in 1992. The model knows it from training.
- CPU diffusion + texture upload is faster than GPU readback for fields under ~256×256. Avoid `gl.readPixels` in your render loop. The architecture choice IS the performance.
- Watching trails emerge is mesmerizing in a way 'the ant pathfinding algorithm computed an optimal route' is not. The emergence is the demo.