# Terrain to Vehicle normal

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

## Recommended Posts

Hi all, I have a terrain and lets say a box. User controls box with arrow keys. I want box height and orientation follows terrain heights. I implemented and I succeded. But I have a small problem. I am using following code to make orientation
	Quaternion q;
Vector3 nodeNormal, rotVector;
FLOAT rotAngle;

nodeNormal = node.GetWorldOrientation().GetYAxis();
rotVector = nodeNormal.Cross( normal );
rotVector.Normalize();

rotAngle = Math_ACos( nodeNormal.Dot( normal ) );
q.RotateAroundAxis( rotVector, rotAngle );

node.SetRotate( q );

It does orientation correctly. But the problem is when it rotates object, it changes z axis of the object. Assume that box is facing (z axis) to (0.0f, 0.0f, 1.0f) when terrain height changes above code is applied and resulting Z axis becomes (0.3f, -0.1, 0.99f). Box should not rotate around Y axis. Is there any suggestion?

##### Share on other sites
what is "normal" equal to in your code sample listed above?

##### Share on other sites
normal is the normal vector of the triangle which box on it. it is calculated like this. let be p1, p2 and p3 the points of the triangle.
pa = p2-p1
pb = p3-p1
pa = pa.Cross( pb )
normal = pa

nodeNormal is the normal (Y Axis) of the box.

##### Share on other sites
another way to do this is through re-normalising the orientation matrix.

Vector right, up, forward; // the three orientation vectors of your vehicle, aka [x, y, z].
Vector normal; // the terrain normal.

up = normal;
forward = right.cross(up);
forward.normalise();
right = up.cross(forward);

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

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

• Total Topics
633359
• Total Posts
3011513
• ### Who's Online (See full list)

There are no registered users currently online

×