Implementation of Craig Reynold's Boids program in object-oriented python. A flock of birds follow three basic rules of behavior:
- Avoidance: Birds will keep a minimum distance to other birds
- Alignment: Birds will attempt to match the velocity of nearby birds
- Cohesion: Birds will move closer to nearby birds as long as it does not conflict with Avoidance
The program has been tested with python 2.7.3
and needs python-mathplotlib
installed for visualization. For generating movies (optional) a recent version of ffmpeg
is required.
The default parameters are tuned to give flocking behavior. Run the program from the root folder by
mkdir png
python ./run
The program should output a real-time visual representation of the positions of the birds and the predator looking like the picture below.
The black arrows represent normal birds and the red arrow represents the predator.The following parameters can be adjusted in the Bird
class in order to change flocking characteristics. Note that extreme values might break the simulation alltogether.
- nd: Neighborhood radius
- dd: Desired minimum distance to other birds
- pd: Desired minimum distance to predators
- sepmult: Scaling factor for the separation rule
- almult: Scaling factor for the alignment rule
- cohmult: Scaling factor for the cohesion rule
- premult: Scaling factor for predator avoidance
- alpha: Number between -1 and 1 determining cone of vision
The program will generate a PNG image in the subfolder PNG for every time the flock updates. A bash script is provided for generating an AVI movie file from the sequence of pictures. To run this:
chmod +x genmovie
./genmovie