|
|
2 months ago | |
|---|---|---|
| .. | ||
| .config | 2 months ago | |
| .vscode | 3 months ago | |
| Core | 2 months ago | |
| Platforms | 2 months ago | |
| Flocking.sln | 3 months ago | |
| FlockingSample.cs | 3 months ago | |
| README.md | 3 months ago | |
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:
Platforms/Windows)Platforms/Desktop)Platforms/Android)Platforms/iOS)Terminal > Run Task...) to build or run:
build-windows, run-windowsbuild-desktopgl, run-desktopglbuild-android, run-androidbuild-ios.vscode/launch.json to debug Windows or DesktopGL.Flocking.sln in Visual Studio..xnb files 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 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. 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 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. 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.