# Rotation matrix from Normal ?

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

## Recommended Posts

Hi, i'm confused about rotation matrix from normal vector, my car model has a x-zplane direction vector, i want the car model stay on track's plane, so i build a rotation matrix like this : crossproduct(car.Dir, plane.Normal) to get the right-vector then use car.Dir, plane.Normal & right-vector to build a rotation but it just doesnt look correct, i'm not good in matrix maths, what am i missing when i build the rotation matrix ?? pls help.

##### Share on other sites
The infos you gave are rare, so I could only post general hints:

If the car's direction vector is in the x-z plane (i.e. its y component is 0) then the plane's normal must be 0 in x and z but not in y, of course, or else both vectors could not be part of the same rotation matrix.

All 3 vectors in the matrix must be of unit length, or else the model becomes scaled/sheared. The cross product will result in a unit length vector if and only if both input vectors are of unit length.

At last, make sure that your cross product computes a result that matches your co-ordinate system definition. Say, the cardinal x axis cross the cardinal y axis must result in the cardinal z axis (and not the inverse of the z axis).

##### Share on other sites
haegarr thx for reply

sorry i'm not explain my problem well, the result matrix is just what u said is little "sheared", & not perpendicular to the ramp/slope. about the normalized vectors, i'm not sure the calculations, maybe i show my code here :
// i'm sure the vDir & vNormal is normalized vectorD3DVECTOR vDir=car->vDir, vUp=plane->vNormal, vRight;CrossProduct(&vRight,&vDir,&vUp);Normalize(&vRight,&vRight);D3DMATRIX mat;mat._11=vRight.x;mat._12=vRight.y;mat._13=vRight.z;mat._21=vUp.x;mat._22=vUp.y;mat._23=vUp.z;mat._31=vDir.x;mat._32=vDir.y;mat._33=vDir.z;mat._14=mat._24=mat._34=0.0f;mat._44=1.0f;mat._41=car->vPos.x;mat._42=car->vPos.y;mat._43=car->vPos.z;

as u can see, i'm using D3D, CrossProduct() is OK for other vector calculations. i really dont know what step(s) not correct, any ideas ?

thx for help.

[Edited by - nlo on April 6, 2006 8:41:57 AM]

##### Share on other sites
I'm still not sure what is happening, so please let me ask some things.

From the definition of a ground normal I assume also your normal is perpendicular to the ground. But you said that the car's forward vector is given in the x-z plane. From that I assume that all those vectors are like
case 1: { x 0 z }
so that the y component is _always_ 0. Is this correct? Or are the vectors already aligned to the slope of the gound, so that they are generally given as
case 2: { x y z }

In the former case (1) the car's forward vector is not suitable for the rotation matrix since it violates the orthogonality principle! You have to do something like this:
CrossProduct(&vRight,&vDir,&vUp);CrossProduct(&vDir,&vUp,&vRight);Normalize(&vRight,&vRight); // necessary only if vUp & vDir were not of unit lengthNormalize(&vDir,&vDir); // necessary only if vUp & vDir were not of unit lengthNormalize(&vUp,&vUp); // necessary only if vUp is not already of unit length

to produce an orthogonal veriant (notice that vDir is assumed to be a local variable, initialized as copy of the car's direction; I think your code snippet does already so). In the snippet first the (possibly unnormalized) right vector is computed, and from the result the new perpendicular forward vector is computed.

In the latter case (2) the vector's direction should be okay in principle, but it may still be necessary to normalize vDir.

You could drive the full version given above in all cases since in the worst case it does nothing more than wasting runtime. You could optimize the routine still if it works well ;)

If the code above doesn't work, then it could be senseful to push some defined input in, and to look at the resulting matrix. E.g. if the car's direction is fixed to be { 1 0 0 } and the ground's normal vector is { 0 1 0 }, then what is resulting for vRight and what for the matrix?

##### Share on other sites
Quote:
 to produce an orthogonal veriant (notice that vDir is assumed to be a local variable, initialized as copy of the car's direction; I think your code snippet does already so). In the snippet first the (possibly unnormalized) right vector is computed, and from the result the new perpendicular forward vector is computed.

Yes, i'm the case (1), & thx haegarr [cool], u just put me to the right position, i didnt realize i need to re-compute the vDir.

thanks again ! [smile]