Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Using Trigonometry to get an object to point where I want it

Author
Message
Seppuku Arts
Moderator
21
Years of Service
User Offline
Joined: 18th Aug 2004
Location: Cambridgeshire, England
Posted: 18th Aug 2010 15:37 Edited at: 18th Aug 2010 15:38
I have an AI that I want to turn smoothly, so I'm wanting to use the 'curveangle' command on the angle of the object. The object will point at each way-point, but I don't want to use 'point object' because I can't apply a curve angle to it. So I've been trying to use trigonometry to calculate the angle from where the object is facing to where the waypoint object is located.

I found using ATANFULL(x,y) to work in a test, but it only works out the angle between 'x' and 'y' from zero. I however, want to work out the angle from the position of the object, which is constantly moving. (I'm using 2, because I only need axis' 'x' and 'z' in the 3d world)

Any ideas?

Cheers.

Click!
Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 18th Aug 2010 15:59
I can't remember if ATANFULL() gives you a 0-360 or a -180 to 180....? Ultimately, you want to turn your object in the direction of the way-point slowly (like maybe 1 degree per cycle or so) and you want it to turn the shortest direction, right?

3D right?

OY#=object angle y()
DY#=ATANFULL(px#,pz#)

If it's the 0-360:
PY#=abs(DY#-OY#)
Turn#=1
if PY#>180 then Turn#=-1

If it's the -180 to 180:
PY#=DY#-OY#
Turn#=1
if PY#<0.0 then Turn#=-1


Use the Turn# to turn your object until it reaches your desired direction (or it gets within 1 degree).

I may be wrong on the polarity of Turn#, but you will quickly find that out when you test it.

The fastest code is the code never written.
James H
19
Years of Service
User Offline
Joined: 21st Apr 2007
Location: St Helens
Posted: 18th Aug 2010 16:51 Edited at: 18th Aug 2010 16:51
I think the snippet in second to last post may be of some use to you as it rotates a tree trunk to match a billboard for smoother LOD transition, based on positions and camera angle. Its not simplified though. Theres a much simpler ezrotate version in an earlier post with media attatched. Hope its of some use

http://forum.thegamecreators.com/?m=forum_view&t=121557&b=1
Seppuku Arts
Moderator
21
Years of Service
User Offline
Joined: 18th Aug 2004
Location: Cambridgeshire, England
Posted: 18th Aug 2010 17:17 Edited at: 18th Aug 2010 18:06
Hawkblood, I don't think that quite solves my problem, but thank you. Atanfull is 0-360.

This is the code I'm using:



[Just for testing]

If I press up and down, it'll move the 'waypoint' object, and the AI object will happily watch it. If I move the AI object with 'w and s', it won't continue facing the waypoint.

'ATANFULL' is reading the angle between X and Y from a point, which is 0,0,0

What I want it to do is read the angle between X and Y from any point in space.
[EDIT]

In theory, I've solved the problem...now to put it to practice

In ATANFULL, I subtracted the object position values from the waypoint position values, so in essence, I get this:

angle = ATANFULL( x(2) -x(1), z(2) - z(1))

So now, for the angle to turn smoothly for each way point, I need a curvevalue, which should work.

Amended (working) code:




[EDIT2]


It works in my game too. Huzzah.


Cheers.

Click!

Login to post a reply

Server time is: 2026-07-25 17:50:20
Your offset time is: 2026-07-25 17:50:20