top of page

Flocking + Obstacle Avoidance

The video demonstrates my latest programming assignment. Whenever the user clicks the space bar down, a variable number of agents will spawn and begin running a weighted flocking steering algorithm. Each agent runs the algorithm indevidually, and when they all work together it gives the appearance of flocking as seen below.

The algorithm is composed of three main simple parts.

  • Cohesion​​​​​​

    • Each agent checks the surrounding area for other agents and gets the direction to move them closer together​

  • Seperation

    • Opposite of cohesion, where each agent gets the direction to move away from the group​

  • Alignment

    • Each agent looks at the surrounding agents and gets the direction that aligns itself with the group​

The flocking algorithm gets each of these directions as a simple vector and combines them, scaling each by their specified weight (shown in the top right corner of the demo). This scaling is what makes this a weighted flocking algorithm. Changing the weights, as shown in the full video, will change how strongly each of the aspects factors in. 

Once I had flocking working, I had then to implement obstacle avoidance. This was done fairly similarly to my previous assignment, where I used raycasting to detect a collision and swerve the agent out of the way, overriding the flocking algorithm. This lead to some issues, where while in large groups, the momentum of the surrounding agents that didn't detect a collision would stop the agent from turning out of the way. I solved this by implementing code that, whenever any agent detects a collision, it will find the surrounding agents and send them the collision location, so that they can turn themselves without ever needing to indevidually detect collision. The long raycasts allow the agents to detect collision long before it occurs and turn away well before they hit.

Once I had that functional, it was only a matter of testing and error to get it to the point where large flocks of agents could manuver around and avoid obstacles with minimal collision. If I were to polish this more in the future, I would fix some issues with the obstical avoidance. It works well enough, but can be buggy at times. 

Flocking1.gif
ObstacleAvoidance1.gif
Avoidance1.gif
bottom of page