Find angle between Quaternion A and B

This topic is 2912 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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?"

Share on other sites
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*

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 19
• 36
• 9
• 16
• 75