views:

687

answers:

2

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

+2  A: 

The problem as stated is not well-defined: there is not a unique rotation for a given pair of vectors. Consider the case, for example, where u = <1, 0, 0> and v = <0, 1, 0>. One rotation from u to v would be a pi / 2 rotation around the z-axis. Another rotation from u to v would be a pi rotation around the vector <1, 1, 0>.

Richard Dunlap
+5  A: 
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.

Polaris878