Skip to content

sharziki/swarm-intelligence

Repository files navigation

Swarm Intelligence

Interactive visualization of the Boids flocking algorithm. Watch emergent collective behavior arise from simple local rules applied to individual agents.

Features

  • Boids Algorithm: Classic flocking simulation with three steering behaviors
  • Interactive Control: Attract or repel boids with mouse clicks
  • Configurable Parameters: Adjust separation, alignment, and cohesion weights
  • Visual Effects: Motion trails and perception radius display
  • Real-time Simulation: Smooth 60fps animation

How It Works

The Three Rules

Each boid follows three simple rules based only on nearby flockmates:

  1. Separation: Steer to avoid crowding local flockmates
  2. Alignment: Steer toward the average heading of local flockmates
  3. Cohesion: Steer to move toward the average position of local flockmates

Emergent Behavior

From these simple local rules, complex group behaviors emerge:

  • Flock formation and movement
  • Natural-looking turns and avoidance
  • Group splitting and merging

Controls

Action Description
Left Click (hold) Attract boids toward cursor
Right Click (hold) Repel boids away from cursor
Sliders Adjust behavior weights
Play/Pause Start or stop simulation
Reset Regenerate flock

Parameters

Parameter Description
Boid Count Number of agents in simulation
Separation Weight for avoiding nearby boids
Alignment Weight for matching neighbor headings
Cohesion Weight for moving toward neighbors
Max Speed Maximum velocity of boids
Perception Radius for detecting neighbors

Tech Stack

  • React 19 + TypeScript
  • Vite
  • Tailwind CSS 4
  • Canvas API for rendering

Getting Started

npm install
npm run dev

Algorithm Details

Steering Force

steering = desired_velocity - current_velocity
steering = limit(steering, max_force)

Separation

for each neighbor within separation_radius:
  diff = my_position - neighbor_position
  diff = normalize(diff) / distance
  sum += diff
steer toward sum

Alignment

for each neighbor within perception_radius:
  sum += neighbor_velocity
average = sum / count
steer toward average heading

Cohesion

for each neighbor within perception_radius:
  sum += neighbor_position
center = sum / count
steer toward center

References

  • Craig Reynolds' original Boids paper (1987)
  • "Flocks, Herds, and Schools: A Distributed Behavioral Model"

License

MIT