# Orienting mesh to vector

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

## Recommended Posts

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

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
Try normalizing right, and then crossing right and up to get the correct forward vector to plug into the matrix.

##### Share on other sites
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?

##### Share on other sites
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...

##### Share on other sites
Makes perfect sense, cheers.

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 19
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633362
• Total Posts
3011531
• ### Who's Online (See full list)

There are no registered users currently online

×