Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post

Link to post
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 this post

Link to post
Share on other sites
Ah, great! Thanks!

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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!