Home Up Feedback Search

Articles & Tips
Recommended Links


Other Links:



About the Flocking Demo

The Flocking demo allows you to play with the four rules of flocking: separation, alignment, cohesion, and avoidance. Separation means that each member, or boid, of a flock tries to keep a minimum distance from every other boid in the flock. Alignment means that each boid tries to go in the same direction as the rest of the flock. Cohesion means that each boid tries to get as close as possible to the rest of the flock (huddle together).  Finally, Avoidance means each boid tries to not get too close, or avoid, boids in other flocks. From these four simple rules of flocking emerges apparently life-like behavior for a flock.

The flocks in the demo represent schools of fishes (actually, they are arrows but it looks so much like schools of fishes that that is what I am calling them :) ). Each flock is drawn in a different color.

The demo allows you to:

bullet View a 2D simulation of one flock or multiple flocks
bullet Run or step through a simulation
bullet Set the number of flocks (1-10)
bullet Set the number of boids (members) of each flock (2-100)
bullet Toggle on/off any flocking rule
bullet Set the relative strength of the rule on each member of the flock

This software is freeware. You are authorized to duplicate and modify this software. No guarantees or warranties.

This demo borrows concepts and algorithms from Steven Woodcock's article, "Flocking: A Simple Technique for Simulating Group Behavior," which is found in the book, Game Programming Gems (a great book by the way).


About the Source Code

There are three .pas files in the project.

bullet Vector.pas provides a simple vector class for such things as orientation, velocity, and position 
bullet FlockMain.pas is the main application for the demo 
bullet Flock.pas is the flocking code and is the meat of the demo.

There are three main classes to the flocking code: TFlockWorld, TFlock, and TBoid.

The TFlockWorld class represents the world within which the flocks live. As such, it tracks every flock in the world as well as define the characteristics of the world (mostly boundaries).

The TFlock class represents the flock and is made up of a collection of TBoid objects. The TFlock class is responsible for tracking every member in the flock as well as defining the behaviors the flock members should execute. It also tracks the averaged center position and velocity of all the flock members (used with alignment and cohesion). The flock is also responsible for freeing the boids in its flock.

The TBoid class represents a single entity in the flock and is the guts of the flocking code. Each boid has a position, velocity, and speed. Every step of the simulation, the TFlock class calls the Update method of the boid. In this method, the boid moves based on the flocking rules.

I recommend reading Steve Woodcock's article in Game Programming Gems to properly understand the source code. It should be noted that the flocking behavior in this demo differs from Steve Woodcock's in a couple ways (besides being modified for Delphi constructs and my style of coding). Steve's boids track an arbitrary number of "friends" and "enemies". His alignment, cohesion, and avoidance rules use this localized knowledge to manipulate the direction and speed of the boid. In the alignment rule for this demo, each boid tries to align with the averaged velocity vector of the entire flock (instead of a localized group). Even though Steve's demo is more "realistic," this code doesn't do this to simplify the classes and code for demo purposes. Similarly, in the cohesion rule, this demo looks at the averaged center point for the entire flock.

Also includes the Formation Flocking demo.

Source and Executable (Delphi 6, 7, 8, 2005, 2006, and 2007)

Sharon Rab has kindly provided a C# port of our flocking code.  It requires TeeChart for C#.  For further information, please contact sharonr2011 AT gmail.com (RiverSoftAVG is not responsible for, and does not make any representation, warranty, or condition concerning this software. Any support questions should be directed to Sharon Rab)


Send mail to webmasterNO@SPAMRiverSoftAVG.com with questions or comments about this web site.
Copyright © 2002-2016 RiverSoftAVG
Last modified: January 19, 2014