Shortest Rotation Between Two Quaternions

I am needing to get the shortest rotation path between two quaternions.

Quaternion a;
Quaternion b;

var c = a * Quaternion.Inverse(b);

c is usually the shortest path, but there are instances where it is the significantly longer rotation.

Is there an easy way to guarantee that c is always the absolute shortest rotation between a and b?

I think the Slerp operation is what you want:

The reason I think this is that article mentions “great circle arc,” which is of course what airliners do to get the shortest distance between distant airports.

can you maybe check if the rotation of c is over 180/pie and if so just flip it?

Hey Kurt good find, I was able to extract what I needed out of this. Here is the fix for anyone else running into this -

{
if (Quaternion.Dot(a, b) < 0)
{
return a * Quaternion.Inverse(Multiply(b, -1));
}
else return a * Quaternion.Inverse(b);
}

public static Quaternion Multiply(Quaternion input, float scalar)
{
return new Quaternion(input.x * scalar, input.y * scalar, input.z * scalar, input.w * scalar);
}```
8 Likes

I think you are imagining that c is a float representing the angle between two vectors. In the example c is a Quaternion representing the rotation to get from one orientation to another.

sorta but not really, just look if that rotation(c) is looking over 180 degrees from “0” rotation…?

I guess quaternion doesn’t work anything like that eh?
lucky there’s all of those functions, haha. :face_with_spiral_eyes:

This is exactly what I was looking for today. Thanks for sharing Guadinman!