Page MenuHomeFeedback Tracker

BIS_fnc_dirTo does not return a 0 - 360 value
Closed, ResolvedPublic


BIS_fnc_dirTo does not return a 0 - 360 value.

This line in BIS_fnc_dirTo is incorrect

_ret = ((_pos2 select 0) - (_pos1 select 0)) atan2 ((_pos2 select 1) - (_pos1 select 1));
_ret = _ret % 360; //ensure return is 0-360

atan2 returns -180 to 180 , the mod 360 of these values is still -180 to 180 not 0 - 360.


Legacy ID
Steps To Reproduce

[ [1,1,0], [0,0,0] ] call BIS_fnc_dirTo; //-135 degrees

Additional Information

This proves problematic for anything that uses this function including BIS_fnc_relativeDirTo and results in bad output when working out relative directions between objects.
See this forum thread : -

Event Timeline

Larrow edited Steps To Reproduce. (Show Details)Dec 13 2013, 8:38 PM
Larrow edited Additional Information. (Show Details)
Larrow set Category to Scripting.
Larrow set Reproducibility to Always.
Larrow set Severity to None.
Larrow set Resolution to Fixed.
Larrow set Legacy ID to 879340339.May 7 2016, 5:37 PM

Confirmed. It certainly does not do what the author of that script expected it to do if we go by the comments for that function:

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

Line 25 of BIS_fnc_dirTo is:
_ret = _ret % 360; //ensure return is 0-360

Changing the line 25 of BIS_fnc_dirTo to:
_ret = _ret - (360 * (floor (_ret / 360))); //ensure return is 0-360

Would fix the problem and return a value in 0 to 359 range as the author of the script intended.

This function is so old that rectifying it now will lead to many broken scripts.

It's a point of view thing.

My counterargument would be, how many authors are not aware that their scripts are broken because they expected a 0 to 359 output but never checked?

Also, for the purposes of scripts that use setDir, which accepts -90 or 270, it won't make a difference.

Additionally, I think it's a mistake to use it in a script if you are aware that it returns values that were not intended by the author.

If you weren't aware that it returns values other than the ones it says to expect, then it will fix things rather than break it. As is the case with relativeDirTo.

Or you can always leave it be because there are people that use it as is and instead create a duplicate BIS_fnc_dirToReal that would return 0-360

Yes, that would be ok too, as long as the intent of new and old functions is clear unlike the current case of the comments littered in dirTo and relativeDirTo.

Resolved according to CL.

Mass-close of resolved tickets not updated in last two weeks.