direction

Blitz3D Forums/Blitz3D Beginners Area/direction

mindstorms(Posted 2006) [#1]
I hope that this is a simple algorithm, but I have to angles along a degree system with 0 up, 90 right, -90 left, and 180 down (for drawing a circle), and two angles from zero. I have one angle that I want to go to the other angle, but I need to know if I go counterclockwise or clockwise to get from it...like I have a 90 degree angle and a -170 degree angle and I want to the program to figure out which way to go (like negative or postitive).


b32(Posted 2006) [#2]
If you want to go from 90 to -170, that would be -260 ?
In that case, use something like Sgn(-ang1 + ang2).
Do you want to calculate the shortest distance ? So that, from 90 to -170 would go from 90 to 190 ? (+100)
This is a routine I wrote for that:

Not sure if it is good for this purpose, I think the angles need to be in the range 0..360, or the difference between them should be <360 or <720.
Anyway, when you calculated the shortest distance, use Sgn(x) to get the direction (-1, 0, +1)


octothorpe(Posted 2006) [#3]
The algorithm is very simple: it's subtraction. The trickery lies in the fact that your result may be off by n*360 where n is any integer, positive or negative. If you're correcting the range of your angles at every step, bram's code will be fine (since it only deals with results off by +-720,) but I prefer a more robust approach:

; calculate difference
relative_angle = target_angle - initial_angle

; simplify range to [0,360)
relative_angle = relative_angle Mod 360 : If relative_angle < 0 Then relative_angle = relative_angle + 360

; modify simplified range to (-180,180]
If relative_angle > 180 Then relative_angle = relative_angle - 360

If relative_angle < 0 Then turn_counter_clockwise()
If relative_angle > 0 Then turn_clockwise()


Another way to modify the range would be

While relative_angle < -180 : relative_angle = relative_angle + 360 : Wend
While relative_angle >= 180 : relative_angle = relative_angle - 360 : Wend



mindstorms(Posted 2006) [#4]
Thanks you guys, this has really helped alot.