This program is a Rust implementation of a boids flocking algorithm.
For graphics, it uses nannou, which can be read about at this link
The article Simulating Flocking with the Boids Algorithm was used to provide a foundation of how boid flocks work.
In the article, it's stated that boids follow three main principles:
Steer to avoid contact with flockmates
Steer towards the average heading of flockmates
Steer to move towards the average position of flockmates
The essential pseudocode for the flock system is as follows:
FOR boid in boids
vec1 = separation(boid) // avoid other birds
vec2 = alignment(boid) // match heading of flock
vec3 = cohesion(boid) // move towards center of flock
// could add more rules here
// vec4, ..., vecX
vec_final = vec1 + vec2 + vec3 + ... + vecX<br>
boid.velocity += finalVector // adjust heading<br>
boid.position += boid.velocity // update position<br>
separation(b1: Boid)
distance = X // some number depending on how close you want to allow them to get
result = <0,0>
FOR b2 in boids
IF b1 != b2
IF dist(b1.position, b2.position) < distance
result -= (b2.position - b1.position)
RETURN result
alignment(b1: Boid)
velo = <0,0>
FOR b2 in boids
IF b1 != b2
velo += b2.velocity
velo = velo / (boids.len - 1)
RETURN (velo - b1.velocity) / X // X is some number depending on how quickly you want them to turn
cohesion(b1: Boid)
loc = <0,0>
FOR b2 in boids
IF b1 != b2
loc += b2.position
loc = loc / (boids.len - 1)
RETURN (loc - b1.position) / X // X is some number depending on how quickly you want them to turn
- Merge the three get_vec functions. This would reduce how many times I'd need to iterate through the flock; instead of one time for each vector (3 total) it would be one time for 3 vectors.
- Implement the predator boid
- Fine-tune steering process
- Add live adjustments to the number of boids
- Add obstacles instead of a blank window