# Vector to rotation

• Jan 30th 2011, 08:40 AM
WtFudgE
Vector to rotation
Hi,

I have this small problem. I have looked on the internet and I found some answers but they weren't 100% clear.
Let's say I have two 3D vectors:

x: -0.551020951227745 y: 0.291595823345109 z: 0.781887323797847
and
x: -0.881938222372968 y: -0.362656707254797 z: 0.301106434007507

I need to find the rotation matrix which rotates the first vector onto the second one. Can somebody explain to me how I do this exactly? Like with a formula or something? Thx
• Jan 30th 2011, 12:34 PM
WtFudgE
Never mind, problem solved, if anyone else ever needs it, here's the solution in matlab code, im assuming it's readable even if u can't read matlab code, cross = cross product and dot = dot product:

Code:

```    u = normal_vectors(1);     v = normal_vectors(2);     n = cross(u,v);     n = n/norm(n);     phi = acos(dot(u,v) / (norm(u) * norm(v))); %in radials     t = (1-cos(phi));     s = sin(phi);     d = cos(phi);     a = n(1);     b = n(2);     c = n(3);     Rmatrix = [  t*a^2+d t*a*b-s*c t*a*c + s*b;             t*a*b + s*c t*b^2+d t*b*c - a*s;             t*a*c - s*b t*b*c+s*a t*c^2 + d             ];```
So you end up with a rotation matrix which if applied rotates like the first vector to the second