Jump to content
Sign in to follow this  
Fortran

Direction to marker/waypoint.

Recommended Posts

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.

Edited by Fortran

Share this post


Link to post
Share on other sites

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.

Edited by nuxil

Share this post


Link to post
Share on other sites

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 {
_thadir = _thadir + 360;
};

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 ?

Edited by Fortran

Share this post


Link to post
Share on other sites

can you explain why you need 0 - 360 ?

for example setdir -90 is the same as setdir 270;

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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 :)

@ 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 ?

Edited by Fortran

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×