Hello,
I hope I can explain this....
I have a script to make a object turn (via setdir) in a random direction between a maximum left & right arc.
Suppose my object's direction is 313 and the max step is 80 degrees, than the object can turn between azimuth 33 & 233...
Because :
1)
313 - 80 = 233
2)
313 + 80 = 393, but I have to correct this so the result stay between 360 degrees. So 393 -360 = 33
So far so good, that works fine.
The problem is when the old heading is below 360 degrees and the new heading is above 360 degrees, the object turns counterclockwise, but the shortest way is clockwise.
How can I calculate this so the object always takes the shortest turn to get there?
_mg = _this select 0
_mainhdg = getdir _mg
_step = 80
;calculate the random heading the first time
_newhdg = (_mainhdg + ((random _step) - (random _step)))
? _newhdg < 0: _newhdg = _newhdg + 360
? _newhdg > 360: _newhdg = _newhdg - 360
#searchloop
? (getdir _mg - _newhdg) < 10 and (getdir _mg - _newhdg) > - 10: goto "pauze"
? (getdir _mg - _newhdg) > 0: _mg setdir ((getdir _mg) - 1)
? (getdir _mg - _newhdg) < 0: _mg setdir ((getdir _mg) + 1)
player globalchat format ["MH=%1 NH =%2 DIR= %3",_mainhdg,_newhdg,getdir _mg]
~0.09
goto "searchloop"
#pauze
_newhdg = (_mainhdg + ((random _step) - (random _step)))
?_newhdg > 360 : _newhdg = _newhdg - 360
?_newhdg < 0 : _newhdg = _newhdg + 360
~2 + random 2
goto "searchloop"
Actually, I think this is a shitty code, I'm sure this can be done easier, but I'm bad with math... :-\
Please help me out.