{
 "id": "boids",
 "title": "Boids flocking",
 "aliases": [],
 "domain": "simulation",
 "subdomain": "crowds & agents",
 "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",
 "summary": "Emergent flocking from three local steering rules per agent: separation, alignment and cohesion, computed from neighbours within a view radius.",
 "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": "Choose it for birds, fish, swarms and crowds where plausible group motion matters more than individual intent. Prefer `physarum-transport-networks` when agents should leave trails and form networks, `verlet-integration-constraints` for physically linked groups, and navmesh/path planning libraries when individuals need to reach distinct goals.",
 "cost": {
  "class": "cheap",
  "notes": "Cheap with spatial hashing; naive O(n^2) breaks past a few thousand agents."
 },
 "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"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/37402.37406",
   "type": "paper",
   "title": "Flocks, herds and schools: A distributed behavioral model",
   "authors": [
    "Craig W. Reynolds"
   ],
   "year": 1987,
   "venue": "ACM SIGGRAPH Computer Graphics",
   "url": "https://doi.org/10.1145/37402.37406",
   "ids": {
    "doi": "10.1145/37402.37406"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/37402.37406",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/37402.37406",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/37402.37406",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/37402.37406",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/37402.37406",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "reynolds1987flocks"
  },
  {
   "role": "tutorial",
   "key": "url:https://www.red3d.com/cwr/boids",
   "type": "tutorial",
   "title": "Boids (Flocks, Herds, and Schools: a Distributed Behavioral Model)",
   "authors": [
    "Craig Reynolds"
   ],
   "year": null,
   "venue": "red3d.com",
   "url": "http://www.red3d.com/cwr/boids",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20261117190525/https://www.red3d.com/cwr/boids/",
     "timestamp": "20261117190525"
    },
    "page_title": "Boids (Flocks, Herds, and Schools: a Distributed Behavioral Model)",
    "host": "red3d.com",
    "title_confirmed_by_page": true,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "http",
     "endpoint": "GET https://www.red3d.com/cwr/boids → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://www.red3d.com/cwr/boids → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "http",
     "endpoint": "GET https://www.red3d.com/cwr/boids → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "reynoldsboids"
  }
 ],
 "presets": [
  {
   "id": "boids-live",
   "title": "Boids flocking — separation, alignment & cohesion murmuration with wake trails",
   "runtime": "glsl-fragment",
   "params": {
    "P_SPEED": 1,
    "P_COHESION": 0.85,
    "P_SEPARATION": 0.65,
    "P_TRAIL_LENGTH": 0.75
   },
   "basis": "original",
   "license_note": "Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).",
   "code": "const float P_SPEED = 1.0; // P Speed [0.0, 2.5, 0.05]\nconst float P_COHESION = 0.85; // P Cohesion [0.1, 2.0, 0.05]\nconst float P_SEPARATION = 0.65; // P Separation [0.1, 2.0, 0.05]\nconst float P_TRAIL_LENGTH = 0.75; // P Trail Length [0.0, 1.8, 0.05]\n\nvec2 hash21(float p) {\n  vec3 p3 = fract(vec3(p) * vec3(0.1031, 0.1030, 0.0973));\n  p3 += dot(p3, p3.yzx + 33.33);\n  return fract((p3.xx + p3.yz) * p3.zy);\n}\n\n// Compute smooth murmuration trajectory with cohesive flock ribbon + local separation offset\nvec2 boidPos(int idx, float t, vec2 attr) {\n  float fi = float(idx);\n  vec2 h = hash21(fi * 17.3 + 3.1);\n  float flockId = step(0.5, fract(fi * 0.5));\n  float phase = t * (0.65 + 0.12 * h.x) + fi * 0.24 + flockId * 3.14159;\n\n  // Ribbon guide path driven by cohesion toward moving attractor\n  float coh = clamp(P_COHESION, 0.1, 2.2);\n  float spread = mix(0.42, 0.11, clamp((coh - 0.1) / 1.8, 0.0, 1.0));\n  vec2 guide = vec2(\n    0.46 * sin(phase * 0.78 + flockId * 1.4) + 0.18 * cos(phase * 1.55),\n    0.34 * cos(phase * 0.92) + 0.16 * sin(phase * 1.38 + flockId)\n  );\n  guide = mix(guide, attr * 0.75, 0.25 * clamp(coh, 0.0, 1.5));\n\n  // Separation micro-orbit around ribbon center\n  float sep = max(P_SEPARATION, 0.05) * spread;\n  float ang = fi * 2.39996 + t * (1.2 + 0.6 * h.y);\n  vec2 offset = vec2(cos(ang), sin(ang)) * sep * (0.45 + 0.55 * h.y);\n  offset += 0.05 * sep * vec2(sin(t * 2.1 + fi), cos(t * 1.9 - fi * 1.3));\n\n  vec2 p = guide + offset;\n  // Soft obstacle avoidance around center beacon\n  vec2 dObs = p - vec2(0.0, 0.0);\n  float distObs = length(dObs) + 1e-4;\n  float avoid = smoothstep(0.24, 0.08, distObs);\n  p += (dObs / distObs) * avoid * 0.14;\n  return p;\n}\n\nfloat sdDart(vec2 p, vec2 dir, float size) {\n  vec2 n = normalize(dir + vec2(1e-5, 0.0));\n  vec2 perp = vec2(-n.y, n.x);\n  vec2 q = vec2(dot(p, n), dot(p, perp)) / size;\n  // Sleek swept-wing boid silhouette\n  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);\n  float notch = -(q.x + 0.45 - abs(q.y) * 0.85);\n  return max(body, -notch) * size;\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float px = 1.0 / iResolution.y;\n  float t = iTime * max(P_SPEED, 0.0);\n\n  vec2 attr = vec2(0.28 * cos(t * 0.45), 0.22 * sin(t * 0.62));\n  if (iMouse.z > 0.5 || length(iMouse.xy) > 1.0) {\n    attr = (iMouse.xy - 0.5 * iResolution.xy) / iResolution.y;\n  }\n\n  // Deep twilight radar backdrop with subtle spatial-hash grid\n  vec3 bg = mix(vec3(0.025, 0.045, 0.085), vec3(0.06, 0.11, 0.19), smoothstep(-0.55, 0.55, uv.y));\n  vec2 g = abs(fract(uv * 8.0 - 0.5) - 0.5) / (8.0 * px);\n  float gridLine = smoothstep(1.4, 0.0, min(g.x, g.y));\n  bg += vec3(0.04, 0.09, 0.14) * gridLine * 0.32;\n\n  // Obstacle beacon at origin\n  float rObs = length(uv);\n  bg += vec3(0.16, 0.24, 0.34) * smoothstep(0.012, 0.0, abs(rObs - 0.13)) * 0.45;\n  bg += vec3(0.05, 0.09, 0.15) * smoothstep(0.13, 0.0, rObs);\n\n  vec3 col = bg;\n  float trailLen = max(P_TRAIL_LENGTH, 0.0);\n\n  for (int i = 0; i < 32; i++) {\n    vec2 p0 = boidPos(i, t, attr);\n    vec2 pPrev = boidPos(i, t - 0.04, attr);\n    vec2 vel = p0 - pPrev;\n    float speed = length(vel) + 1e-4;\n    vec2 dir = vel / speed;\n    float flockId = step(0.5, fract(float(i) * 0.5));\n    vec3 boidTint = mix(vec3(0.32, 0.85, 1.0), vec3(1.0, 0.64, 0.28), flockId);\n\n    // Cohesion/alignment wake trail (4 trail segments)\n    if (trailLen > 0.01) {\n      vec2 a = p0;\n      for (int s = 1; s <= 4; s++) {\n        float fs = float(s);\n        vec2 b = boidPos(i, t - fs * 0.055 * trailLen, attr);\n        vec2 pa = uv - a, ba = b - a;\n        float h = clamp(dot(pa, ba) / (dot(ba, ba) + 1e-5), 0.0, 1.0);\n        float dSeg = length(pa - ba * h);\n        float fade = (1.0 - fs * 0.22);\n        col += boidTint * exp(-dSeg * 115.0) * 0.18 * fade * min(trailLen, 1.2);\n        a = b;\n      }\n    }\n\n    // Leader perception & separation radius rings for first two agents\n    if (i < 2) {\n      float dRing = length(uv - p0);\n      float perceive = smoothstep(2.0 * px, 0.0, abs(dRing - 0.12));\n      float sepRing = smoothstep(2.0 * px, 0.0, abs(dRing - 0.048 * max(P_SEPARATION, 0.2)));\n      col += boidTint * perceive * 0.28;\n      col += vec3(1.0, 0.35, 0.35) * sepRing * 0.25;\n    }\n\n    // Crisp anti-aliased dart silhouette + subtle glow\n    float d = sdDart(uv - p0, dir, 0.024);\n    float mask = smoothstep(1.5 * px, -1.5 * px, d);\n    float glow = exp(-max(d, 0.0) * 85.0) * 0.26;\n    col += boidTint * glow;\n    col = mix(col, mix(boidTint, vec3(1.0), 0.45), mask);\n  }\n\n  col *= 1.0 - 0.25 * dot(uv, uv);\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "boids",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T16:50:13.852Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.347,
      "std": 0.1035,
      "unique_colors": 353
     },
     "image": "/specimens/boids-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "physarum-transport-networks",
  "verlet-integration-constraints",
  "chaotic-attractor-density-rendering"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/simulation/boids.json"
 },
 "solves": [
  "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"
 ],
 "authority": "Reynolds, 1987",
 "url": "https://texelatlas.vercel.app/t/boids/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 354.76,
  "tier": "extreme",
  "p50_ms": 11.6716,
  "p95_ms": 11.798,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "boids-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 322.14,
   "tier": "extreme",
   "p50_ms": 5.122,
   "p95_ms": 5.2655,
   "speedup_x": 2.28,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 246.54,
   "tier": "extreme",
   "p50_ms": 7.1004,
   "p95_ms": 7.1014,
   "speedup_x": 1.64,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}