Hello! I'm not quite sure where this topic should go in the pre-university or university forums, but figured this would be the most appropriate place in the end, only because while I'm using the determinant of a matrix, you should get the same result if you rearranged and substituted to solve for simultaneous equations. Anyway, I'm trying to program an "aimbot" for a simple 2D game.

Here's the scenario.

You are riding around in a tank at position P. Your target is at point T, moving in a direction $\displaystyle \phi$ with speed $\displaystyle v_T$.

Given that the speed of your projectile is $\displaystyle v_P$, find the angle $\displaystyle \theta$ you should aim at such that if your projectile will hit the target if you were to fire now.

Here's a diagram to help illustrate:

In other words, find theta in terms of all the other variables.

Here's what I have so far:

$\displaystyle a = tu - tv$

thus, $\displaystyle a = t(u-v)$

The two vectors $\displaystyle a$ and $\displaystyle u-v$ are linearly dependent (by t), so the determinant of the matrix formed by $\displaystyle a$ and $\displaystyle u-v$ is 0.

Thus, expanding into components, I get

$\displaystyle detM = det\begin{pmatrix}\triangle x&v_P cos\theta - v_T cos\phi\\\triangle y&v_P sin\theta - v_T sin\phi\end{pmatrix}=0$

$\displaystyle \triangle x(v_P sin\theta - v_T sin\phi) = \triangle y(v_P cos\theta - v_T cos\phi)$

Gathering up all the theta components, I have:

$\displaystyle v_P (\triangle x sin\theta - \triangle y cos\theta) = v_T (\triangle x sin\phi - \triangle y cos\phi)$

Thus,

$\displaystyle \triangle x sin\theta - \triangle y cos\theta = \frac{v_T (\triangle x sin\phi - \triangle y cos\phi)}{v_P}$

I can then use the trigonometric identity,

$\displaystyle asin x + bcosx = \sqrt{a^2 + b^2} sin(x + tan^{-1}(\frac{b}{a}))$

So if I let $\displaystyle a = \triangle x$ and $\displaystyle b = -\triangle y$, then I get the following hideous expression:

$\displaystyle \sqrt{(\triangle x)^2 + (\triangle y)^2}sin(\theta - tan^{-1}(\frac{\triangle y}{\triangle x})) = \frac{v_T (\triangle x sin\phi - \triangle y cos\phi)}{v_P}$

So as a final expression,

$\displaystyle \theta = tan^{-1}\bigl(\frac{\triangle y}{\triangle x}\bigr) + sin^{-1}\left [\frac{v_T (\triangle x sin\phi - \triangle y cos\phi)}{v_P\sqrt{(\triangle x)^2 + (\triangle y)^2}} \right ]$

This looks correct, but when I coded it up, it only seemed to work if the target lay within the first quadrant.

Does anyone have any ideas on how to get around this without having to deal with things on a quadrant-by-quadrant basis?