AI: aiming towards a moving target

I would like to show a possible solution that I worked on for the aiming towards a moving target problem. Let P be the player, E the enemy, S the shot. P shoots towards E, but in the meantime E may be moved.

Let’s put ourselves in the reference system of P and consider polar coordinates. Let’s calculate the normal velocity of E against P. Let’s assume that the distance between P and E is constant over time.

We can compute the time that S takes to reach E from P. Then, we can use this time to compute the new angle of E against P.

In this way, we are able to make a rough prevision of where E will be when it will be reached by S. P just needs to shoot S in that direction.

Enjoy!