Page 1 of 1

Posted: Fri Nov 15, 2002 10:16 am
by BackupUser
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?

Code: Select all

;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)
Thanx

GC

Posted: Fri Nov 15, 2002 10:47 am
by BackupUser
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.

Posted: Fri Nov 15, 2002 10:55 am
by BackupUser
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.

Rgrds

Code: Select all

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

Francois Weil
14, rue Douer
F64100 Bayonne

Posted: Fri Nov 15, 2002 5:27 pm
by BackupUser
Restored from previous forum. Originally posted by GCEclipse.

Thanks very much guys - I will try the revised code out as soon as I am back at work.



GC

Posted: Tue Nov 19, 2002 10:40 am
by BackupUser
Restored from previous forum. Originally posted by GCEclipse.

Thanx Guys - I've now been able to sort this out with help from the pointers you've both given me.

Thanks Again

GC

GC

Posted: Tue Nov 19, 2002 12:02 pm
by BackupUser
Restored from previous forum. Originally posted by fweil.

You are welcome GCEclipse.

Let us know about your nice programs or trick'n tips ...

Francois Weil
14, rue Douer
F64100 Bayonne