Boids / Flocking
First appeared 1986 · Craig Reynolds — Craig Reynolds, flocking model (1986/1987). · timeline ↗
inventor-coined Name · coined by Craig Reynolds (1987) — Reynolds coined "boids" himself, introducing it in the 1987 SIGGRAPH paper "Flocks, Herds, and Schools: A Distributed Behavioral Model" ("I called the generic simulated flocking creatures boids"). Commonly glossed as a contraction of "bird-oid object". Inventor-coinage.
In one line An agent-based model of flocking in which each "boid" steers by three local rules — separation, alignment and cohesion — applied only to nearby neighbours, so coordinated group motion emerges with no central control.
The code
The actual sketch behind the demo above — sandboxed and reproducible. This is the whole thing.
boids/index.html
const SEED = Math.floor(Math.random()*1e9);
let boids = [];
let s;
function setup() {
randomSeed(SEED);
s = Math.min(windowWidth, windowHeight);
createCanvas(s, s);
// Craig Reynolds' three rules: separation, alignment, cohesion.
for (let i = 0; i < 220; i++) {
boids.push({
x: random(width), y: random(height),
vx: random(-1, 1), vy: random(-1, 1),
});
}
noStroke();
}
function draw() {
background(250, 18);
const VIEW = s * 0.07;
const SEP = s * 0.022;
const MAX_SPEED = s * 0.0045;
for (const b of boids) {
let cx = 0, cy = 0, vx = 0, vy = 0, sx = 0, sy = 0, n = 0;
for (const o of boids) {
if (o === b) continue;
const dx = o.x - b.x, dy = o.y - b.y;
const d2 = dx * dx + dy * dy;
if (d2 > VIEW * VIEW) continue;
n++;
cx += o.x; cy += o.y;
vx += o.vx; vy += o.vy;
if (d2 < SEP * SEP) {
const d = Math.sqrt(d2) + 1e-4;
sx -= dx / d; sy -= dy / d;
}
}
if (n > 0) {
cx /= n; cy /= n; vx /= n; vy /= n;
// cohesion
b.vx += (cx - b.x) * 0.0008;
b.vy += (cy - b.y) * 0.0008;
// alignment
b.vx += (vx - b.vx) * 0.04;
b.vy += (vy - b.vy) * 0.04;
// separation
b.vx += sx * 0.08;
b.vy += sy * 0.08;
}
// limit speed
const sp = Math.hypot(b.vx, b.vy);
if (sp > MAX_SPEED) { b.vx = b.vx / sp * MAX_SPEED; b.vy = b.vy / sp * MAX_SPEED; }
b.x += b.vx; b.y += b.vy;
// wrap
if (b.x < 0) b.x += width; if (b.x >= width) b.x -= width;
if (b.y < 0) b.y += height; if (b.y >= height) b.y -= height;
// draw as a tiny triangle pointing along velocity
const a = Math.atan2(b.vy, b.vx);
const L = s * 0.012;
fill(20);
const px = b.x, py = b.y;
const c = Math.cos(a), si = Math.sin(a);
triangle(
px + c * L, py + si * L,
px - c * L*0.5 - si*L*0.35, py - si * L*0.5 + c*L*0.35,
px - c * L*0.5 + si*L*0.35, py - si * L*0.5 - c*L*0.35,
);
}
}How it works
Give every boid a position and velocity. Each step, for each boid, find the neighbours within a perception radius and compute three steering vectors: 1. Separation: steer away from neighbours that are too close (avoid crowding). 2. Alignment: steer toward the average heading of neighbours. 3. Cohesion: steer toward the average position (centre of mass) of neighbours. Sum the three (often weighted), add it to the velocity, clamp to a maximum speed, and move. The flock-like behaviour is purely emergent from these local interactions; extra rules (goal-seeking, obstacle/predator avoidance) are layered on the same way.
Parameters
separation: number — Weight of the steering force that avoids crowding nearby flockmates.alignment: number — Weight of the force that matches velocity with neighbours.cohesion: number — Weight of the force that steers toward the local centre of mass.perceptionRadius: number — Distance within which other agents count as neighbours.maxSpeed: number — Upper bound on each agent’s velocity.
Lineage
Frequently combined with
- Perlin / Simplex Noise · 7 works
- Particle System · 4 works
- HSL / Hue Cycle · 3 works
- Quadtree / Recursive Quad Split · 3 works
- Spring-Mass / Verlet · 2 works
- Raymarching with SDFs · 2 works
- Fragment Shader (GLSL) · 2 works
- Plotter-style Hatching · 2 works
- Flow Field / Vector Field · 2 works
- Fractal / Recursive Subdivision · 2 works
- Voronoi Diagram · 1 work
- Marching Squares / Contours · 1 work
Works using this algorithm (11)
Last updated 2026-07-16