I needed to find the angle between two 2D vectors, and found the following on the net:
I can get the first method to work fine, but as the text suggests, this isn't any use to me - I need the relative angle of v1 to v2 (i.e. a result in the range -180 to +180).
I've tried the second method, using ATan2, but can't seem to get it to work. Part of the reason is probably because the atan2 function in the text works with Radians, whereas blitz ATan2 works with degrees.
I tried converting to radians, doing the calculation, then converting back to degrees, but still no joy. I may have been doing the conversion incorrectly, but frankly, I think this is a bit of a kludge, anyway.
So, is there a neat way to do this using blitz's ATan2 function?
Cheers.
(P.S. This has probably been asked a load of times, but searching and looking through the code archives was fruitless.)
For 2D Vectors
This is relatively simple because there is only one degree of freedom for 2D rotations. If v1 and v2 are normalised so that |v1|=|v2|=1, then,
angle = acos(v1•v2)
The only problem is, this won't give all possible values between 0 and 360 degrees, or -180 and +180 degrees. In other words, it won't tell us if v1 is ahead or behind v2, to go from v1 to v2 is the opposite direction from v2 to v1.
In most math libraries acos will usually return a value between 0 and PI (in radians) which is 0 and 180 degrees.
If we want a + or - value to indicate which vector is ahead, then we probably need to use the atan2 function (as explained on this page). using:
angle of 2 relative to 1= atan2(v2.y,v2.x) - atan2(v1.y,v1.x)
This is relatively simple because there is only one degree of freedom for 2D rotations. If v1 and v2 are normalised so that |v1|=|v2|=1, then,
angle = acos(v1•v2)
The only problem is, this won't give all possible values between 0 and 360 degrees, or -180 and +180 degrees. In other words, it won't tell us if v1 is ahead or behind v2, to go from v1 to v2 is the opposite direction from v2 to v1.
In most math libraries acos will usually return a value between 0 and PI (in radians) which is 0 and 180 degrees.
If we want a + or - value to indicate which vector is ahead, then we probably need to use the atan2 function (as explained on this page). using:
angle of 2 relative to 1= atan2(v2.y,v2.x) - atan2(v1.y,v1.x)
I can get the first method to work fine, but as the text suggests, this isn't any use to me - I need the relative angle of v1 to v2 (i.e. a result in the range -180 to +180).
I've tried the second method, using ATan2, but can't seem to get it to work. Part of the reason is probably because the atan2 function in the text works with Radians, whereas blitz ATan2 works with degrees.
I tried converting to radians, doing the calculation, then converting back to degrees, but still no joy. I may have been doing the conversion incorrectly, but frankly, I think this is a bit of a kludge, anyway.
So, is there a neat way to do this using blitz's ATan2 function?
Cheers.
(P.S. This has probably been asked a load of times, but searching and looking through the code archives was fruitless.)