Restored from previous forum. Originally posted by GCEclipse.
Hi, I thought I'd got this figured out but it just doesn't work. Its part of a function to calculate the angle in degrees from one point in space to another. Really to work out which direction a sprite would have to move in to hit another object. Anyway it just doesn't return the appropriate figure - can anyone tell me what I'm doing wrong?
;Calculate oppositeOpp=y1-y2
;If negative convert to positive
If opp0 And opp>0
tanang=opp/adj
Else
If adj=0
tanang=opp
Else
tanang=adj
EndIf
EndIf
;Convert to radians
radang=ATan(tanang)
;Convert to degrees
Ang=180/(radang*3.14)
Restored from previous forum. Originally posted by Pupil.
You have to determine in wich quadrant the angle is, ATan can only give values between 0-90 degrees(or 0 - pi/2 radians). So you have to add 0, 90, 180, 270 degrees to the returned angel depending on quadrant.
One note: if adj in you code is 0 the angle is 90 degrees, because ATan(infinity)=pi/2
Hope this will help some.
If i vere you i wouldn't go for the angle stuff when dealing with this sort of thing, just calculate how many pixels in x-direction to go and how many pixels in y-direction to go, this gives you a nice vector pointing at your target. You just have to scale this vector to get the proper speed for the object to travel with.
Restored from previous forum. Originally posted by fweil.
Hello GCEclipse ... welcome to the PureBasic forum.
I suppose you forgot to declare angle stuff as floats.
I propose you the update of your code here. But as I do not understand well what you would like to do, check if it helps and if not come back here with some more details about your needs.
x1 = 10
y1 = 10
x2 = 20
y2 = 20
;Calculate absolute value of opposite
opp=Abs(y1-y2)
;calculate absolute value of adjacent
adj=Abs(x1-x2)
;Find the tangent of the angle
If adj>0 And opp>0
tanang=opp/adj
Else
If adj=0
tanang=opp
Else
tanang=adj
EndIf
EndIf
;Convert to radians
radang.f=ATan(tanang)
;Convert to degrees
Ang.f=180/(radang*3.14159265)
CallDebugger