Psychologically speaking, flocking is very simple. Mathematically speaking, its almost impossible to generally create (make it actually look like behaviour).
The above image is from a PDF on the web, have no idea where now (its been on my hard drive for about a year now). It talks simply about simple mathematical algorithms in a very simple manner that is applicable to any computer language (like pseudocode, but even simpler). Some algorithms are circle/object packaging, tiling, weaving, blending, cracking. The above image shows the underlying concepts of flocking. Below is what it says on the image:
Recipe for Flocking
For each agent for each increment of time:
- Avoid crowding local flock-mates. Steer to keep a minimum distance between each agent and the ones around it.
- Align towards the average heading of local flockmates.
- Cohere to the flock, move towards the center of mass of local flockmates
Well, that seems easy, right? Its just two simple rules! If too close, make agent turn away. If too far, but close, make agent turn towards nearest center of mass. Sadly, it is something quite difficult, as it is hard to define center of masses and how to make something go towards something in a non-weird behaviour.
I found some great examples online. One of them stood out in particular. I particulary like the one linked because it also contains objects, food and predators, something that I initially wanted. A simpler example is one from the Processing.org site, which is originally based in Craig Renold (someone who developed the algorithm in 1986).
So I tried to replicate it on my own for a while, so far with out sucess. But hopefully I’ll find a way in the next few days (I may have to peek onto the source code on the Processing.org site version, hopefully not as I want the challenge)
As someone who likes to look at behavioural psychology, I think flocking is one of the greatest phenomena in nature, and its amazing to see flocking occur (most impressive are birds, which take 3-dimensional motions). And to think that it can be really replicated in mathematical terms is even more impressive.