Aiming Problem
So, yesterday my brother and I worked on an aiming problem for an AI. The problem was this:
A shooter wants to know where to aim based on his target's position, velocity, and projectile speed. I came up with the formula once I drew it as shown. I was thinking maybe I should try it in polar form, but I didn't have to. The real solution comes from thinking in terms of the distance from the origin (in 2d). Set the distance of the projectile from the origin equal to the distance to the target from the origin and you get a nice quadratic equation that can be solved. As shown there may be two points to aim at (if the projectile and target have close speeds).

As it says, the cone is the possible location of a shot fired at t=0.
To simplify we call the shooter's position the origin. Also assume the projectile is faster than the target, so we are guaranteed a solution.
Let the enemy position be x = (x1, x2). Let the enemy's velocity be v=(v1,v2). Let s be the bullet speed.
(We use |a| to mean the length of the vector a. |a| = sqrt(a1^2 + a2^2) ).
Target distance from origin is
So, yesterday my brother and I worked on an aiming problem for an AI. The problem was this:
A shooter wants to know where to aim based on his target's position, velocity, and projectile speed. I came up with the formula once I drew it as shown. I was thinking maybe I should try it in polar form, but I didn't have to. The real solution comes from thinking in terms of the distance from the origin (in 2d). Set the distance of the projectile from the origin equal to the distance to the target from the origin and you get a nice quadratic equation that can be solved. As shown there may be two points to aim at (if the projectile and target have close speeds).

As it says, the cone is the possible location of a shot fired at t=0.
To simplify we call the shooter's position the origin. Also assume the projectile is faster than the target, so we are guaranteed a solution.
Let the enemy position be x = (x1, x2). Let the enemy's velocity be v=(v1,v2). Let s be the bullet speed.
(We use |a| to mean the length of the vector a. |a| = sqrt(a1^2 + a2^2) ).
Target distance from origin is
|x + vt|
Projectile distance from origin is
ts
So when these are equal we have
ts = |x + vt|
which leads to
(ts)^2 = (x1 + v1t)^2 + (x2 + v2t)^2
0 = (v1^2 + v2^2 - s^2)t^2 + 2(x1v1 + x2v2)t + x1^2 + x2^2
a = v1^2 + v2^2 - s^2
b = 2(x1v1 + x2v2)
c = x1^2 + x2^2
t1 = -(b - sqrt(b^2 - 4ac))/(2a)
t2 = -(b + sqrt(b^2 - 4ac))/(2a)
use smallest t1 or t2 > 0, and call this t*.
Now we just use this time with our targets velocity to calculate the new position to aim at at this time. Location to aim at is y = x + vt*.
This was fun. Lets do it again!
(ts)^2 = (x1 + v1t)^2 + (x2 + v2t)^2
0 = (v1^2 + v2^2 - s^2)t^2 + 2(x1v1 + x2v2)t + x1^2 + x2^2
a = v1^2 + v2^2 - s^2
b = 2(x1v1 + x2v2)
c = x1^2 + x2^2
t1 = -(b - sqrt(b^2 - 4ac))/(2a)
t2 = -(b + sqrt(b^2 - 4ac))/(2a)
use smallest t1 or t2 > 0, and call this t*.
Now we just use this time with our targets velocity to calculate the new position to aim at at this time. Location to aim at is y = x + vt*.
This was fun. Lets do it again!

