Angle from point A to point B
BlitzMax Forums/BlitzMax Beginners Area/Angle from point A to point B
| ||
Seems strange that whenever I look this topic up on the net, it's often met with some rather sarcastic comments that there is no angle from point A to point B, and then some mathematical jargon :p Anyway, let's say I have my player at 100,100 and a monster at 200,200. How do I calculate the angle from the player to the monster (I know how to move along that angle once I have it). I suspect it's something like ATAN2(playerY-monsterY,playerX-MonsterX) but that gives some strange results. It might be correct but they just need adjusting afterwards to 0-359 degrees somehow? |
| ||
Function AngleTO:Float(X1:Float, Y1:Float, X2:Float, Y2:Float) Local dx# = x2 - x1 Local dy# = y2 - y1 Return (ATan2(dy, dx) End Function If your getting values outside of 0-360 then use: Function WrapAngle:Float(angle:Float) If angle > 0 Then While (angle >= 360 ) angle :- 360 Wend ElseIf angle < 0 Then While(angle < 0) angle :+ 360 Wend End If Return angle End Function |
| ||
You should just be able to use the return value from a=ATAN2 in a deltax=Sin(a)*speed, deltay=Cos(a)*speed scenario |
| ||
if you are planning to shoot from the source to the target do this: angle = atan2(destination_y - source_y, destination_x - source_x) directionx = cos(angle) directiony = sin(angle) to move it in the direction desired: x = x + directionx * speed y = y + directiony * speed the negative result in the angle don't matter unless you are planning to do other things with the result. if you are just trying to use the returning angle to shoot, this should suffice. this is the same thing that everybody is saying, I just hope this helps understand it better. |
| ||
Yep, those calculations work fine, thank you :) |
| ||
A bit of a problem with this is that the angles don't work as compass angles, i.e 90 to the right, 180 is down, 270 is left, 0 is up. If I add 90 to the Atan line, it converts to a compass angle, but then the sin/cos lines don't work correctly. For example, try the code below.....Graphics 800,600 Angle:Float=0 While Not KeyDown(Key_Escape) Cls Angle=AngleTo(GraphicsWidth()/2,GraphicsHeight()/2,MouseX(),MouseY()) DrawText "Angle to mouse pointer from centre of screen : "+Angle,0,0 DrawLine (GraphicsWidth()/2,GraphicsHeight()/2,GraphicsWidth()/2+(Cos(angle)*100),GraphicsHeight()/2+(Sin(angle)*100)) Flip Wend Function AngleTO:Float(X1:Float,Y1:Float,X2:Float,Y2:Float) Local dx:Float=x2-x1 Local dy:Float=y2-y1 Local Angle:Float=(ATan2(dy,dx))+90 If Angle>0 Then While(Angle>= 360) Angle:-360 Wend ElseIf Angle<0 Then While(Angle<0) Angle:+360 Wend End If Return Angle End Function You'll see the angle to the mouse pointer is a compass angle but the line doesn't point towards it. I've tried every combination of +sin/-cos I can think of but none of them point to the mouse pointer. What's the solution to that? |
| ||
You have to do x = sin; y = -cos as the y-axis is opposite to the standard one. |
| ||
Oh, switch Sin and Cos around on X and Y? Thanks, didn't think of that :) |
| ||
ups! |