Oh, brother, this is just unrealistically cool algorithm!
Its essence is to imitate flock behavior.
That is, having some kind of abstract agents, we can make them move in flocks (like a large group of birds, fish or other animals).
I had a lot of fun implementing this algorithm. It amazed me how such simple logic could lead to such magic!
This algorithm is based on damn simple logic.
Suppose we have so-called agents that have a location in two-dimensional space and a velocity represented by a vector.
Now we need to go through each of the agents and apply some set of rules to them:
- Cohesion (an agent tends to the center of the flock);
- Separation (an agent keeps a distance from other agents);
- Alignment (an agent tends to move at the same speed as other agents).
That's all!
How exactly to implement these rules in the code is a secondary issue.
Most importantly, we have agents and a set of rules that they must follow.
If you want to learn more about the boids simulation algorithm, I advise you to read the following articles:
- «The computational beauty of flocking: boids revisited» by I. Lebar Bajec, N. Zimic & M. Mraz
- «Boids - Stanford Computer Science» by Timm[ie] Wong
- «Simulating Flocking with the Boids Algorithm» by Rohith Vishwajith
- «Boids. Background and Update» by Craig Reynolds
You can simulate the presence of a predator with your mouse.
To do this, hold down the left mouse button and, without releasing, move it near the agents.