Formation Flocking
Home Up Feedback Search

Articles & Tips
Recommended Links


  Other Links:
Delphi CodeSmith Blog 

About the Formations Flocking Demo

The Formations Flocking demo extends the flocking demo to add formations
(squares, wedges, etc). The demo itself allows you to play with the parameters
controlling the formation flocking. The formation flocking code is based
on the four rules of flocking*: separation, alignment, cohesion, and avoidance.
The Formation Flocking code modifies the standard cohesion and separation rules.
The cohesion rule now moves the boid towards its position in the formation.
The separation rule has been modified so that it doesn't apply when the boid
is in its place in formation. In addition, the separation strength has been
turned way down to avoid the boids from pushing away from the formation.
Finally, when a boid gets into position, it stops.

Each flock is drawn in a different color.

The demo allows you to:

· View a 2D simulation of one flock or two flocks. The second flock
is added just to have enemies to avoid. They do not move.
· Run or step through a simulation
· Set the formation: wedge, square, etc
· Direct the formation to follow the movement of the mouse. The formation
will rotate to face the mouse and then move towards the mouse.
· Set the number of boids (members) of each flock (2-100)
· Toggle on/off any flocking rule
· 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).

* The four rules of flocking are: 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.

How the Formations Work

Formations work by overriding the cohesion rule for the boids. The standard
flocking cohesion rule (at least how it is implemented in the TBoid class)
works by giving the boid the "desire" to go towards the center of the
flock (which is calculated each turn). However, with a formation flock,
when you specify a formation, the TFormationFlock class assigns a position
and orientation offset from the flock's center for each boid. The
TFormationBoid class' Cohesion Rule attempts to go towards its assigned
position in the flock.

For example, if the formation is set to SQUARE, the TFormationFlock class
calculates the orthogonal vector to its (the group's) orientation. This
orthogonal vector is used to spread the assigned positions of the members
of the flocks in a square. If a flock was facing due north, the flock
class would calculate the east-west vector, the number of rows and columns
needed, and assign positions:

|| Flock's Orientation

B01 B02 B03 B04 B05
B06 B07 B08 B09 B10
B11 B12 B13 B14 B15

As long as the flock's orientation is not changed, the boid's offset position
and orientation are *not* changed. These values are always *relative* to the
flock's position and orientation.

Note that with a TFormationFlock, a desired position have been added.
This allows the flock to wheel the formation to face the desired position
(from its current position) and then advance towards the desired position.
By this device, the flock can advance while keeping formation. Also note
that the speed of the flock is *less* than a boid's speed. This allows
boids to keep their position more easily and also to "run and catch up" with
the rest of the formation. It also has the added benefit of making a lot
of real-world sense since a group moving in formation is slower than just
one individual.

Part of the Flocking demo.

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


Send mail to with questions or comments about this web site.
Copyright © 2002-2016 RiverSoftAVG
Last modified: September 20, 2010