GLBasic forum

Feature request => IDE/Syntax => Topic started by: MrTAToad on 2009-Jan-22

Title: ATAN2 is needed
Post by: MrTAToad on 2009-Jan-22
GLBasic at the moment doesn't recognise the command...
Title: Re: ATAN2 is needed
Post by: Moru on 2009-Jan-22
ATAN = ATAN2
Title: Re: ATAN2 is needed
Post by: Schranz0r on 2009-Jan-22
That's right!  :good:
Title: Re: ATAN2 is needed
Post by: MrTAToad on 2009-Jan-22
Should have noticed that :)

The angle between points is correct now (up is 270 degrees) :

Code (glbasic) Select
FUNCTION angleBetweenPoints:ax,ay,ex,ey
LOCAL angle

angle=ATAN(ey-ay,ex-ax)
IF angle<0.0
angle=180.0+(180.0+angle)
ENDIF

RETURN angle
ENDFUNCTION
Title: Re: ATAN2 is needed
Post by: WPShadow on 2009-Jan-29
is it the same with x & z?

Code (glbasic) Select

angle=ATAN(ez-az,ex-ax)


To use the 2 angles together for (x, y, z) <--> (ax, ay, az)
Title: Re: ATAN2 is needed
Post by: Kitty Hello on 2009-Jan-29
Yes, indeed.
Title: Re: ATAN2 is needed
Post by: Schranz0r on 2009-Jan-29
thats simple :D

Code (glbasic) Select
DRAWRECT 0,0,20,20, RGB(0xff, 0x00, 0x00)
DRAWRECT 0,10,10,1, RGB(0x00, 0x00, 0x00)
GRABSPRITE 1, 0,0,20,20
BLACKSCREEN


TYPE TPlayer
x%;y%
angle#
lookat_handle% = -1
ENDTYPE

GLOBAL TPlayers[] AS TPlayer

LOCAL Player1 = CreatePlayer(20,20)
LOCAL Player2 = CreatePlayer(100,100)

PlayerLookAt(Player1,Player2)

WHILE TRUE


DrawAll()
SHOWSCREEN
WEND
END



FUNCTION CreatePlayer%: x, y
LOCAL t AS TPlayer

t.x = x
t.y = y

DIMPUSH TPlayers[], t
RETURN LEN(TPlayers[])-1
ENDFUNCTION

FUNCTION PlayerLookAt: player, target
TPlayers[player].lookat_handle = target
ENDFUNCTION


FUNCTION DrawAll:
FOREACH t IN TPlayers[]
IF t.lookat_handle > -1
t.angle = 180-ATAN(TPlayers[t.lookat_handle].y - t.y,  TPlayers[t.lookat_handle].x - t.x)
ENDIF

ROTOSPRITE 1, t.x, t.y, t.angle

NEXT
ENDFUNCTION