Simulating flocking behavior
Flocking is a term applied to the behavior of birds and other flying animals that are organized into a swarm or flock.
From our point of view, it is especially interesting that flocking behavior can be simulated by applying only three rules to each particle (Boid). These rules are as follows:
Separation: Avoid neighbors that are too near
Alignment: Steer towards the average velocity of neighbors
Cohesion: Steer towards the average position of neighbors
Getting ready
In this recipe, we are going to use the code from the Creating a particle system in 2D recipe.
How to do it…
We will implement the rules for flocking behavior. Perform the following steps to do so:
Change the number of the particles, their radius, and mass.
int numParticle = 50; float radius = 5.f; float mass = 1.f;
Add a definition for new methods and properties to the
Particle
class inside theParticle.h
header file.void flock(std::vector<Particle*>& particles); ci::Vec2f steer(ci::Vec2f target,...