Dominique Louis 4d66891287 Ensure all Android entry points are called MainActivity 2 months ago
..
.config 177b9c2f2c Ensure all projects have a .config directory. 2 months ago
.vscode 787aa50223 History Reset and Projects updated to SDK and MG 3.8.* 3 months ago
Core 5a1fb288f4 Ensure all Core files have PreserveAsset set to all. 2 months ago
Platforms 4d66891287 Ensure all Android entry points are called MainActivity 2 months ago
Flocking.sln 787aa50223 History Reset and Projects updated to SDK and MG 3.8.* 3 months ago
FlockingSample.cs 787aa50223 History Reset and Projects updated to SDK and MG 3.8.* 3 months ago
README.md 787aa50223 History Reset and Projects updated to SDK and MG 3.8.* 3 months ago

README.md

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.

Building and Running

This project supports the following platforms:

  • Windows (Platforms/Windows)
  • DesktopGL (Platforms/Desktop)
  • Android (Platforms/Android)
  • iOS (Platforms/iOS)

Prerequisites

Build and Run (VS Code)

  1. Open the root folder in VS Code.
  2. Use the built-in tasks (Ctrl+Shift+B or Terminal > Run Task...) to build or run:
    • build-windows, run-windows
    • build-desktopgl, run-desktopgl
    • build-android, run-android
    • build-ios
  3. Use the launch configurations in .vscode/launch.json to debug Windows or DesktopGL.

Build and Run (Visual Studio)

  1. Open Flocking.sln in Visual Studio.
  2. Set the desired platform project as the startup project.
  3. Build and run as usual.

Notes

  • Android and iOS require appropriate emulators or devices and platform SDKs.
  • Content is pre-built as .xnb files and does not require a content pipeline build step.

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/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: 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.