AI swarming behavior

In this game we wanted to give the player a real Hack n' Slash experience, with a lot of enemies to grind through. To make this feel good we decided to implement a swarming behavior for the enemies. I started by implementing a basic BOID-like system with alignment, separation, and cohesion. This worked great when the enemies were moving around and roaming freely, but when they were attacking the player they could not wrap around to the other side of the player to attack. It was time to find a solution.

No swarming

With the normal BOID behavior enabled, the AI tries to reach the player, but the separation algorithm is pushing them away from each other. This makes the AI in the back bounce back and forward trying to get into range of the player character, or, if disabled, making them just go through each other as seen here in the video clip.

Carousel imageCarousel imageCarousel image

Swarming Theory

To solve this problem I had an idea to calculate the difference between the two movement vectors affecting the AI; the separation vector and the "to player" vector, and applying this to make the AI go around other AI actors. That would make the AI walk in the vector perpendicular to the two influencing vectors. As the AI gets closer to the other AI actors, the opposite force gets stronger and the perpendicular vector will be stronger. When the AI is far away from other AI actors, the perpendicular vector will be weak enough for it to have no noticeable influence over the actor. Time to put it in code!

First iteration of wrap around.


Wrap around algorithm refined.


Adjusted weights for a better behavior.


Almost there. Next thing to implement is behavior for AI that can't reach the player.

Swarming In Action

This worked good enough for me to spend time adjusting the algorithms and weights, and keep iterating until it looked good. What worked best in the end was to adjust the weight of the AI movement before it adjusted for other AIs. You can see the direction vector (yellow line) adjusting in the gifs as the AI wraps around.