Programming Specialism: AI Hitting Moving Targets

As my first programming specialism brief, I decided to stretch myself and go for one of the advanced options on the list; programming an automatic aiming system for projectiles. This particular brief did not require the target to have acceleration (thank GOD because that would’ve made it so much harder), instead my only problem was trying to predict at exactly what point both the target and the projectile could collide.

In this specific example, I will be controlling a player which has a stationary turret constantly shooting projectiles towards it. These calculations are for the projectiles to hit the player.

The first equation used is d (distance) = s (speed) * t (time). Assuming that the projectile and the player will collide, the distance that the projectile travels can be found in terms of t. This can then be used to calculate the player’s position at the same time, t.

Considering two points (X,Y) and (Tx, Ty), you can calculate the distance (D) between them using the equation D² = (X – Tx)² + (Y – Ty)². Using the first equation, we can get D² = s² * t² (by squaring both sides). We can then equate the two equations we have to get:

s² * t² = (X-Tx)² + (Y-Ty)²

As we know the velocity and position of the player, we can calculate the X and Y co-ordinates at time t (calling the velocity (Vx, Vy) and the position (Px, Py)):

X = t * Vx + Px

Y = t * Vy + Py

And now, we can substitute the X and Y values into the previous equation to get a (very long) quadratic equation:

s² * t² = ((t * Vx + Px) – Tx)² + ((t * Vy + Py) – Ty)²

Through expanding and combining like terms, we can re-arrange it into the form ax² + bx + c = 0, where x = t. This will allow us to use the quadratic equation to find t, which we can then use to find the collision position. The equation formed is:

(Vx² +Vy² – s²) * t² + 2* (Vx*(Px – Tx) + Vy*(Py – Ty)) *t + (Py – Ty)² + (Px – Tx)² = 0

Therefore we have:

a = Vx² +Vy² – s²

b = 2* (Vx* (Px – Tx) + Vy * (Py – Ty))

c = (Py – Ty)² + (Px – Tx)²

using these, we can see how many (if any) collisions there can be using the discriminant (b² – 4ac). If the discriminant is less than zero there are no collisions. This means we will just ignore this cycle and try again the next time the calculation is run. If it is greater than or equal to zero, there is at least one collision. In this case, we can calculate t using the quadratic formula:

t = (-b ± √(b² – 4ac)) ÷ 2a

Taking the largest value of t, we can then calculate the player’s position at time t, which will be the position that the projectile is able to reach the player at. We can then move the projectile towards said position, and the player will be there at the same time as the projectile. This position can be calculated from the equations mentioned earlier (as we now know all of the unknowns except X and Y:

X = t * Vx + Px

Y = t * Vy + Py

And that’s pretty much it. This was a pretty fun challenge to have a go at, and I’m glad I did because it’s certainly taught me a lot. Hopefully the other briefs are just as interesting.

Leave a comment