# How to calculate shot angle and velocity to hit a moving target?

I am developing a 2D Android game and I am making an aiming algorithm for AI projectiles to hit enemies either following a path, or free moving. At the moment it just calculates where the target will be after a distance and fires a projectile to meet it at that distance. Of course this means varying the projectile speed to meet the target.

Does anyone have any tips for a simple-ish algorithm (optimal-ish) to calculate when the projectile needs to fire and where it needs to aim if it can only travel at a constant velocity? Say the projectile goes twice the speed of the target?

The only way I can think of involves searching and seems quite large.

In a tower defence game I made, I used a quadratic equation to predict the intersection and thus aim point. The following aiming code snippet assumes the enemy is traveling at a constant speed and direction. It also assumes the projectile will be traveling at a known constant speed (could be any speeds but must be known to the algorithm).

Vector totarget =  target.position - tower.position;

float a = Vector.Dot(target.velocity, target.velocity) - (bullet.velocity * bullet.velocity);
float b = 2 * Vector.Dot(target.velocity, totarget);
float c = Vector.Dot(totarget, totarget);

float p = -b / (2 * a);
float q = (float)Math.Sqrt((b * b) - 4 * a * c) / (2 * a);

float t1 = p - q;
float t2 = p + q;
float t;

if (t1 > t2 && t2 > 0)
{
t = t2;
}
else
{
t = t1;
}

Vector aimSpot = target.position + target.velocity * t;
Vector bulletPath = aimSpot - tower.position;
float timeToImpact = bulletPath.Length() / bullet.speed;//speed must be in units per second


Since it also determines time of impact I simply waited until the time pasted to call the impact graphics at the position of the target at that time... no need for any collision detection to determine hits.

• "no need for any collision detection to determine hits." - If the target(s) are constrained to maintaining their speed and direction. --- If, however, they have an active AI, or worse, are the player, and are able to move after the shooter has fired, then basing your hits on time rather than collision-detection will lead to what your player-base will perceive as some nasty bugs/glitches. – XenoRo Jul 10 '15 at 15:25
• @TheLima yup, just like it says in the 2nd sentence of the answer. ;-) – Steve H Jul 10 '15 at 22:14
• This is almost perfect for my scenario, but how would you factor in these 2 additional variables: acceleration for the projectile, the movement of the tower itself? – Jack Nov 25 '15 at 19:44
• What is a is 0? This will produce a division by zero exception, but what does this mean in terms of the variable t? should it be considered 'a very big number' or what would the best case be? – firelynx Jun 4 '16 at 19:28
• Is there a good book that goes into the intuition behind this? I'm curious how you knew to look for this math... – davidkomer Jun 8 '18 at 8:06

The AI, the target's location during the time the projectile is fired, and the target's eventual location at point of death form a triangle.Here is what you should already know:

1. Side length a, which is the projectile speed
2. Side length b, which is the target speed
3. The angle of motion of the target of motion of the target.

You have three parts of the triangle, a SSA case, so solve it like this

1. Find angle B based on the the angle of motion and the location of the AI
2. Use the law of sines to find angle A

Angle A should allow you to determine the angle at which the projectile should be launched.