---
id: "boids"
title: "Boids flocking"
domain: "simulation"
stage: "simulation"
realtime: "realtime"
platforms: [webgl2, webgpu, desktop-gpu, cpu, unity, unreal, godot]
year_introduced: 1987
inputs: [particles]
outputs: [particles, animation]
style: "both"
dynamic: "dynamic"
cost: "cheap"
authority: "Reynolds, 1987"
url: "https://texelatlas.vercel.app/t/boids/"
sources_verified_at: "2026-11-29"
---
# Boids flocking (Reynolds, 1987)

> Emergent flocking from three local steering rules per agent: separation, alignment and cohesion, computed from neighbours within a view radius.

## Reach for it when

- want birds or fish to move together as a believable group
- crowd of agents needs natural swarming motion
- individual agents collide or scatter instead of flocking
- emergent group movement from simple local rules
- cheap schooling behaviour for ambient creatures

## How it works

Each agent looks at neighbours within a radius and field of view. Separation steers away from nearby neighbours, alignment steers toward their average heading, and cohesion steers toward their centre. The three steering vectors are weighted, summed and clamped to a maximum acceleration, then integrated into velocity and position with a speed limit. Obstacle avoidance, goal seeking and predator flee are extra steering terms. Neighbour lookups use a spatial hash or grid; on the GPU the algorithm maps to one thread per boid with a sorted-cell neighbour search. Orientation is set from velocity with a banking term for believable flight.

## When to use it

Choose it for birds, fish, swarms and crowds where plausible group motion matters more than individual intent. Prefer [Physarum agent transport networks](https://texelatlas.vercel.app/t/physarum-transport-networks.md) when agents should leave trails and form networks, [Verlet integration with relaxation constraints](https://texelatlas.vercel.app/t/verlet-integration-constraints.md) for physically linked groups, and navmesh/path planning libraries when individuals need to reach distinct goals.

## Cost

cheap — Cheap with spatial hashing; naive O(n^2) breaks past a few thousand agents.
Measured GPU Weight: 354.76× baseline (extreme) — 11.6716 ms median / 11.798 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 322.14× baseline (extreme) — 5.122 ms median (2.28× faster on L4).
Ampere A100 scaling: 246.54× baseline (extreme) — 7.1004 ms median (1.64× faster on A100).

## Failure modes

- O(n^2) neighbour search unless a grid or hash is used
- Flocks split or collapse into a ball with badly balanced weights
- Jitter and oscillation when separation and cohesion fight at fixed radius
- Agents pass through concave obstacles unless explicit avoidance is added

## Engine support

- blender: builtin — `Particle Properties > Physics > Boids` (evidence: https://docs.blender.org/manual/en/5.2/physics/particles/emitter/physics/boids.html, checked 2026-12-01)
- threejs: official — `examples/webgpu_compute_birds.html` (evidence: https://github.com/mrdoob/three.js/blob/r187/examples/webgpu_compute_birds.html, checked 2026-12-01) — Official GPU flocking example; not a library.
- unreal: builtin — `Niagara GPU neighbor-grid & flocking particle modules` (evidence: https://dev.epicgames.com/documentation/en-us/unreal-engine/overview-of-niagara-effects-for-unreal-engine, checked 2026-12-01)

## Dataflow

- Inputs: particles
- Outputs: particles, animation
- Platforms: webgl2, webgpu, desktop-gpu, cpu, unity, unreal, godot

## Presets

### Boids flocking — separation, alignment & cohesion murmuration with wake trails (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_SPEED": 1,
  "P_COHESION": 0.85,
  "P_SEPARATION": 0.65,
  "P_TRAIL_LENGTH": 0.75
}
```

```glsl
const float P_SPEED = 1.0; // P Speed [0.0, 2.5, 0.05]
const float P_COHESION = 0.85; // P Cohesion [0.1, 2.0, 0.05]
const float P_SEPARATION = 0.65; // P Separation [0.1, 2.0, 0.05]
const float P_TRAIL_LENGTH = 0.75; // P Trail Length [0.0, 1.8, 0.05]

vec2 hash21(float p) {
  vec3 p3 = fract(vec3(p) * vec3(0.1031, 0.1030, 0.0973));
  p3 += dot(p3, p3.yzx + 33.33);
  return fract((p3.xx + p3.yz) * p3.zy);
}

// Compute smooth murmuration trajectory with cohesive flock ribbon + local separation offset
vec2 boidPos(int idx, float t, vec2 attr) {
  float fi = float(idx);
  vec2 h = hash21(fi * 17.3 + 3.1);
  float flockId = step(0.5, fract(fi * 0.5));
  float phase = t * (0.65 + 0.12 * h.x) + fi * 0.24 + flockId * 3.14159;

  // Ribbon guide path driven by cohesion toward moving attractor
  float coh = clamp(P_COHESION, 0.1, 2.2);
  float spread = mix(0.42, 0.11, clamp((coh - 0.1) / 1.8, 0.0, 1.0));
  vec2 guide = vec2(
    0.46 * sin(phase * 0.78 + flockId * 1.4) + 0.18 * cos(phase * 1.55),
    0.34 * cos(phase * 0.92) + 0.16 * sin(phase * 1.38 + flockId)
  );
  guide = mix(guide, attr * 0.75, 0.25 * clamp(coh, 0.0, 1.5));

  // Separation micro-orbit around ribbon center
  float sep = max(P_SEPARATION, 0.05) * spread;
  float ang = fi * 2.39996 + t * (1.2 + 0.6 * h.y);
  vec2 offset = vec2(cos(ang), sin(ang)) * sep * (0.45 + 0.55 * h.y);
  offset += 0.05 * sep * vec2(sin(t * 2.1 + fi), cos(t * 1.9 - fi * 1.3));

  vec2 p = guide + offset;
  // Soft obstacle avoidance around center beacon
  vec2 dObs = p - vec2(0.0, 0.0);
  float distObs = length(dObs) + 1e-4;
  float avoid = smoothstep(0.24, 0.08, distObs);
  p += (dObs / distObs) * avoid * 0.14;
  return p;
}

float sdDart(vec2 p, vec2 dir, float size) {
  vec2 n = normalize(dir + vec2(1e-5, 0.0));
  vec2 perp = vec2(-n.y, n.x);
  vec2 q = vec2(dot(p, n), dot(p, perp)) / size;
  // Sleek swept-wing boid silhouette
  float body = max(abs(q.y) - mix(0.42, 0.02, clamp((q.x + 0.65) / 1.65, 0.0, 1.0)), abs(q.x - 0.15) - 0.85);
  float notch = -(q.x + 0.45 - abs(q.y) * 0.85);
  return max(body, -notch) * size;
}

vec3 aces(vec3 x) {
  x = max(x, 0.0);
  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
  float px = 1.0 / iResolution.y;
  float t = iTime * max(P_SPEED, 0.0);

  vec2 attr = vec2(0.28 * cos(t * 0.45), 0.22 * sin(t * 0.62));
  if (iMouse.z > 0.5 || length(iMouse.xy) > 1.0) {
    attr = (iMouse.xy - 0.5 * iResolution.xy) / iResolution.y;
  }

  // Deep twilight radar backdrop with subtle spatial-hash grid
  vec3 bg = mix(vec3(0.025, 0.045, 0.085), vec3(0.06, 0.11, 0.19), smoothstep(-0.55, 0.55, uv.y));
  vec2 g = abs(fract(uv * 8.0 - 0.5) - 0.5) / (8.0 * px);
  float gridLine = smoothstep(1.4, 0.0, min(g.x, g.y));
  bg += vec3(0.04, 0.09, 0.14) * gridLine * 0.32;

  // Obstacle beacon at origin
  float rObs = length(uv);
  bg += vec3(0.16, 0.24, 0.34) * smoothstep(0.012, 0.0, abs(rObs - 0.13)) * 0.45;
  bg += vec3(0.05, 0.09, 0.15) * smoothstep(0.13, 0.0, rObs);

  vec3 col = bg;
  float trailLen = max(P_TRAIL_LENGTH, 0.0);

  for (int i = 0; i < 32; i++) {
    vec2 p0 = boidPos(i, t, attr);
    vec2 pPrev = boidPos(i, t - 0.04, attr);
    vec2 vel = p0 - pPrev;
    float speed = length(vel) + 1e-4;
    vec2 dir = vel / speed;
    float flockId = step(0.5, fract(float(i) * 0.5));
    vec3 boidTint = mix(vec3(0.32, 0.85, 1.0), vec3(1.0, 0.64, 0.28), flockId);

    // Cohesion/alignment wake trail (4 trail segments)
    if (trailLen > 0.01) {
      vec2 a = p0;
      for (int s = 1; s <= 4; s++) {
        float fs = float(s);
        vec2 b = boidPos(i, t - fs * 0.055 * trailLen, attr);
        vec2 pa = uv - a, ba = b - a;
        float h = clamp(dot(pa, ba) / (dot(ba, ba) + 1e-5), 0.0, 1.0);
        float dSeg = length(pa - ba * h);
        float fade = (1.0 - fs * 0.22);
        col += boidTint * exp(-dSeg * 115.0) * 0.18 * fade * min(trailLen, 1.2);
        a = b;
      }
    }

    // Leader perception & separation radius rings for first two agents
    if (i < 2) {
      float dRing = length(uv - p0);
      float perceive = smoothstep(2.0 * px, 0.0, abs(dRing - 0.12));
      float sepRing = smoothstep(2.0 * px, 0.0, abs(dRing - 0.048 * max(P_SEPARATION, 0.2)));
      col += boidTint * perceive * 0.28;
      col += vec3(1.0, 0.35, 0.35) * sepRing * 0.25;
    }

    // Crisp anti-aliased dart silhouette + subtle glow
    float d = sdDart(uv - p0, dir, 0.024);
    float mask = smoothstep(1.5 * px, -1.5 * px, d);
    float glow = exp(-max(d, 0.0) * 85.0) * 0.26;
    col += boidTint * glow;
    col = mix(col, mix(boidTint, vec3(1.0), 0.45), mask);
  }

  col *= 1.0 - 0.25 * dot(uv, uv);
  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}
```

Attribution: Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).

## Combination ideas

- [Brush strokes that flock](https://texelatlas.vercel.app/ideas/flocking-brush-strokes/)

## Sources

1. [primary] Craig W. Reynolds (1987) Flocks, herds and schools: A distributed behavioral model. ACM SIGGRAPH Computer Graphics. https://doi.org/10.1145/37402.37406 (verified via crossref on 2026-11-29)
2. [tutorial] Craig Reynolds Boids (Flocks, Herds, and Schools: a Distributed Behavioral Model). red3d.com. http://www.red3d.com/cwr/boids (verified via http on 2026-11-29)

## Cite this entry

Texel. "Boids flocking." https://texelatlas.vercel.app/t/boids/ (accessed 2026-12-01).
