Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Terrain to Vehicle normal

Started by Yaskil Jun 22, 2008 at 3:21 PM 2 replies 1k views
Original Post
Yaskil
Yaskil
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?
Life is beautiful
Yaskil
Yaskil
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.
Life is beautiful
oliii
oliii
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);
Everything is better with Metal.

Topic Locked

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

Sign in to reply to this topic.