LookAt Function

Just starting out? Need help? Post your questions and find answers here.
minime
New User
New User
Posts: 4
Joined: Tue Jun 10, 2003 10:44 pm

LookAt Function

Post 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 :)
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post 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.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
minime
New User
New User
Posts: 4
Joined: Tue Jun 10, 2003 10:44 pm

Post 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.
Post Reply