# Generate rotaton matrix from 2 normal vectors

This topic is 4161 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I am trying to generate a rotation matrix from two normal vectors. I have a point with a normal vector (eg: a plane), and then I have a new normal vector (eg: same plane with a different orientation), and I would like to generate the rotation matrix to get the first point&vector to the second point&vector. Any advice? Thanks.

##### Share on other sites
The 2 vectors span the plane of rotation by itself, and they show an angle inbetween them. Compute the normalized cross product of the vectors to yield in the plane's normal (called "pivot" in the following), and the angle (called "amount" in the following) using the dot-product definition. Then the basis vectors of the rotation maxtrix can be computed like so:
Vector b0,b1,b2;const float sine = ::sin(amount);const float cosine = ::cos(amount);const float oneMinusCosine = 1.0f-cosine;double temp;temp = oneMinusCosine*pivot.x;b0.x = pivot.x*temp+cosine;b0.y = pivot.y*temp+sine*pivot.z;b0.z = pivot.z*temp-sine*pivot.y;temp = oneMinusCosine*pivot.s1;b1.x = pivot.x*temp-sine*pivot.z;b1.y = pivot.y*temp+cosine;b1.z = pivot.z*temp+sine*pivot.x;temp = oneMinusCosine*pivot.z;b2.x = pivot.x*temp+sine*pivot.y;b2.y = pivot.y*temp-sine*pivot.x;b2.z = pivot.z*temp+cosine;

AFAIK the stuff works for both right handed and left handed systems. As said the pivot vector must be normalized, or else the resulting matrix will not represent a pure rotation.

EDIT: Notice please that mathematically a rotation is a transformation of an orientation. I.e. it maps the one direction vector onto the second direction vector, but it doesn't deal with points (hence it doesn't map "the first point&vector to the second point&vector")! Mapping points needs a translation instead. So, if you really have to map a point onto another one, then you additionally have to wrap the above rotation by a translation and its inverse.

##### Share on other sites
Ah, great! Thanks!

Edit: No, I just need a rotation, so thats fine. Cheers.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 11
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633721
• Total Posts
3013535
×