I built this because I wanted to watch ants do ant things. There was no product brief and no practical destination. Simulating the natural world is simply fun: a few understandable rules can get close enough to a complicated behavior that something surprising appears on screen.
The finished colony has dozens of ants, two food sources, fading signals, obstacles, and three ways to view the same world. Starting there asks the reader to decode everything at once. So this version begins with one ant and one question: what has to be true before a trail can exist?
A useful path begins as an accident
Start with one ant in an empty field. It needs enough forward motion to travel, enough variation to explore, and enough boundary pressure to stop turning the edge into an infinite treadmill.
Movement before memory
INTERACTIVE- Movement before memory +
Drag the ant near a wall and release it. Boundary correction should bend its heading before the wall becomes a treadmill.
Accessible experiment transcript
A single ant integrates forward motion, bounded turning, and an inward edge force before pheromones exist. Canvas gesture: drag the ant to an edge, then release.
This fixes locomotion, but the result is still obviously broken. The ant can pass beside food without reacting. Even when it collides with food by chance, nothing in its movement policy can use that discovery again.
The first missing piece is not colony intelligence. It is local perception.
A smell can bend one path
Give the ant five samples in a fan: hard left, soft left, forward, soft right, and hard right. Each sample reads the relevant local field, subtracts edge risk, and keeps a small forward preference. The best valid sample becomes a steering target.
Five samples, one steering target
INTERACTIVE- Movement before memory ✓
- Five local samples +
Drag the signal around the ant. The strongest sample should change immediately while the current heading catches up gradually.
Accessible experiment transcript
Five forward samples compare a synthetic pheromone slope; the strongest valid sample becomes a gradual steering target. Canvas gesture: drag the signal around the ant.
Pheromone priority is a state switch. An outbound ant reads the food field. An ant carrying food reads the home field. If the actual food or nest is close enough to sense directly, the physical target overrides both fields.
The best ray wins only if it clears the temperament’s confidence threshold. If no sample is convincing, the ant keeps exploring. This is already more purposeful than the empty-field ant, but the improvement reveals the next failure: there is nothing to sense until a discovery leaves something behind. One ant can learn nothing from another ant’s lucky trip.
The ground becomes the colony’s notebook
A searching ant now deposits home evidence as it travels. Once it picks up food, it follows that evidence back and deposits food evidence in the opposite direction. The route is not stored inside any ant. It survives temporarily in the world.
A private trip becomes public memory
INTERACTIVE- Movement before memory ✓
- Five local samples ✓
- A trip becomes evidence +
Draw a trail across the field. Each mark should widen, fade, and eventually disappear instead of becoming permanent advice.
Accessible experiment transcript
A successful trip deposits evidence that spreads to neighboring cells and fades with age. Canvas gesture: draw a trail; watch it spread and fade.
Update one patch of ground, then repeat the same operation across the field.
- 01 Read the current memoryP at time t is the signal already stored at this location.
- 02 Share it with nearby groundThe diffusion term moves a fraction toward neighboring cells. D controls how widely it spreads.
- 03 Let old evidence fadeMultiplying by one minus rho removes a small fraction. A larger rho means faster forgetting.
- 04 Add what happened nowDelta P is the fresh signal deposited by ants during this update.
- 05 Use the result nextThe combined value becomes P at time t plus one—the field the ants read on the next step.
Decay is doing real work. Permanent marks would fill the world with obsolete recommendations. Instant evaporation would make one ant’s luck useless to the next. Useful shared memory lives between those failures.
The loop is small:
- leave home and deposit a way back;
- wander until food or useful food evidence is nearby;
- carry one piece home while reinforcing the successful route in proportion to its value;
- unload, turn around, and search again.
No ant owns the route. The route exists only while enough trips keep confirming it.
A working route is not necessarily a good route
Shared memory creates another visible flaw: the first route that works can dominate even when it wanders. The simulation therefore measures each completed trip against the direct distance between food and nest. Efficient trips deposit more useful evidence than long detours.
Repeated shortcuts become preferred
INTERACTIVE- Movement before memory ✓
- Five local samples ✓
- A trip becomes evidence ✓
- Short routes reinforce faster +
Click either corridor to complete another trip. Its evidence and the probability of choosing it next should rise together.
Accessible experiment transcript
Two valid corridors compete. Repeated efficient trips make the shorter route increasingly likely without a global map. Canvas gesture: click a route to complete another trip.
The shorter corridor does not win because an ant can calculate the global shortest path. It wins because it completes more reinforcing trips in the same time and each efficient return contributes a stronger deposit. This follows the useful idea behind nonlinear trail choice—stronger evidence should become disproportionately persuasive—without pretending the model recreates any one species exactly. (Beckers et al., 2003)
The route now improves itself, but it still trusts old success too much. Move the food or place an obstacle across the corridor and ants can keep following a strong signal into nothing.
A strong smell is not proof
The first version had no explicit answer to a false-positive trail. An ant would keep selecting whatever food signal remained until evaporation or random motion finally freed it. Severing a route exposed the same weakness, and the field edges made it worse: position was clamped at the border while the ant took too long to turn, so it could pace along the wall indefinitely.
The new policy exits early. Each ant carries a short spatial memory and a small commitment ledger: how long it has followed this signal, the strongest evidence recently seen, and whether it is revisiting the same patch. A collapsing signal, a loop, an overlong commitment without finding food, an obstacle, or an edge contact triggers recovery. The ant turns away, temporarily ignores attractive trails, and leaves a short-lived purple doubt mark. Other ants subtract that local warning from their five ray scores.
Turn trusted advice into doubt
INTERACTIVE- Movement before memory ✓
- Five local samples ✓
- A trip becomes evidence ✓
- Short routes reinforce faster ✓
- Bad advice must expire +
Drag the obstacle onto the trusted trail. With recovery enabled, the ant should exit early and leave doubt near the dead end.
Accessible experiment transcript
A trusted trail ends at an obstacle. Declining evidence triggers a turn, and a temporary doubt mark discourages the same dead end. Canvas gesture: drag the block across the trusted trail.
That mechanism is inspired by real negative feedback, not copied wholesale. Pharaoh’s ants can place a “no entry” signal at unrewarding branches, while other ant systems reduce positive pheromone deposition under bad local conditions such as crowding. (Robinson et al., 2005; Czaczkes et al., 2013) Here, one fast-decaying doubt field gives the experiment a visible way to say, “this advice stopped working.”
Edges now participate in the same logic. The sensing fan assigns increasing risk before a ray leaves the habitat, steering the ant inward before contact. If contact still happens, its heading reflects immediately and the boundary receives a doubt mark. It no longer waits against the wall for a slow turn to finish.
At this point every addition answers a failure created by the previous one. The remaining question is whether those local fixes still cooperate when dozens of ants, multiple food values, moving targets, and obstacles share one field.
The pattern I did not draw
My favorite moment in the original project was what I started calling “laminar flow.” I do not mean the code became a fluid simulation. I mean that many jittery, private decisions began to look like a coherent stream between food and nest.
I had written movement, sensing, pickup, return, and decay. I had not drawn that stream. Seeing it emerge was the payoff.
Tuning the rules felt less like polishing constants and more like giving the colony a temperament. A disciplined colony trusts signals and settles quickly. A curious colony wanders farther and keeps alternatives alive. The adaptive setting sits between them. Trail persistence still controls memory, but temperament controls how readily an individual believes it. The same fascination is why I later built boids: simple agents become interesting when their local rules overlap.
Why keep the old flat view?
The isometric habitat makes the colony feel alive, but it also hides information behind perspective. The original top-down idea remains the clearest way to inspect density, direction, and the full trail at once. I kept it as an alternate experience instead of replacing it with a prettier camera.
That distinction matters to me: a visualization should answer a question. The orbitable world answers, “What would this little system feel like?” The signal view answers, “What information are the ants changing?” The map answers, “Where is the pattern actually forming?” One simulation can support all three without pretending one camera is universally best.
Rebuilding the source UI without multiplying the work
The simulation now keeps behavior separate from presentation. A seeded colony engine owns the ants, food sources, obstacles, delivery value, and two diffusing pheromone buffers. Three.js renders the habitat with one orthographic camera, orbit controls, instanced ant bodies, legs, antennae, cargo, sharp labels anchored inside the world, and a point cloud for the signal field. The alternate canvas renderer reads those same objects into the flat map.
The article also mounts only one iframe for all three figures. As you scroll, that iframe moves to the active slot and receives a view message. Offscreen work pauses. On smaller or resource-constrained devices, the reading preset lowers pixel density and caps the population before it removes behavior. That keeps the “same colony” claim literal and avoids running three animation loops just to tell one story.
The older Svelte port experimented with typed entities and a quadtree for spatial lookup. This rebuild uses fixed-size trail buffers for the environmental memory and instancing for repeated ant geometry. Those choices fit the questions this version asks: sample a local field cheaply, keep many agents visible, and make the trail itself legible.
Break a good route on purpose
Once a route works, the most revealing thing to do is interrupt it. Block busy trail places a log or rock across the strongest current corridor. The obstruction masks the pheromone field beneath it, steers ants away from its footprint, and turns yesterday’s good advice into a dead end.
That makes reorganization visible rather than theoretical. Ants pile up briefly, exploratory trips become useful again, and a competing corridor begins to collect enough reinforcement to take over. Up to three obstacles can be added before the control clears the field for another experiment.
The closer animation serves the same explanation. Legs cycle while an ant travels, antennae search ahead, cargo rises during pickup, and it lowers into the nest during unloading. I kept those gestures compact so the group remains readable when dozens of ants share a trail.
There is still plenty to explore. I would like to compare temperaments quantitatively, add colony-to-colony competition, and make the obstacle tools more spatially editable. For now, the lab reaches the part I originally cared about: ants discover food without a route, turn repeated trips into shared evidence, and recover when that evidence stops being useful.
Put the pieces together
The capstone below recombines the controlled examples. These are three windows onto one running colony: the orbitable habitat, the environmental memory field, and the original top-down map. Moving food or blocking a route in one window changes the others.
Run the complete colony
ORBIT / LIVEInspect the colony’s public memory
MEMORY / LIVEFlatten the same session into a field map
2D FIELD / LIVEWhat this project taught me
There is a limit to how far I can predict emergent behavior from reading the rules. I can define the agents, their information, the boundaries, and the feedback. I can make a hypothesis about what the group will do. I still have to run it and watch.
That is what I want this project to say about me. I like building systems whose parts remain understandable while their interactions create something I did not script frame by frame. Randomness is not always an error to remove. Sometimes it is the first ant that finds the food.
The expanded source lab keeps Habitat, Signals, and 2D Field available together. The repository contains the simulation engine, both renderers, and the archived experiments that led here.