Page 1 of 1

LookAt Function

Posted: Tue Jun 10, 2003 10:53 pm
by minime
Hi all,

I'm trying to have a gun turret follow an enemy(rotation wise), is there something like a 'lookAt' function in Pure that returns an angle, sortof like:

angle = lookat(myship, targetship)
rotatesprite3d(myship,angle,1)

or do we have to do it with math ?

If math it is, can you give me an example ?

I'm using the Sprite3D functions soo i'm just intrested in the x,y aspect of things.

Thanks in advance for the help.

btw: i love the Sprite3D function, definitly a step in the right direction, i'd love to see this expanded further :)

Posted: Tue Jun 10, 2003 11:29 pm
by tinman
There is a command like that in the £D engine (camera?) libraries, but it manipulates the things internally to the library, so you would not be able to use it.

The maths would be something like:

Code: Select all

Procedure.l Sgn(value.l)
    If value=0
        ProcedureReturn value
    Else
        ProcedureReturn value / Abs(value)
    EndIf
EndProcedure

If (myship\y - target\y) = 0
    angle = Sgn(myship\x - target\x) * 90
Else
    angle = ATan((myship\x - target\x) / (myship\y - target\y))
EndIf
I could be wrong, I never tested it and never thought about what would happen from 180 degrees to 360 degrees, but since the values of ATan repeat in those cases, you might need to manually check whether the angle should be 0 to 180 or 180 to 360.

Posted: Wed Jun 11, 2003 3:00 pm
by minime
Thanks Tinman,

it definitly gives me something to start with.
I remember playing with this lookat concept a fiew years back in Delphi and i had the same problem, identifying the 180 - 360 part.

I'll have to dig up my old code and see what i did to make it work :)

Thanks again,

minime.