Boids
Technical Details
- p5.js for canvas rendering and animation
- Boids algorithm for flocking behavior
- HSB color mode for vibrant, speed-based coloring
- Particle trails with alpha blending for smooth motion blur
- Mouse interaction with distance-based force fields
The Code
Each boid follows these steps every frame:
1// 1. Calculate forces from neighbors2let alignment = this.align(boids);3let cohesion = this.cohesion(boids);4let separation = this.separation(boids);5
6// 2. Apply forces as acceleration7this.acceleration = alignment + cohesion + separation;8
9// 3. Update velocity and position10this.velocity += this.acceleration;11this.position += this.velocity;12
13// 4. Limit maximum speed14if (speed > maxSpeed) {15 velocity = normalize(velocity) * maxSpeed;1 collapsed line
16}