This was a two part assignment for our Artificial Intelligence class. The first part was to make flocking AI, the second part was to add in collision avoidance. This assignment has a group of space ships flying around in an asteroid belt. The ships will flock together depending on their weights and try to avoid the asteroids floating around.
The flocking consists of three steering behaviors that are combined on top of a wandering behavior. This has the ships wander around the map while sticking together.
The first of the steering behaviors is group alignment. A boid in the flock will check the rotation of its surrounding neighbors as well as their velocities. The boid will then take averages of both and try to match its own velocity and rotation to the average of its group of neighbors.
The second steering behavior involved for flocking is cohesion. With cohesion, the boid will find the average center position of its neighbors and try to fly towards that position.
The separation behavior is a little more complex than the other two. Most of the time the separation steering does not affect the boid unless it gets too close to another. The boid will check its neighbors and if the distance between it and a neighbor passes a threshold of closeness, an acceleration vector will be applied to the boid in the opposite direction of its neighbor. The strength of the vector depends on how close the boid is to its neighbor once it is within the threshold.
These steering behaviors are then combined and weighted so the effect of each can be adjusted to find an appealing flocking like behavior.
The other portion of this assignment was the collision avoidance with cylindrical objects. In this case it happens to be with asteroids moving around in space. Using vector math the units will check ahead of their current path for any asteroids and change their course to try to avoidance.