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.

Boids / Flocking · Open in new tab ↗ · Motion begins only when you press Run.

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,
    );
  }
}

Open the demo source ↗

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

Lineage

Precedents
Craig Reynolds
Related
Particle System, Physics Simulation, Strange Attractor

Frequently combined with

Works using this algorithm (11)

Beauty in the Hurting
ryan-green
Cytographia
golan-levin
Democracity
generative-artworks
flora, fauna, false gods & floods
ryan-green
KARMA
kala
Memories of Qilin
emily-xie
ORI
james-merrill
Solar Transits
robert-hodgin
Space Debris [m'aider]
whalestreet-x-pxlq
Space Debris [ravaged]
whalestreet-x-pxlq
Swarm Study / III
random-international

Last updated 2026-07-16