1. Programming an "aimbot"

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 &quot;aimbot&quot; 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 &#91;\frac{v_T (\triangle x sin\phi - \triangle y cos\phi)}{v_P\sqrt{(\triangle x)^2 + (\triangle y)^2}} \right &#93;$

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?

2. The identity $\displaystyle a\sin x + b\cos x = \sqrt{a^2 + b^2} \sin(x + \tan^{-1}(\tfrac{b}{a}))$ is only correct if a>0. If a<0 then it becomes $\displaystyle a\sin x + b\cos x = \sqrt{a^2 + b^2} \sin(x + \tan^{-1}(\tfrac{b}{a}) + \pi)$. Similarly, if $\displaystyle y = \sin x$ it does not necessarily follow that $\displaystyle x = \sin^{-1}y$. In your case, the condition for this to hold is $\displaystyle \cos x > 0$. If $\displaystyle \cos x<0$ then again you have to add $\displaystyle \pi$ to the angle, to get the vector pointing the right way.

I think that the quickest way to ensure that your formula works in all four quadrants is to define $\displaystyle \psi = \tan^{-1}\bigl(\tfrac{\Delta y}{\Delta x}\bigr) + \bigl\langle \Delta x<0\bigr\rangle*\pi$, where the angled brackets denote a Boolean expression taking the value 1 if $\displaystyle \Delta x<0$ and 0 otherwise. Then define $\displaystyle \theta = \psi + \sin^{-1}\left [\frac{v_T (\Delta x \sin\phi - \Delta y \cos\phi)}{v_P} \right ] + \bigl\langle\cos(\theta-\psi)<0)\bigr\rangle*\pi$.

I think that should work in each quadrant, but it could still go wrong in the exceptional cases where $\displaystyle \Delta x=0$ or $\displaystyle \cos(\theta-\psi)=0)$.

3. Ah yes, I forgot about that condition. Thanks for pointing it out!

On a similar note, do you think it would it be necessary to account for all values of $\displaystyle \phi$ in similar fashion?

If $\displaystyle \phi$ were to fall in say, the second quadrant, then $\displaystyle \sin\phi > 0$ and $\displaystyle -\cos\phi > 0$.
Still, it's not immediately obvious to me given that $\displaystyle \frac{v_T}{v_P} < 1$ by default (well, barring one heckuva fast tank), that the entire expression $\displaystyle \frac{v_T(\triangle x\sin\phi - \triangle y \cos\phi)}{v_P\sqrt{(\triangle x)^2 + (\triangle y)^2}}$ will evaluate to a value whose magnitude is less than 1, thus allowing us to evaluate $\displaystyle \sin^{-1}\frac{v_T(\triangle x\sin\phi - \triangle y \cos\phi)}{v_P\sqrt{(\triangle x)^2 + (\triangle y)^2}}$ when $\displaystyle \phi$ lies in the second quadrant.

I'll try coding what you've done once the weekend is over. With any luck, it should work. ^^

Edit: I forgot to divide the final expression by $\displaystyle \sqrt{(\triangle x)^2 + (\triangle y)^2}$ both here and in the original post. Now, at least it's a lot more plausible that $\displaystyle \frac{v_T(\triangle x\sin\phi - \triangle y \cos\phi)}{v_P\sqrt{(\triangle x)^2 + (\triangle y)^2}} < 1$ in all quadrants.