Trying to make my AI hit people. So I need a formula to know the time until my projectile will hit the target and also the direction the projectile should be shot at.
Here is an example scenario.
- The projectile shoots from the origin $(0,0)$
- Both projectile and target will move in a straight line.
- Speed of projectile: $v = 2000$ m/s
- Speed of target: $u = 450$ m/s
- Initial position of target: $x = 6000$ m, $y = 0$ m
- Direction target moves: $b$ (above $+x$ axis). $b = 135^\circ$
Unknown variables.
- Time projectile reaches target: $t$
- Angle of projectile: $a$
Here are 2 equations made from the data.
- $(v \cos a)t = x + (u \cos b)t$
- $(v \sin a)t = y + (u \sin b)t$
How do I get the $t$ and $a$, but mostly I just need the $t$?
The easy step is to eliminate $a$ by squaring your last two equations, and summing them up. You are using $\sin^2 a+\cos^2a=1$. You obtain $$v^2t^2=[(x+ut\cos b)^2+(y+ut\sin b)^2]$$ This is a quadratic equation in $t$, which is easy to solve. Make sure you use the positive solution.