Slime Mold — Physarum Pattern Formation
March 2026
This page contains a working simulation of Physarum polycephalum foraging behavior. The branching networks you see forming aren't drawn — they emerge from simple rules followed by thousands of agents. Three species compete for trail space, each reinforcing its own chemical signals while ignoring the others. Watch as networks grow, connect, and self-optimize.
Live Demo
Terracotta, amber, and sage — three species forming parallel networks. Each follows only its own trail.
New: Enable obstacle drawing below, then click/drag on canvas to place barriers. Agents will route around obstacles.
Obstacles
The simulation now supports obstacles — barriers that agents cannot pass through. This mirrors real Physarum behavior, which can navigate mazes and route around physical barriers. When an agent encounters an obstacle, it bounces off and randomly redirects, exploring new paths. The result is networks that adapt to the environment, finding routes around obstacles rather than through them.
Obstacles also affect trail diffusion — chemical signals don't spread through walls, creating more distinct network structures that conform to the environment.
Obstacle Behavior
Agents use simple collision response:
if (isObstacle(newX, newY)) {
// Reverse direction and apply random turn
agent.x -= Math.cos(agent.angle) * MOVE_SPEED;
agent.y -= Math.sin(agent.angle) * MOVE_SPEED;
agent.angle += (Math.random() - 0.5) * Math.PI; // Random redirect
// Try several angles until finding a free direction
}
During diffusion, obstacle pixels are skipped — trails don't spread through walls:
for (let dy = -1; dy <= 1; dy++) {
for (let dx = -1; dx <= 1; dx++) {
if (!isObstacle(x + dx, y + dy)) {
sum += diffusionBuffer[...];
count++;
}
}
}
What Is Physarum?
Physarum polycephalum is a slime mold. It has no brain, no nervous system, no central controller. Yet it can:
- Solve mazes — find the shortest path between food sources
- Optimize networks — recreate efficient transport graphs (researchers placed food on Tokyo metro stations; Physarum grew a network matching the actual rail system)
- Remember patterns — anticipate periodic environmental changes
- Adapt — route around damaged areas
How does an organism without neurons solve the traveling salesman problem? The secret is chemotaxis — movement guided by chemical gradients. Physarum pulses its body, streaming cytoplasm through tubular veins. The flow deposits chemoattractants. Existing tubes that carry more flow get reinforced. Tubes that carry less flow wither. The result: an efficient transport network emerges from local feedback.
The Algorithm
The simulation uses a simplified model based on Jeff Jones' 2010 paper. Instead of a continuous biological flow, we have discrete agents:
- Each agent moves forward, leaving a chemical trail
- Each agent senses trail concentration at three angles (left, forward, right)
- Each agent turns toward the direction with highest concentration
- Each agent deposits more trail where it walks
- The trail map diffuses and decays over time
That's it. No path planning. No network optimization. Just move, sense, turn, deposit — and networks form.
Trail Mechanics
Sensing
Each agent samples the trail map at three points ahead:
function senseTrail(x, y, species) {
const ix = Math.floor(x);
const iy = Math.floor(y);
if (ix < 0 || ix >= WIDTH || iy < 0 || iy >= HEIGHT) return 0;
return trailMaps[species][iy * WIDTH + ix];
}
The agent looks left, forward, and right at a fixed angle (45°) and distance (9 pixels). This is the "sensor" — a simple point sample of the chemical field.
Turning Decisions
After sampling three directions, the agent turns toward the strongest signal:
if (forwardSense > leftSense && forwardSense > rightSense) {
// Keep going forward - no turn
} else if (leftSense < rightSense) {
agent.angle += TURN_SPEED * (0.5 + Math.random() * 0.5);
} else if (rightSense < leftSense) {
agent.angle -= TURN_SPEED * (0.5 + Math.random() * 0.5);
} else {
// Random turn when stuck
agent.angle += (Math.random() - 0.5) * TURN_SPEED;
}
The randomness (the * (0.5 + Math.random() * 0.5) factor) prevents deterministic loops. Agents explore rather than circling forever. When all three directions show equal trail, random wandering kicks in.
Deposition
Movement deposits trail at the current position:
function depositTrail(x, y, species) {
const ix = Math.floor(x);
const iy = Math.floor(y);
if (ix < 0 || ix >= WIDTH || iy < 0 || iy >= HEIGHT) return;
trailMaps[species][iy * WIDTH + ix] = Math.min(1,
trailMaps[species][iy * WIDTH + ix] + 0.15);
}
Each step adds ~0.15 to the trail value, capped at 1.0. Heavily-trafficked areas become brighter — this is the positive feedback loop.
Diffusion and Decay
Without diffusion and decay, trails would be thin lines that never spread or fade. The network quality comes from these processes:
Decay
function decayTrails() {
for (let s = 0; s < SPECIES_COUNT; s++) {
for (let i = 0; i < trailMaps[s].length; i++) {
trailMaps[s][i] *= TRAIL_DECAY; // 0.98 per frame
}
}
}
Every frame, all trail values are multiplied by 0.98. A value of 1.0 becomes 0.98, then 0.96, then 0.94... exponential fade. Unused paths disappear in seconds.
Diffusion
function diffuseTrails() {
for (let s = 0; s < SPECIES_COUNT; s++) {
diffusionBuffers[s].set(trailMaps[s]);
for (let y = 1; y < HEIGHT - 1; y++) {
for (let x = 1; x < WIDTH - 1; x++) {
const i = y * WIDTH + x;
const sum = (
diffusionBuffers[s][i - WIDTH - 1] +
diffusionBuffers[s][i - WIDTH] +
diffusionBuffers[s][i - WIDTH + 1] +
diffusionBuffers[s][i - 1] +
diffusionBuffers[s][i] +
diffusionBuffers[s][i + 1] +
diffusionBuffers[s][i + WIDTH - 1] +
diffusionBuffers[s][i + WIDTH] +
diffusionBuffers[s][i + WIDTH + 1]
);
const avg = sum / 9;
trailMaps[s][i] = trailMaps[s][i] * (1 - DIFFUSION_RATE) + avg * DIFFUSION_RATE;
}
}
}
}
Diffusion averages each pixel with its 3×3 neighborhood. This causes trails to blur outward, creating an "attractor basin" around established paths. Agents are drawn toward these basins, reinforcing the center.
The Balance
Decay and diffusion work against each other. High decay + low diffusion = thin, sharp trails that disappear quickly. Low decay + high diffusion = broad, persistent pools that never form structure. The right balance produces branching networks that persist while still evolving.
Multi-Species Behavior
This simulation has three species, each with its own trail map. The key difference from single-species simulation:
// Each species has a separate trail map
const trailMaps = [];
for (let s = 0; s < SPECIES_COUNT; s++) {
trailMaps.push(new Float32Array(WIDTH * HEIGHT));
}
// Agents only sense and deposit their OWN species' trail
const leftSense = senseTrail(agent.x + Math.cos(leftAngle) * SENSOR_DISTANCE,
agent.y + Math.sin(leftAngle) * SENSOR_DISTANCE,
agent.species); // <- species index
Agents of species 0 follow trails deposited by species 0. They ignore the terracotta and sage trails; they reinforce only amber. The three networks self-organize in parallel, sometimes overlapping, sometimes diverging.
In nature, Physarum doesn't have "species" competing. But this multi-species approach reveals something: trail-following is language. An agent can only read the chemical it knows. Networks form within communication channels.
Parameters That Matter
| Parameter | Value | Effect |
|---|---|---|
SENSOR_ANGLE |
π/4 (45°) | Wider angle = more exploration, narrower = focused |
SENSOR_DISTANCE |
9 px | Farther sensing = smoother paths, longer-range networks |
TURN_SPEED |
0.3 rad/frame | Higher = sharper turns, more chaotic exploration |
MOVE_SPEED |
1.5 px/frame | Faster = longer paths before reinforcement |
TRAIL_DECAY |
0.98 | Higher = longer-lasting trails, more stable networks |
DIFFUSION_RATE |
0.15 | Higher = broader paths, stronger attraction basins |
Why Networks Form
The emergent network structure comes from positive feedback and resource competition:
- Reinforcement: A path that gets used deposits more trail, making it more attractive, drawing more agents, depositing more trail. Popular paths become highways.
- Decay: Unused paths fade away. Dead ends get pruned.
- Diffusion: Trails spread, creating soft attractors. Nearby agents veer toward existing paths rather than blazing new ones.
- Initial clustering: Agents start in a cluster, so early trail deposition concentrates in one area. This seed becomes a network hub.
Over time, you see branches break off, explore, either reinforce into new paths or dissolve back into nothing. The network isn't static — it's continuously self-optimizing.
What This Shows
Physarum simulations demonstrate a principle that recurs throughout computational biology: global optimization from local rules. The agents don't "know" they're building a network. They don't "know" the shortest path exists. They follow gradients, deposit chemicals, and the math does the rest.
This principle appears in:
- Ant colony optimization — pheromone trails solve routing problems
- Boids flocking — local avoidance/alignment produces coherent swarms
- Neural development — axons follow chemical gradients to wire brains
- Vascular systems — blood vessel growth follows flow signals
The elegance is in the simplicity. Complex structure emerges from rules you can fit on a notecard. No blueprint. No planner. Just feedback loops.
References
- Jeff Jones — Characteristics of Pattern Formation and Evolution in Approximations of Physarum Transport Networks (the implementation reference for this simulation)
- Tero et al. — Rules for Biologically Inspired Adaptive Network Design (Science, 2010)
- Adamatzky — Advances in Physarum Machines