Find angle between Quaternion A and B

Started by
1 comment, last by ProjectinMatrix 12 years, 6 months ago
After much google-ing i found a few ways to find the angle between two quaternions that yield the same result. I picked the one that makes the most sense to me and implemented it.


float Quaternion::Angle(Quaternion& a, Quaternion& b) {
Quaternion inv = Quaternion::Inverse(a);
Quaternion res = b * inv;
return acosf(res.w) * 2.0f;// * 57.2957795f; // To degrees!;
}



Now, i run my test code


Quaternion q1 = Quaternion::Euler(10.0f, 5.0f, 90.0f);
Quaternion q2 = Quaternion::Euler(7.0f, -80.0f, 360.0f);
cout << Quaternion::Angle(q1, q2) << '\n';


And the result is 4.40809 radians
Not bad, i multiply it by 57.2957795f to convert it to degrees and the angle is 252.565 degrees

I compare this to Unity's implementation of the .Angle function


Quaternion q1 = Quaternion.Euler(10.0f, 5.0f, 90.0f);
Quaternion q2 = Quaternion.Euler(7.0f, -80.0f, 360.0f);
Debug.Log(Quaternion.Angle(q1, q2));


And the result is 107.4352 degrees
Odd... Multiply that by Unity's Mathf.Deg2Rad constant and the result is 1.875097 radians

So my question would be something along the lines of: "What on earth am i doing wrong?"
Advertisement
You're forgetting that the quaternion angle is either N degrees rotation around the axis or -N degrees rotation around the opposite axis. If you get an angle above 180 degrees what you probably wanted is 360 degrees minus the answer you computed. Notice how your answer plus Unity's answer add up to 360 degrees.
*facepalm*

This topic is closed to new replies.

Advertisement