Finding quaternion representing the rotation from one vector to another

sdfqwerqaz1 picture sdfqwerqaz1 · Jul 23, 2009 · Viewed 83.5k times · Source

I have two vectors u and v. Is there a way of finding a quaternion representing the rotation from u to v?

Answer

Polaris878 picture Polaris878 · Jul 23, 2009
Quaternion q;
vector a = crossproduct(v1, v2);
q.xyz = a;
q.w = sqrt((v1.Length ^ 2) * (v2.Length ^ 2)) + dotproduct(v1, v2);

Don't forget to normalize q.

Richard is right about there not being a unique rotation, but the above should give the "shortest arc," which is probably what you need.