Boids / Flocking

初出 1986 · Craig Reynolds — Craig Reynolds, flocking model (1986/1987). · 年表 ↗

発明者による命名 名称 · 命名 Craig Reynolds (1987) — Reynolds 自身が 1987 年の SIGGRAPH 論文で "boids" と命名(「一般化した群れの シミュレーション生物を boids と呼んだ」)。"bird-oid object" の短縮とされる。発明者命名。

ひとことで 分離・整列・結合の 3 ルールでエージェントが群れを成す生物模擬モデル。

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

コード

上のデモの実際のスケッチ(サンドボックスで再現可能)。これで全部です。

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

デモのソースを開く ↗

仕組み

各ボイドに位置と速度を与え、毎ステップ知覚半径内の近傍を求めて 3 つの操舵ベクトルを計算する — 近すぎる近傍から離れる「分離」、近傍の平均進行方向に合わせる「整列」、近傍の重心へ向かう「結合」。これらを(重み付きで)合算して速度に加え、最大速度で制限して移動する。群れの挙動は局所的相互作用のみから創発し、目標追従や障害物回避も同様に重ねられる。

パラメータ

系譜

先行例
Craig Reynolds
関連
Particle System、Physics Simulation、Strange Attractor

よく組み合わされるアルゴリズム

このアルゴリズムを使う作品 (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

最終更新 2026-07-16