This project was to implement Obstacle Avoidance with Flocking. This project was assembled over the course of two weeks in the Unity Engine. This above video is a quick demonstration of how I used a previous obstacle avoidance project to add to the algorithm.
For those who are not familiar Flocking is a combination of three different steering algorithms, Alignment, Cohesion and Separation. Each algorithm usually outputs some sort of Vector or an equivalent data structure. The data structures I used in my implementation were vectors so each steering algorithm outputs a Vector2 (since my implementation is in 2D).
Alignment works to make sure all boids, the term used to denote agents using Flocking, are facing the same direction. Cohesion steers a boid towards a common location of surrounding boids bringing them close together. Separation makes sure the boids aren’t too close together so they don’t hit each other. These three steering patterns are then combined together in another algorithm called Weighted Blending. Weighted blending takes each of the steering algorithms and separates the components of the returned vectors into their individual components, X and Y (if my implementation was in 3D then there would be a Z component). Each steering algorithm’s component would then be multiplied by a scalar value or a weight. After being multiplied all steering algorithms would then be added together to create a new vector component to be applied to the boid.
The implementation with obstacle avoidance is as easy as adding another weight and vector component to the calculations. Adjusting the weights would be the trickiest since you need find a balance that would work well. The combination is simple if your have an understanding of both algorithms. An additional change I made to the Flocking algorithm was a part that detects how far from the center each boid is and if they are too far way they are directed back towards the center.
My implementation has a few features to help show what is going on. The first is UI to help adjust weights while the project is running. There is an option to hide the UI to better see how the flocking changes with weight changes. Each boid keeps track of how many times they have collided with something. They are colored green if they have have less than four collisions, yellow is they’ve collided more between four and seven time and if they have collided with objects eight or more times they turn red.