# Rotation difference between two quaternions?

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

## Recommended Posts

I am trying to implement a delta-compression technique for key-framed skeletal animation exporter. The way it will work is: if the rotation difference between two key-frames of a joint is small enough, it drops the key-frame. the problem: given two quaternions how do I derive a scalar value that represents the difference between the two rotations? the quaternions can represent two completely arbitrary rotations. the animation system will use SLERP One way I could do this is by converting the quaternion to a matrix but I would like to find a shorter and more elegant way if possible.

##### Share on other sites
Given quaternions q1 and q2 representing two orientations, the quaternion q' = q-1q2 is the rotation that will rotate from the q1 orientation to the q2 orientation. Once you have q', you can simply convert q' to an axis-angle representation and look at its angle.

##### Share on other sites
Quote:
 Original post by SneftelGiven quaternions q1 and q2 representing two orientations, the quaternion q' = q-1q2 is the rotation that will rotate from the q1 orientation to the q2 orientation. Once you have q', you can simply convert q' to an axis-angle representation and look at its angle.

Thanks that helps. A couple of questions..

by q' = q-1q2
you mean q' = q1-1q2 ?

and
if the axis of q1 is different from the axis of q2 (in addition to the rotation), what would the axis of q' represent?

##### Share on other sites
Quote:
 Original post by Hexerby q' = q-1q2 you mean q' = q1-1q2 ?

Er, yes. Sorry.

Quote:
 if the axis of q1 is different from the axis of q2 (in addition to the rotation), what would the axis of q' represent?

"Whatever it has to". For (almost) any two orientations, there is exactly one minimal rotation between them. If the first orientation was "looking up" and the second orientation was "looking down", then the rotation between them would be a rotation around the X axis. If the first orientation was "looking up" and the second orientation was "looking to the left", then the axis would be something complicated and hard to visualize which would nevertheless rotate a looking-up object to be looking to the left. You've correctly identified that if q1 and q2 can be represented as rotations around the same axis then q' will also be around that axis, but that's the special case.

##### Share on other sites
A quaternion is just a special type of vector. Usually, you can get the angle between two unit vectors by using the dot product which would give you the cosine of the angle. If we look at the rotations that are represented by the quaternions, then we see that the unit quaternions form a double cover over the rotation space (so q = -q). Therefore, we need to do some extra work to get the correct angle between two rotations represented by quaternions, but the dot product still works. The difference in rotation (angle) between two quaternions can be given by:

angle = 2 * acos(|q1.q2|)

If you don't want to use acos, you can just use the absolute dot product itself (which is close to one for small angles).

##### Share on other sites
To calculate the inverse quaternion

q
q* is the conjugate quaternion

so the inverse q^-1 = q*/(qq*)

##### Share on other sites
Thanks everyone, here is what I ended up doing and it seems to work, although not sure if it's correct. I'll test the other techniques you presented too and see if there is any improvement.

Given two quaternions q1 and q2I convert both to axis/angleaxis1, axis2, angle1, angle2theta1 = angle2 - angle1; // difference between two rotation anglestheta2 = acos(axis1 dot axis2); // angle between two rotation axesif( (theta1 + theta2) < threshold) {    // the difference is too small, drop the keyframe}

##### Share on other sites
No, that's very wrong. Consider q1= a rotation of 1 degree about the X axis, and q2= a rotation of 1 degree about the Y axis. theta1=0, and theta2=90 degrees, so unless your threshhold is over 90 degrees that'll be falsely marked as a keyframe to keep.

got it, thanks!