top of page
Adel Talhouk

AI Agents - Smart Flocking V.2


This is the second version of the Smart Flocking algorithm I implemented. As a refresher, Smart Flocking is a combination of the Flocking algorithm (Reynolds) and dynamic (or real-time) obstacle avoidance. This implementation relies of Raycasting in order to detect obstacles that lie ahead of the AI Agents (I will be referring to them as Boids, as does Craig Reynolds).






The behaviour observed in the video demonstration is achieved through a weighed blending of 6 different steering behaviours: Cohesion, Separation, Group Alignment (the 3 alone make up the flocking behaviour), Obstacle Avoidance, Avoid Edge, and Swarm.


Cohesion Separation Group Alignment

Obstacle Detection


Boids cast a ray forward (visualised in green in the demo) each frame in order to detect incoming obstacles at a distance (this distance can be modified through the scriptable object that represents this Steering behaviour). 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).



As with the first version of the Smart Flocking algorithm I implemented, 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).


The final behaviour I implemented was a swarming behaviour. When the user clicks on the screen (Left Mouse button), a "food" gameObject is spawned. If boids are within a certain radius of the food (also modifiable through the scriptable object), they steer towards it and "take a bite out of it". If enough "bites were taken", the food then disappears, and we have the ability to spawn more. The logic for the food is in its own script, and it simply deducts from its health each time a Boid enters its collider. The boids themselves do not have any access to this logic, only to the swarm behaviour.


 

As with the last implementation of this behaviour, the weights for all steering behaviours can be modified through the weighed blending object (the Composite Steering object) in Unity's Inspector. As you can see through the demo, modifying these values while the simulation is running is reflected in the boids' real-time change of behaviour.


On my machine, the frame rate starts dipping below 60 Frames Per Second when I have spawned around 450 boids, and it drops temporarily when there are too many near each-other. An improvement I would like to implement in the future would be a Spatial Partitioning algorithm, and I would like to compare between that future implementation and this one in terms of number of boids I can spawn before the FrameRate takes a significant hit, or drops below 60.





References:

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

  • 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.

15 views0 comments

Comments


bottom of page