Rust Boids
Three simulations powered by Rust compiled to WebAssembly: a flock, a flock hunted by predators, and slime-mold trails. Pick one, tune its rules, press to interact, and switch to the JavaScript port to see what the speedup is worth.
Press and hold on the flock to pull it toward you and turn it green, or hold Shift while pressing to scare it away.
- Engine
- Rust (WebAssembly)
- Boids
- 4,000
- Step time
- 0.00 ms
- Frames per second
- 0
Each boid follows three rules: steer away from close neighbors (separation), match the heading of nearby ones (alignment), and drift toward their center (cohesion). Flocking emerges from nothing else. To avoid comparing every boid with every other, they are sorted into a grid each frame so only the 3×3 surrounding cells are checked.
In Predators mode each hunter chases the nearest boid and rests for a moment after a catch. Boids nearby panic: they steer away and can sprint a third faster than their normal top speed. A caught boid respawns somewhere else, so the flock never runs out.
Slime mode is a different simulation in the same Rust module. Each agent senses the trail ahead, to the left and to the right, turns toward the strongest scent, and leaves scent behind. The trail is blurred and fades every frame, and from those few rules a branching network emerges, much like the slime mold Physarum.
The simulation is a few hundred lines of dependency-free Rust. JavaScript never copies boid data: it reads x, y and velocity straight out of WebAssembly's linear memory and draws them. The JavaScript engine is a line-for-line port of the same algorithm, so “Step time” compares like with like. Try 16,000 boids on each.