top of page
  • Adel Talhouk

AI Agents - Smart Flocking

Updated: Dec 17, 2020

This AI technique is a combination of Flocking (a technique that Craig Reynolds created in 1986) and Dynamic Obstacle Avoidance (a technique that has many different implementations). Below is a link to the GitHub repository where you can see and download my demo. Please read the README.md file for instructions on how to control the Boids (this is the name that Mr. Reynolds gave to the Flock Agents) and the Obstacles.





In my demo, there are boids flocking around in a certain radius, and more can be instantiated when the press of the space bar. If you click the left mouse button, a circular obstacle will spawn in the scene, and clicking the left mouse button again will move the obstacle to the location of the mouse (the mouse button can be held to move the obstacle along with the mouse). If a boid comes close enough to the obstacle, it will move away from it, and continue with its flocking behaviour. Pressing the 'W' key will de-activate the circular obstacle, and will activate a square-shaped one. The controls and boid behaviour are the exact same, the only difference being the shape of the obstacle. Clicking 'W' will swap which obstacle is active, and pressing the right mouse button will de-activate whichever obstacle is on screen, allowing the boids to freely flock together.


The flocking behaviour is composed of 5 different steering behaviours: Cohesion, Separation, Group Alignment, Avoid Edge, and Avoid Obstacle. The first three are the essentials of flocking, and are described in the article by Craig Reynolds (linked below). Avoid Edge Steering is simply to ensure that the boids flock within a certain radius, allowing them to remain on-screen instead of giving them the liberty to flock to any part of the world (as the camera remains stationary). Finally, Avoid Obstacle Steering controls the logic for detecting and avoiding obstacles.


Cohesion Separation Group Alignment

Obstacle Detection


As long as a boid detects an obstacle in its neighbourhood radius, it casts a ray forward. This ray is used to check if it hits an obstacle or not, determining whether or not the boid will collide with an object in front of it. If the ray hits an obstacle, we calculate the normal to the surface at the point where the ray hit the object. We then use this normal to determine which side the boid should steer towards in order to most effectively avoid the obstacle. If the normal is to the left side of the ray, then the boid must steer left (the same logic applies to the right side).



 

I envision this technique to be used in large open-world games to simulate a realistic flock or herd of animals. This technique is not essential to gameplay, however it does add to the atmosphere of the game, as it can be implemented in the background of outdoor scenes instead of leaving them empty. This would make the world more believable and increase the players' suspension of disbelief.


That being said, however, some design decisions I made render this technique pretty CPU power-hungry: every frame, each boid is checking for other boids in its neighbourhood radius, and the raycasting to detect obstacles, despite only happening if an obstacle is in the radius, can slow down the framerate significantly with larger boid radii. For that reason, I recommend this technique be implemented for powerful PCs or consoles with very capable processors.


I will, however, be iterating on this technique with the goal of greatly increasing efficiency. All changes will be uploaded to the repository, and this article will be modified to explain how the most current and up-to-date approach works.






References:

  • Flocking (Craig Reynolds) - The original article explaining the essence of AI Flocking.

  • Flocking in Unity - This tutorial series helped me debug my code, as parts of my original approach were not working as intended.

  • Collision avoidance - Explains how collision avoidance works and how units can see ahead to detect obstacles.

  • Obstacle avoidance video - Links to the timestamp explaining the raycast approach to detecting obstacles and how a unit should steer to avoid the obstacle ahead.

65 views0 comments
bottom of page