Math Help Forum

Math Help Forum Feed Site Feed

Go Back   Math Help Forum > Pre-University Math Help > Geometry
Reply
 
Thread Tools Display Modes
  #1  
Old November 6th, 2009, 10:00 PM
Newbie
 
Join Date: Nov 2009
Posts: 6
Thanks: 1
Thanked 4 Times in 3 Posts
Tuufless is on a distinguished road
Default 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 02:21 AM. Reason: Fixed some algebraic errors. :/
Reply With Quote
Advertisement
 
  #2  
Old November 7th, 2009, 10:31 AM
Opalg's Avatar
MHF Contributor

 
Join Date: Aug 2007
Location: Leeds, UK
Posts: 2,063
Country:
Thanks: 126
Thanked 1,257 Times in 1,045 Posts
Opalg has a brilliant futureOpalg has a brilliant futureOpalg has a brilliant futureOpalg has a brilliant futureOpalg has a brilliant futureOpalg has a brilliant futureOpalg has a brilliant futureOpalg has a brilliant futureOpalg has a brilliant futureOpalg has a brilliant futureOpalg has a brilliant future
Default

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 02:43 PM. Reason: corrected error
Reply With Quote
The following users thank Opalg for this useful post:
Donate to MHF
  #3  
Old November 7th, 2009, 10:38 PM
Newbie
 
Join Date: Nov 2009
Posts: 6
Thanks: 1
Thanked 4 Times in 3 Posts
Tuufless is on a distinguished road
Default

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 02:33 AM. Reason: Fixed more algebraic errors. :(
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -7. The time now is 04:39 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
©2005 - 2009 Math Help Forum


Math Help Forum is a community of maths forums with an emphasis on maths help in all levels of mathematics.
Register to post your math questions or just hang out and try some of our math games or visit the arcade.