Skip to main content
GameDev.net gamedev.net
🔒 Locked

Generate rotaton matrix from 2 normal vectors

Started by e64 Jul 25, 2007 at 1:19 AM 1 replies 950+ views
Original Post
e64
e64
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.
haegarr
haegarr
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.
e64
e64
Ah, great! Thanks!

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.