Game Development Community

Calculating transform to follow the terrain

by Keith Johnston · in Technical Issues · 02/12/2003 (1:25 pm) · 4 replies

I've been struggling with this for a few hours now: I have a heading, which is under the player's control, and the normal of the terrain below the vehicle. I want to set the transform of the vehicle to align the vehicle with the terrain but have it point in the direction specified by the heading.

I've been trying this, and it doesn't work right at all - as I turn the vehicle when it is on a hill, it sometimes turns upside down. I've looked at the vehicle code in the 1.1.2 branch (the one I'm using) to see how they do it for the hovercraft, but I don't quite understand what is being done there.

MatrixF rot;
	   AngAxisF aa(normal,mHeading);
	   aa.setMatrix(&rot);
   
	   MatrixF curTrans = getTransform();
	   curTrans[0] = rot[0];
	   curTrans[1] = rot[1];
	   curTrans[2] = rot[2];
	   curTrans[4] = rot[4];
	   curTrans[5] = rot[5];
	   curTrans[6] = rot[6];
	   curTrans[8] = rot[8];
	   curTrans[9] = rot[9];
	   curTrans[10] = rot[10];   
	   setTransform(curTrans);

So as you can see I'm using the normal as the axis, and the heading as the rotation around that axis. Seems like this would work, but I must be missing something.

#1
02/12/2003 (2:01 pm)
Hey, if I remember right, Joe Maruschak released a code snipped to conform a vehicle to the plane of the poly under it a while back. Do a search for that?

What youre doing makes no sense to me :) what youre ideally after is a vehicle forward vector thats perpendicular to the plane youre travelling on right?

I'd be tempted to cross product a vehicle sideways vector with the current plane and use that scaled to the forward velocity.

Or of course, use Joe's example :)

Phil.
#2
02/12/2003 (2:12 pm)
hey guys, is there a method for making weapons follow the terrain? I need this for things like tornados and such which will run along the base of the terrain without coliding into it.
#3
02/12/2003 (2:21 pm)
Well I did some more searching on deja and this time found just what I needed. The trick is to get the sideways vector first by taking a cross product between the normal and the heading. Then cross the sideways and the normal to get the front vector. Voila - the vehicle follows the terrain.

Sometimes just posting the question spurs me to search for the answer harder... :-)

F32 pitch=0;
VectorF heading;
MathUtils::getVectorFromAngles(heading,mHeading,pitch);
heading.normalize();

VectorF sideWays;
mCross(heading,averageNormal,&sideWays);
sideWays.normalize();
	   
VectorF front;
mCross(averageNormal,sideWays,&front);
front.normalize();

MatrixF curTrans(true);	   
curTrans.setColumn(0,sideWays);
curTrans.setColumn(1,front);
curTrans.setColumn(2,averageNormal);
curTrans.setPosition(getPosition());
	   
setTransform(curTrans);
#4
02/12/2003 (3:06 pm)
Just a note: Joe's code is good, but it has some weird bugs; it will sometimes flip you upside down, and it really screws up the rotation controls at times. I think it's because he has a flawed matrix transform; I spent some time trying to fix this, but I had no luck (nor did he :).