Bird flocking
A computational model
Note: Due to rendering limitations, the simulation may not behave as expected on screens with a size smaller than 800 x 600. To optimal quality, we recommend to visit this webpage on a desktop computer.
Introduction
Have you ever seen a flock of birds flying? Many bird species form flocks for a variety of reasons, included but not limited to:
- Foraging: feeding in group allows the birds to take advantage of the same food supplies.
- Protection: a large group of birds has a better chance of spotting a predator or another potential threar than a single bird has.
- Mating: male birds performing in a flock make themselves more visible to a greater number of females, increasing their chances of a successful mating.
- Warmth: during winter, bird flocks can share the benefit of communal warmth to survive severely cold temperatures.
- Aerodynamics: while flying in flocks, birds arrange themselves in specific formations that take advantage of changing wind patterns, allowing them to use the air currents in the most energy-efficient way.
Modeling the behavior
Flocking behavior was first simulated on a computer in 1987 by Craig Reynolds, with his simulation program, Boids. Reynolds assumed that flocking birds were driven by three local forces: collision avoidance, velocity matching and flock centering. That is, a bird in a flock
- pulls away before it crashes with a neighbor.
- tries to go about the same speed as its neighbors in the flock.
- tries to move towards the center of the flock as it perceives it.
Implementing just these three simple rules, Reynolds' programs showed very realistic flocking behavior, with coherent clusters of simple agents called 'boids' whirling through the three-dimensional simulated space, splitting to flock around obstacles and rejoining again.
The simulation above is a simplification of the original algorithm by Reynolds, to adapt it to a two-dimensional simulated space. Birds in the simulation start at random positions, and form clusters as they fly around the unbounded simulated space. Eventually, when enough time has passed, the clusters merge to form a flock. New birds can be added to the simulation simply by clicking over the space.
How it works?
Each bird in the simulation is modeled as a set of 3-dimentional vectors: loc
, the vector that indicates position; vel
, the vector that indicats velocity; and acc
, the vector that indicates acceleration. Each bird has direct access to the whole scene's information, but flocking requires that it reacts only to flockmates within a certain small neighborhood around itself, characterized by a distance: flockmates outside this local neighborhood are ignored.

The flocking behavior requires that birds follow three simple rules that describe how an individual bird steers and maneuvers based on the positions and velocities of its nearby flockmates.
Rule 1: Separation
Each bird defines a security area around itself, defined by a circle with fixed ratio. Any bird inside the security area is considered "too close", and so the bird computes a vector that allows it to avoid crowding local flockmates.

Rule 2: Alignment
Each bird tries to follow the same direction that the flock is following. To do this, birds compute the average direction of their neighbors and steer themselves accordingly.

Rule 3: Cohesion
To keep the flock, birds need to fly together so they move towards the "center" of the flock as each of them perceives it. To do this, each bird computes the average position of all its neighboring flockmates and moves itself towards it.

Swarm Intelligence
Bird flocking is an example of a phenomenon known as swarm intelligence, the collective behavior of decentralized, self-organized systems composed of simple agents (named 'boids' after Reynolds' work) interacting locally with one another and with their environment. The inspiration often comes from nature, especially biological systems. The agents follow simple rules (like the three rules exposed above) and the interaction between agents lead to the emergent of "intelligent" global behavior, unknown to the individual agents.
Swarm intelligence-based techniques are used in a wide number of applications, for example the U.S. military plans to used swarm techniques for controlling unmanned vehicles, the European Space Agency is thinking about an orbital swarm for self-assembly and defense, and some researchers discusses the possibility of using swarm intelligence to control nanobots within the human body for the purpose of killing cancer tumors. Swarm intelligence has also been used in everyday life applications, such data mining, network routing, and flight planning.
Sources
All of the simulation's code is written on a special language called Processing, which is later translated into JavaScript by the Processing JavaScript library. You can download and examine the original Processing source code here.
The code requires JQuery 2.1.4 or higher and Processing 1.4.1 or higher to work.