Behaviour Parameters
Separation1.50
Alignment1.00
Cohesion0.80
Perception Radius4.0
Max Speed0.40
-- FPS
Drag to disturb the flock
``` I built a boids simulation with 2,600 agents flocking in 3D space using Three.js. Here's what's happening under the hood: **Performance architecture** - A flat typed-array spatial hash grid (linked-list per cell) rebuilds every frame in O(N), giving ~27-cell neighbor queries instead of N² comparisons - All boid state (position, velocity) lives in `Float32Array`s for cache-friendly iteration - Rendering uses a single `InstancedMesh` with 2,600 cone instances — one draw call - Per-instance colors are written into an `InstancedBufferAttribute` each frame **Flocking rules** (classic Reynolds boids) - **Separation** — inverse-distance-weighted repulsion within a tight radius - **Alignment** — steer toward average heading of neighbors - **Cohesion** — steer toward average position of neighbors - Each force is computed as a desired velocity, then steering = desired − current, clamped to `maxForce` - Soft boundary repulsion keeps the cloud contained; a minimum-speed floor prevents stalling **Interaction** - Drag (or touch-drag) anywhere — a raycaster projects the cursor onto a plane through the origin facing the camera, giving a 3D disturbance point - Boids within the disturbance radius get a falloff-weighted repulsion force - Two glowing additive rings visualize the disturbance zone - Color is mapped to speed: slow boids glow deep amber → gold → cyan → white as they accelerate, so a drag sends a visible "shockwave" of bright color through the flock **Visual design** - Additive blending makes dense flock regions naturally glow brighter — emergent luminosity - Exponential fog adds depth so distant boids fade into the dark - Camera breathes slowly on a Lissajous path for parallax - Glass-morphism control panel with live sliders for all three flocking weights, perception radius, and max speed Try cranking cohesion high and separation low to watch the flock collapse into a swirling blob, or push separation to maximum for a crystalline lattice that scatters when you drag through it.