These past weeks we decided that we wanted to have light spaceships fighting around the player. It is cool, adds some life to the game, and it is really fun to watch. But programming their fighting behavior wasn't so triffling!
I took on the task to implement those flying light spaceships. First, I used a plain dumb steering algorithm where the spaceship would take on a target and keep on trying to move towards them. The results were... well... painfully awful. The fighters would just wander around as clouds of flies, slowly circling around towards somewhere outside the screen.
Then, I decided to throw all of it away and use a physics based approach. The spaceships are simply attracted to their targets. It should work! But it didn't! They would either fly in an interesting circular motion (emergent behaviors are always awesome!) or collapse in small groups with spaceships slingshotting in and out. It was clear then that the best solution would be a steering algorithm with some brains to know where the possible targets are and dynamically decide what to do. For those more into AI, what I meant is that I started to build an steering algorithm inspired on Boids.
Many attempts came after, but here's my final spaceships movement algorithm:
A spaceship has its own acceleration, deceleration, max and min movement speeds. The min speed is needed to avoid "docking" on a stationary target - this is an undesirable behavior. To make the battles smooth and convincing, a ship has two turning speeds. The 'maneuver turn speed' is used when the spaceship is slow enough, and the 'hi-speed turn speed' us used otherwise.
The different turn speeds are very important to give a good visual feeling to the player. They won't allow a fast spaceship to do a sharp U-Turn, resulting in taking some distance before turning to attack an stationary target, and also allowing ships to dodge their pursuers (they won't be able to keep on the spaceship tail because it, being slower, will turn faster. The pursuer will then need more time to adjust it's own speed to be able to turn back to their target).
To keep the battles more chaotic, the spaceships will search for a new target every x seconds of time (or fragments) - even if the previous target didn't die. The new target is decided by the 'Distance Then Cone Target Fetcher Behavior', which will try to find the best target within an ideal distance from the ship, and closest to straight forward direction. This allows the ships to give up on some dodging target to aim at a new target that just happens to be within range and weapons angle.
The final challenge was to not allow the battles to move too far away from the player. This was solved with a guard position and distance. If the ships go too far away, they'll abort all engagement and come back within an ideal distance again.
Finally, Allan then fine-tuned the parameter so we could see fun battles. Phew. It was some work to do. But the results are neat! Here's a Web Build. Use left and right mouse buttons to spawn fighters! May the force be with you.