Shortest difference between two quaternions

BlitzMax Forums/BlitzMax Programming/Shortest difference between two quaternions

JoshK(Posted 2009) [#1]
The difference between two quaternions can be found like this:

diff=(q0.inverse()).times(q1)

There are two possible solutions, because you can rotate one way or another to make q1 match q0. Is there a way to find the shortest difference between two quaternions?


Floyd(Posted 2009) [#2]
The w component is Cosine of half the angle. This will be negative if and only if the angle is greater than 180. In that case you want to replace the angle with angle-360. That has the effect of negating Cos(angle/2), i.e. w becomes -w.

You want the positive w. So compute the difference quaternion any way you like, then replace w with Abs(w).


Floyd(Posted 2009) [#3]
Well that was superficial. It just dawned on me that the axis must also be flipped.

For example, if w = -0.5 then the angle is 2*ACos(-0.5) = 240 degrees. In that case we want 120, but going "the other way around".

Changing w to +0.5 makes the angle 120 as desired, but we must also make the axis point the other way, thus reversing the direction of the turn.

So the technique is to compute qDiff. If w<0 then replace qDiff with -qDiff. This negates all of w,x,y,z.

That's what I get for doing this "on paper" and not actually running any code.


JoshK(Posted 2009) [#4]
From what I gather, you are saying find the difference using the method I first said, and then do this:

if q.w<0.0
q.x:*-1.0
q.y:*-1.0
q.z:*-1.0
endif

Because this:

q.x
q.y
q,z
q.w

is the same angle as this:

-q.x
-q.y
-q.z
-q.w


JoshK(Posted 2009) [#5]
It appears to work as I posted above. Thanks!:
http://www.leadwerks.com/post/omega4.wmv