Flocking Sample
This sample demonstrates how AIs can use simple rules to move together and create complex behaviors.Sample Overview
When programming the AI for your game, you often want your actors to move and react together without having to behave identically. For example, you might want to simulate a school of fish that all swim together without a centralized control or a battalion of soldiers that can march together in formation around obstacles. This sample demonstrates some of these behaviors. The sample has a flock of birds that fly to, and in the same direction as, other birds they see nearby. The sample also has a cat that you can turn on and who then chases the birds as they run away. This sample is based on the Chase and Evade sample, and assumes that the reader is familiar with the code and concepts explained in that sample.
This sample uses the following controls.
Action | Windows Phone | Windows - Keyboard Control | Windows/Xbox - Gamepad Control |
---|---|---|---|
Select the tuning parameter. | DRAG tuning bar | UP ARROW, DOWN ARROW | D-Pad Up and Down |
Increase/decrease the tuning parameter. | DRAG tuning bar | LEFT ARROW, RIGHT ARROW | D-Pad Left and Right, Left and Right Triggers |
Reset the bird flock. | TAP "Reset Flock" button | X | X |
Reset the tuning parameters. | TAP "Reset Distance" button | B | B |
Add/remove the cat | TAP "Add/Remove Cat" button | Y | Y |
Move the cat. | TAP or DRAG on screen | W, S, A, D | Left Thumbstick |
Exit the game. | BACK | ESC or ALT+F4 | BACK |
How the Sample Works
Flocking Behavior
Flocking behavior is controlled by three simple behaviors: cohesion, alignment, and separation. Other behaviors can be present, but they are not required. In this sample, the birds also have a flee behavior.
Cohesion is the birds’ tendency to fly together with others. Alignment is the birds’ tendency to fly in the same direction as other birds. Separation is the birds’ tendency to fly away from others that are too close. Fleeing is the birds’ tendency to avoid dangerous things.
Cohesion
Each bird flies towards others it can see. To make one bird fly closer to another, for each other bird inside its detectionDist value, the bird changes its direction towards the other bird in proportion to its moveInFlockDirInfluence setting and according to how close it is to the midpoint between its detectionDist and separationDist values.
Alignment
Each bird flies in the general direction of others it can see. To make one bird line up with another, for each other bird inside its detectionDist value, the bird adds the direction the other bird is facing to its own direction in proportion to its moveInFlockDirInfluence setting.
Separation
Each bird flies away from others that that are too close. For each other bird inside both its detectionDist and its separationDist values, the bird applies the separation rule instead of the cohesion rule. To move one bird a comfortable distance away from another, the bird adds the opposite of the direction towards the other bird's direction to its direction in proportion to its moveInFlockDirInfluence setting and according to the ratio of how close the other bird is relative to its separationDist value.
Flee
Each bird flies away from the cat if the bird can see the cat. To move a bird away from the cat, if the cat is inside the bird's detectionDist and the bird isn't already moving away from the cat, the bird adds the opposite of the direction towards the cat to its direction value.
Extending the Sample
- Make one bird in the flock the leader and give it the wander behavior from the Chase and Evade sample. Then make the other birds in the flock follow the leader.
- Add different colored flocks and make the birds fly with their own kind and avoid other birds.
- Add formations.
- Add predator birds that chase the others.