This sample demonstrates how AIs can use simple rules to move together and create complex behaviors.
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 project supports the following platforms:
- Windows (
Platforms/Windows) - DesktopGL (
Platforms/Desktop) - Android (
Platforms/Android) - iOS (
Platforms/iOS)
- .NET 8 SDK
- MonoGame 3.8.*
- For Android/iOS: Visual Studio 2022+ with Xamarin/MAUI workloads, or JetBrains Rider
- Open the root folder in VS Code.
- Use the built-in tasks (Ctrl+Shift+B or
Terminal > Run Task...) to build or run:build-windows,run-windowsbuild-desktopgl,run-desktopglbuild-android,run-androidbuild-ios
- Use the launch configurations in
.vscode/launch.jsonto debug Windows or DesktopGL.
- Open
Flocking.slnin Visual Studio. - Set the desired platform project as the startup project.
- Build and run as usual.
- Android and iOS require appropriate emulators or devices and platform SDKs.
- Content is pre-built as
.xnbfiles and does not require a content pipeline build step.
| 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/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 |
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: Each bird flies towards others it can see. For each other bird inside its
detectionDistvalue, the bird changes itsdirectiontowards the other bird in proportion to itsmoveInFlockDirInfluencesetting and according to how close it is to the midpoint between itsdetectionDistandseparationDistvalues. -
Alignment: Each bird flies in the general direction of others it can see. For each other bird inside its
detectionDistvalue, the bird adds thedirectionthe other bird is facing to its owndirectionin proportion to itsmoveInFlockDirInfluencesetting. -
Separation: Each bird flies away from others that are too close. For each other bird inside both its
detectionDistand itsseparationDistvalues, 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 itsdirectionin proportion to itsmoveInFlockDirInfluencesetting and according to the ratio of how close the other bird is relative to itsseparationDistvalue. -
Flee: Each bird flies away from the cat if the bird can see the cat. If the cat is inside the bird's
detectionDistand the bird isn't already moving away from the cat, the bird adds the opposite of the direction towards the cat to itsdirectionvalue.