Results 1 to 3 of 3

Math Help - Programming an "aimbot"

  1. #1
    Junior Member
    Joined
    Nov 2009
    Posts
    51
    Thanks
    13

    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 "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 \phi with speed v_T.

    Given that the speed of your projectile is v_P, find the angle \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:
    a = tu - tv
    thus,  a = t(u-v)

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

    Thus, expanding into components, I get
    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

    \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:
    v_P (\triangle x sin\theta - \triangle y cos\theta) = v_T (\triangle x sin\phi - \triangle y cos\phi)

    Thus,
    \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,
    asin x + bcosx = \sqrt{a^2 + b^2} sin(x + tan^{-1}(\frac{b}{a}))

    So if I let a = \triangle x and b = -\triangle y, then I get the following hideous expression:
    \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,
    \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?
    Last edited by Tuufless; November 8th 2009 at 01:21 AM. Reason: Fixed some algebraic errors. :/
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor
    Opalg's Avatar
    Joined
    Aug 2007
    From
    Leeds, UK
    Posts
    4,041
    Thanks
    7
    The identity 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 a\sin x + b\cos x = \sqrt{a^2 + b^2} \sin(x + \tan^{-1}(\tfrac{b}{a}) + \pi). Similarly, if y = \sin x it does not necessarily follow that x = \sin^{-1}y. In your case, the condition for this to hold is \cos x > 0. If \cos x<0 then again you have to add \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 \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 \Delta x<0 and 0 otherwise. Then define \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 \Delta x=0 or \cos(\theta-\psi)=0).
    Last edited by Opalg; November 7th 2009 at 01:43 PM. Reason: corrected error
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Nov 2009
    Posts
    51
    Thanks
    13
    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 \phi in similar fashion?

    If \phi were to fall in say, the second quadrant, then \sin\phi > 0 and -\cos\phi > 0.
    Still, it's not immediately obvious to me given that \frac{v_T}{v_P} < 1 by default (well, barring one heckuva fast tank), that the entire expression \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 \sin^{-1}\frac{v_T(\triangle x\sin\phi - \triangle y \cos\phi)}{v_P\sqrt{(\triangle x)^2 + (\triangle y)^2}} when \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 \sqrt{(\triangle x)^2 + (\triangle y)^2} both here and in the original post. Now, at least it's a lot more plausible that \frac{v_T(\triangle x\sin\phi - \triangle y \cos\phi)}{v_P\sqrt{(\triangle x)^2 + (\triangle y)^2}} < 1 in all quadrants.
    Last edited by Tuufless; November 8th 2009 at 01:33 AM. Reason: Fixed more algebraic errors. :(
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 1
    Last Post: September 16th 2011, 02:08 AM
  2. Replies: 2
    Last Post: June 4th 2011, 01:11 PM
  3. Replies: 2
    Last Post: April 24th 2011, 08:01 AM
  4. "free" variables, linear programming
    Posted in the Advanced Algebra Forum
    Replies: 1
    Last Post: December 3rd 2010, 08:45 PM
  5. Replies: 1
    Last Post: October 25th 2010, 05:45 AM

Search Tags


/mathhelpforum @mathhelpforum