Finding quaternion representing the rotation from one vector to another

mathquaternionsvector

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

Best Answer

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.