# matrix to rotate a vector to a known arbitrary axis

Let's say I have a vector V (v.x,v.y,v.z) which is normalized and points somewhere in 3d space. And that I have another vector Z of (0,0,1). Knowing only these two things I'm looking for a transformation matrix that will take the vector Z and turn it in to V.

Or some other way to get to the vector V from the vector Z.

In my application for this I'll be taking other vectors that are in a Z axis space and putting them in (or rotating them to) the same space as the V vector. But a proof that it's working will be to take Z and make it V.

I know that if I had this matrix I could multiply the vec3 by the matrix and get the answer. But making this matrix from only that destination vector is what I'm not sure how to do.

Seems like it might be super simple and that I'm just not thinking of it correctly. Answer right in front of my face?

• Do you need rotation in one step, or interpolation ? Feb 17, 2014 at 9:13
• I need to map vectors from one axis to a new axis. Z+ is the reference for the source axis. V is the reference for the new axis. It's very similar to just a basic matrix transformation that I do all the time to put my 3d objects where and rotated how I want them. I have matrix functions to rotate on the x y and z axis. So maybe I just need to take each axis component and find the angle between z and v and create a matrix from that and multiply them together. Feb 17, 2014 at 9:20
• 1st Cross product the two vectors, you get the rotation axis. 2nd dot product using A.B = |A||B|cos(theta) you can get the rotation angle. Now we have an axis angle representation, we can convert this into rotation matrix. en.wikipedia.org/wiki/… which I think give the same effect as Rodrigues formula. Feb 17, 2014 at 9:45
• I wish I understood math terms better. I understand code. :) I don't know what A.B = |A||B|cos(theta) means. I know what a cross product and a dot product is. Also what is A B and theta from my V and Z? Feb 17, 2014 at 10:17
• A is V , B is Z... and A.B = |A||B|cos(theta) is actually the geometric interpretation of a dot product A.B being dot product. And |A|=length of A Feb 17, 2014 at 10:38

1. Cross product the two vectors N = V x Z where N is the rotation axis.
2. Calculate the angle to get an axis angle representation.

dot( A, B) = len(A)*len(B)*cos(theta)

if A and B where normalized vectors then

dot( A, B) = 1 * cos(theta)

So we can get the angle using cos-1

theta = cos-1( dot(A,B) )

3.Now we have an axis angle representation of orientation, to apply rotation we can do one of the following:

• Convert the axis angle representation to matrix and multiply it be the vectors.

or

Since vector V had been normalized you know the length of vector V and Z will be the same; V has length 1 since it had been normalized, and Z has length of 1 since it is( 0, 0, 1).

Good job :D now the only thing left to take care of in order to make Z vector V is it's orientation.

You will want to get rotation matrix of V then combine it with reverse-rotation matrix of Z. Then apply the resulting matrix to Z.

• By reverse rotation matrix I mean, a matrix that cancels applied rotation. If vector Z rotated 30 degree along Y axis, you want matrix that will rotate Z vector -30 degree along Y axis.

Rotation matrix required for Z to be V = rotation matrix of V * reverse-rotation matrix of Z

• V = RotationMatrixOf_V * ReverseRotationMatrixOf_Z * Z;
• that sounds good. How do I get the rotation matrix of v and the reverse rotation matrix of z? Are these 3x3 or 4x4 matrices? Feb 17, 2014 at 8:11
• Direction vector to rotation matrix, The theory on how to do it is explained on the bottom of this wikipedia link take a look at inverse matrix here as well Feb 17, 2014 at 8:29
• Feb 17, 2014 at 8:30
• Thinking this out.. why do I need the reverse rotation matrix of Z? If my particular input vector is off of Z by 30 degrees I would also want it to be off of V by 30 degrees. That's why I'm looking for a matrix to turn Z into V. It sort of seems like that matrix in that stack overflow answer might be all I would need. ??? Feb 17, 2014 at 8:43
• you are looking for matrix to turn Z into V because you want it to be off of V by 30 degrees? I don't understand your words. If the link is sufficient, then it appears you were looking for a way to get rotation matrix of vector A, not "rotation matrix to turn this vector A to B" Feb 17, 2014 at 8:57