Boids
Boids (Bird-oids) is a program for simulating flocking behaviour.
Each boid follows 3 simple rules. In a group, they exhibit emergent behaviour.
Rule 1: Avoidance
Boids avoid crashing into one another, as well as other obstacles.
→ ↗
→ ↘
Too Close! Move Apart
const acceleration = new Vec2(0,0);
for (const obstacle of obstacles) {
const distance = this.position.distanceTo(obstacle.position);
if (distance > obstacle.repelRadius) continue;
const displacement = this.position.clone().subtract(obstacle.position);
acceleration.add(displacement.multiply(obstacle.repelStrength));
}
return acceleration;
Rule 2: Alignment
Boids match the average velocity of their flock.
→ →
← →
→ →
Find velocity Align
acceleration.add(
this.#averageVelocity(flock)
.multiply(this.options.alignmentStrength)
);
Rule 3: Cohesion
Boids move towards the center of their flock.
→ ↘
x x
→ ↗
Find center Converge
acceleration.add(
this.#displacementFromCenter(flock)
.multiply(this.options.cohesionStrength)
);
See Also: