Skip to main content
gamedev.net
🔒 Locked

Orienting mesh to vector

Started by jameswren Aug 19, 2005 at 12:43 PM 7 replies 1.2k views
Original Post
jameswren
jameswren
Basically I've hit a brick wall in trying to orientate my mesh (rotate it properly) so that it lies flat on the ground. I think what I need is some maths help in creating a rotation matrix from just the normal of the ground, so can anyone help? Cheers
jollyjeffers
jollyjeffers
A similar question was asked a few days back, and it was suggested that using properties of the dot product might be useful.

The dot product between an up vector (0,1,0) and the surface normal should give you the cosine of the angle, which you should be able to feed into a rotation calculation.

Not 100% sure on that one, having never tried it myself [oh] - but it could be worth considering?

hth
Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
jameswren
jameswren
Hmm thanks well I've had a think about that but haven't been able to work out how to use it for what I want...maybe my maths just sucks though
Zakwayda
Zakwayda
jollyjeffer's solution should be what you're looking for if you already have an orientation for your mesh. If you're starting from scratch, there isn't one unique orientation that will align with the ground normal, so you'll need some sort of 'make orthonormal basis' function.

If that still doesn't help, you might describe more specifically what you're trying to do.
jameswren
jameswren
Well I tried an alternative technique I found after learning some more matrix maths, in which I directly put 'axis' vectors into my transformation matrix:

        private Matrix BuildTransformationMatrix(Vector3 vPos, Vector3 vForward, Vector3 vUp)        {            Matrix mat;            Vector3 vRight = Vector3.Cross(vUp, vForward);            mat.M11 = vRight.X;            mat.M12 = vRight.Y;            mat.M13 = vRight.Z;            mat.M21 = vUp.X;            mat.M22 = vUp.Y;            mat.M23 = vUp.Z;            mat.M31 = vForward.X;            mat.M32 = vForward.Y;            mat.M33 = vForward.Z;            mat.M41 = vPos.X;            mat.M42 = vPos.Y;            mat.M43 = vPos.Z;            mat.M14 = mat.M24 = mat.M34 = 0.0f;            mat.M44 = 1.0f;            return mat;        }


However it doesn't work properly. Sometimes the mesh seems to be orientated correctly, and sometimes it looks at though the mesh is 'slanted' or otherwise distorted. Can anyone see a problem with my code?
Zakwayda
Zakwayda
Try normalizing right, and then crossing right and up to get the correct forward vector to plug into the matrix.
jameswren
jameswren
Works great, thanks alot.

I can see why I should normalize vRight, but why the need to calculate a new vForward? vRight is calculated from vForward (and vUp) in the first place?
Zakwayda
Zakwayda
Quote:
I can see why I should normalize vRight, but why the need to calculate a new vForward? vRight is calculated from vForward (and vUp) in the first place?
Unless up and forward were perpendicular to begin with, the resulting basis won't be orthonormal if you only calculate vRight. Remember that in an orthonormal basis all three vectors are unit-length and perpendicular to one another. So the way you were doing it initially, the final basis was only orthonormal if the up and forward vectors you passed in were perpendicular to begin with.

Let me know if that doesn't make sense...
jameswren
jameswren
Makes perfect sense, cheers.

Topic Locked

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

Sign in to reply to this topic.