Flock, Herd, And Shoal Simulation

Flocks of birds, herds or animals, shoals (or schools) of fish, and swarms of insects seem to behave as if they are a single creature. In fact, their behaviour emerges from simple rules being followed by each individual creature. In the demonstration below of how to simulate swarm behaviour, three rules used:

  • If the creature is close to the edge of the arena, it turns to avoid it
  • Othwerise, if the creature is very close to another creature, it turns to avoid it
  • Otherwise, if the creature is close to other creatures, it turns to face a similar direction

The demonstration requires a browser with support for Javascript and PNG transparency (in browsers without support for PNG transparency, such as Internet Explorer 6, the creatures will appear on a rectangular background). In addition, a large browser window size is required to be able to see the entire arena at one time. Clicking the ‘Randomise Position’ button will move all the creatures to random locations facing random directions.

The creatures quickly assemble themselves into groups which, constrained by the arena, begin to move clockwise or anticlockwise. If different groups are moving in different directions then the situation can become chaotic for a long time as the groups frequently collide. After a time there will be a number of groups all heading in the same direction.

Each group slowly rotates (one creature has inverted colours to make this easier to see), and a consequence of this is that large groups move around the edge faster than smaller groups. After a time the larger groups catch up with and absorb the smaller groups, leading to a single group containing all the creatures. However, very large groups are unstable and eventually the single group will fracture, leading to the several smaller groups, and the process then repeats.

This article was last edited on 3rd August 2011. The author can be contacted using the form below.
Back to home page
Bookmark with: