# How to rotate an object to terrain normal

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

## Recommended Posts

I have an idea on how to do this but not 100% sure on how to implement it. 1.Get Normal of object to terrain? 2.Get Normal of terrain at point X? 3.Take cross() of those two? 4.Align object's normal you found to terrains normal? I have a lot of books, but can't think of any I have that I ever came across on how to implement this... So if anyone has a specific book that covers this I would appreciate it or if someone has a tutorial site that would be great also. I have looked around and most just talk theory not actual code base to learn from. I am not going to be doing each wheel for my models as of now are modeled as a solid unit... thanks [Edited by - MARS_999 on June 9, 2007 5:45:55 AM]

##### Share on other sites
you can use the followign method to generate a rotation matrix to transform an object alligned with the y-axis to the surface normal:

let the surface normal be the unit vector 'y'

then you have:

x = [y.z, 0, -y.x]/sqrt(y.x*y.x+y.z*y.z);
z = x cross y

thatll give a rotation matrix:

[x.x y.x z.x 0]
[x.y y.y z.y 0]
[x.z y.z z.z 0]
[ 0 0 0 1 ]

which you can add to the modelview transformation.

there is one degenerate case, when y.x and y.z are both 0, in which case you can use the matrix:

[ 1 0 0 0 ]
[ 0 y.y 0 0]
[ 0 0 0 y.y 0]
[ 0 0 0 1 ]

in otherwords, for a terrain this will be when its completely flat, and the rotation matrix will be the identity matrix since the terrain wont be upside down

##### Share on other sites
Please clarify that the surface normal you speak of is the terrain surface normal? and not the models(car) surface normal.

##### Share on other sites
the terrain surface normal:

##### Share on other sites
Ok here is what I have... I think I am close, but need someone who has an idea on this to take a look. Thanks

void RotatePlayerToTerrainNormal(Player *player, vec3 &normal){	Matrix4x4 matrixMV;	glGetFloatv(GL_MODELVIEW_MATRIX, matrixMV.matrix);	if(normal.x == 0.0f && normal.z == 0.0f)	{		Matrix4x4 tempMatrix;		tempMatrix.Identity();		tempMatrix *= matrixMV;		player->model->rot.x = tempMatrix.matrix[12];		player->model->rot.y = tempMatrix.matrix[13];		player->model->rot.z = tempMatrix.matrix[14];	}	else	{		vec3 x = vec3(normal.z, 0.0f, -normal.x) / sqrt(normal.x * normal.x + normal.z * normal.z);		vec3 z = Cross(x, normal);			Matrix4x4 tempMatrix(x.x, normal.x, z.x, 0.0f, 				     x.y, normal.y, z.y, 0.0f,				     x.z, normal.z, z.z, 0.0f,				     0.0f, 0.0f, 0.0f, 1.0f);		tempMatrix *= matrixMV;		player->model->rot.x = tempMatrix.matrix[12];		player->model->rot.y = tempMatrix.matrix[13];		player->model->rot.z = tempMatrix.matrix[14];	}}

***BUMP anyone?

##### Share on other sites
Well can anyone confirm this then? Can I use my TBN matrix I have setup already to use that to rotate the objects?????

##### Share on other sites
Have you tried implementing it? Does it work?

##### Share on other sites
Quote:
 Original post by RasmadrakHave you tried implementing it? Does it work?

Which one? My TBN idea? If so yes and I can't get the math right, for how to setup the matrices.

If you are referring to luca-deltodesco idea, I posted my code and that isn't working either.

Thanks for the reply. I am about bald from pulling my hair out... :(

##### Share on other sites
Here is what I have now and still isn't working...

void RotatePlayerToTerrainNormal(Player *player, Camera &camera){	Matrix4x4 rotationMatrix;	vec3 terrainNormal = gterrain->GetTerrainNormal(camera.View.x, camera.View.z);	vec3 player1Normal = vec3(player->model->Objects->Normals[3],							  player->model->Objects->Normals[4],							  player->model->Objects->Normals[5]);	float angle = Dot(terrainNormal, player1Normal);	rotationMatrix.Rotate(angle, true, true, true);//rotate on all three axes	vec4 tempRotation = rotationMatrix.VectorMatrixMultiply3x3(player->model->rot);	player->model->rot.x = tempRotation.x;	player->model->rot.y = tempRotation.y;	player->model->rot.z = tempRotation.z;}

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002295
×