PDA

View Full Version : Direction to marker/waypoint.

Fortran
Sep 5 2009, 01:41
Ok I am turning to ask for help after several failed attempts at something which is probably quite simple. I need to obtain the 2D direction (from the player) to a waypoint or marker. Unfortunately my math is poor to say the least so I am really struggling to get a grip on what calculations I need to make. Sorry for the poor illustrations here but the basic premise is...

For example: ( > = player facing direction)
*****************************
^
player_____________Xwaypoint/marker

value returned = 90
*****************************

or

*****************************
player >___________Xwaypoint/marker

value returned = 0
*****************************

or

*****************************
Xwaypoint/marker
|
|
|
|
|
player
\/

value returned = 180
*****************************

etc. etc. Basically I need to get a 2d direction of an object relative to the players facing direction and convert that to a value between 0 and 359. It doesn;'t matter which direction the waypoint/marker is facing (obviously) just it's direction relative to the direction the player is facing. Obviously getting the direction of the player with GetDir is fine and getting the position of the marker or waypoint is fine, its just the calculation inbetween I cannot get my head around.

Any help at all is hugely appreciated.

nuxil
Sep 5 2009, 04:22
may i join your club of poor mathematical skills :p

_p1 = player
_p2 = your object or whatever.

// 1st you need to find the direction from the player to the marker.
_somedir = ((round (getpos p2 select 0))-(round (getpos p1 select 0))) atan2 ((round (getpos p2 select 1)) - (round (getpos p1 select 1)));

// now subtract the playerdir from _somedir.
_thadir = _somedir - getdir p1;

this is not perfect. you prolly want to add in some checks..
maybe someone else can come up with a better solution.

ps you can take away the round stuff i put in there

math isnt my strongest thing.

Fortran
Sep 5 2009, 05:47
Ah mate your a star! That works perfectly nuxil many thanks! Your math is spot on! Just removed your additional rounds and rounded the final value instead, then tagged the following onto the end of your code to prevent it returning negative numbers so it will always be 0 - 360 instead.

if (_thadir < 0) then {
};

Many thanks again mate! Very much appreciate your help, will credit your code in the script.

---------- Post added at 05:47 AM ---------- Previous post was at 05:02 AM ----------

EDIT:Ok it appears my additional code to prevent negative numbers failed lol. Anybody have any hints on how to prevent the direction falling into a negative number ? The output needs to be between 0 and 360 only ?

nuxil
Sep 6 2009, 09:59
can you explain why you need 0 - 360 ?
for example setdir -90 is the same as setdir 270;

Xeno
Sep 6 2009, 10:12
A2 Functions Module...

[player, getpos dude] call BIS_fnc_dirTo

Parameters: [object or position 1, object or position 2]

Returns the compass direction from object/position 1 to
object/position 2. Return is always >=0 <360.

;)

Xeno

Fortran
Sep 6 2009, 16:22
can you explain why you need 0 - 360 ?
for example setdir -90 is the same as setdir 270;

Initially I thought this was going to be a problem, the data is being used to drive a useranimation and when the number slips under 0 into a negative value I thought this would effect it, but after testing it, it doesn't actually seem to matter, the animation behaves as expecting when being fed the output of your code :)

@<hidden> Xeno. Ah great, ok many thanks I will give that a go as well and see how it works out. Thanks alot, didn't realize there was a function for this already! I take it I need to spawn a module first before it will work ?