My aiming math is wrong :S

BlitzMax Forums/BlitzMax Beginners Area/My aiming math is wrong :S

Drakim(Posted 2008) [#1]
(I think think you need more of my code than this to see the problem, but if you do, I'll post it)

My missile object has this method:
Method Fire(X:Float,Y:Float,Speed:Float)
  Local Angle:Float = DegreesBetweenPoints(XPos, YPos, X, Y)
  XSpeed = Sin(Angle) * Speed
  YSpeed = Cos(Angle) * Speed
End Method


Here is the degrees function:
Function DegreesBetweenPoints:Float(X1:Float,Y1:Float,X2:Float,Y2:Float)
  Return 180.0 - ATan2(X2 - X1,-(Y2 - Y1))
End Function


I use this to make the missile be shot against the mousepointer. And it appears to work. However, upon closer inspection I noticed that the aim is wrong. When shooting in any straight direction, such as North or West, the aim is perfect, by in between them the aim becomes wrongs. Is my aiming math in error?


Jesse(Posted 2008) [#2]
an obvious error is that
atan2 should be in the form of atan2(y,x) not atan2(x,y).


Drakim(Posted 2008) [#3]
Ah, I see.

.....

However, changing it did not fix the problem. :S

changing the x,y to y,x made the aim completely off, but what's really strange is that the same "wrong aiming" was still there. It almost looks like a rounding error of some kind, but I'm not sure.


Jesse(Posted 2008) [#4]
if you post the actual code, it will be easier for some one to help you. I haven't really looked and I don't have time right now sence I am on my way out. I might be able to look at it when I comeback if you haven't figured it out.


Drakim(Posted 2008) [#5]
Let's see....I can't give all the code. It's too many pages.

The main problem is that I extend most types to another, so I can't just copy the missile type, because it doesn't remotely work alone. And I highly doubt anybody would bother helping if that required them to read though more than 10 pages of code. >>

But, I've made an attempt at giving only the relevant pieces:

When the player presses mouse button 1, we create a missile and run it's Fire() method. The Fire() method needs the X and Y of where the missile is headed, and a speed value (which I use 2000 here).
If MouseDown(1) then
  Local Missile1:TMissile = TMissile.Create()
  Missile1.Fire(MouseX(),MouseY(),2000)
End If


TMissile's Fire method, which is activated right after it's created. (I didn't bother including the creation function, it's boring and irrelevant). Here, X and Y is the XMouse and YMouse we passed earlier. XPos and YPos is the position of the TMissile itself. We get the angle of these points, and use it to calculate how much XSpeed and YSpeed the TMissile should have in order to travel in the direction of the current position of the mouse. (and multiply the values we get with the Speed argument obviously)
  Method Fire(X:Float,Y:Float,Speed:Float)
    Local Angle:Float = DegreesBetweenPoints(XPos, YPos, X, Y)
    XSpeed :+ Sin(Angle) * Speed
    YSpeed :+ Cos(Angle) * Speed
  End Method



Degree finding function. This was the one you pointed out was wrong. Changing it didn't help much. It was a while since I wrote this function, and I was using the help of some other text, so I don't know much what it's supposed to be. >>
Function DegreesBetweenPoints:Float(X1:Float,Y1:Float,X2:Float,Y2:Float)
  Return 180.0 - ATan2(Y2 - Y1,-(X2 - X1))
End Function



Jesse(Posted 2008) [#6]
my bad, I was reading how the atan2 was assigned. But you had it sort of correct the first time with the x and y. but not all right try this:
Function DegreesBetweenPoints:Float(X1:Float,Y1:Float,X2:Float,Y2:Float)
  Return ATan2( (X2 - X1) , (Y2 - Y1) ) 
End Function



Drakim(Posted 2008) [#7]
....

I found the problem. It had to do with a little max speed safeguard I had put at 400, thus making 2000 speed not work properly. DX

Sorry for asking you to fix something that wasn't broken. S:
(although your math is easier on the eye, so I'll use it)


Drakim(Posted 2008) [#8]
Actually, I got all sorts of problems using your math function. My original one works perfectly once the max speed is removed.


Jesse(Posted 2008) [#9]
I'm sorry if I messed you up. I was basing the information on code I downloaded sometime ago from here:
http://www.blitzmax.com/Community/posts.php?topic=64402#718961
which works great for me