# Pitching against a terrain (a little long)

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

## Recommended Posts

So, I am trying to get a small vehicle to pitch against the terrain it is driving on so that when its going up or down hill it actually looks normal. My problem is essentially that I dont know a good way to tell if I am going straight... My original design simply had me getting the angle between the vector in the terrain that the vehicle is on and the vehicles position before I corrected it to have it go up-hill (for example if it were going uphill it sort of just drives straight and gets bumped up to keep it on the terrain). This gave me angles that were way too small. My second design had me taking the vehicles position and getting the angle between that vector and the same vector without the height. This worked beautifully going up or down hill (had to figure that out myself because with this scheme I never got a negative angle). It sucked when I was on straight terrain though. Basically I would find myself pretty much always rotated somewhat and in certain cases perpendicular to the terrain. My third scheme which I havent tried is to define some height level as ground level and anything above or below that is above or below ground, respectively. I would then get the angle between the vehicles actual position and the x and z coordinates with the ground height. This sounds like it could work assuming the only time i have flat terrain is at ground level but my terrain has canyons and pleataus so I am out of luck. SOmething that just occured to me...if I keep track of net change in height I could use that to assume if Ive gone up or down hill and just ignore pitching if that change isnt past a certain threshold...does that sound like it might work or is there an obvious problem that I am not seeing at the moment? ALternatively, instead of fixing one of my schemes, how is this thing normally done? Thanks.

##### Share on other sites
I'm just brainstorming here, but.

Couldn't you just keep track of the height of the terrain under each axle of the vehicle and pitch the vehicle accordingly? That's really what it is, isn't it? If the terrain under the front axle is higher than the terrain under the rear axle, you are pitched up, and vice versa.

You could even set a particular threshhold for terrain that's too steep to climb this way.

Add a check for a point midway along the length of each side of the vehicle and you get roll as well as pitch, in case the terrain is uneven side to side.

Edit: clarification

##### Share on other sites
That sounds like a pretty sweet idea ;D For some reason I never considered using more than one vertex to check the vehicles height...

When I get back from classes I will try that out immediately and let you know how it worked.

Thanks.

##### Share on other sites
do a dotproduct between the vehicles up and the normal of the terrain, if theyre the same the result will be 1.0

##### Share on other sites
Ok...

Using multiple vectors to get the height at the front, back, left and right is giving me really small angles and so it doesnt look realistic enough.

I am going to try using the up vector (always 0, 1, 0) and the terrain's normal vector at the vehicles position to get the angle. My question is this: Will using the vehicles right vector and the terrain's normal vector give me the right angle for roll?

##### Share on other sites
Simple method:

new_right_vector = old_right_vector
new_up_vector = terrain_normal
new_forward_vector = CrossProduct(new_up_vector, new_right_vector)

Then you use these vectors to build your orientation matrix.

The simplest way to get the terrain_normal is to use the normal of the point under the center of the vehicle for example. You can also get fancy and average the normals of the polygons under each wheel of the vehicle.

james

##### Share on other sites
find the angle between the horizontal axis (x and then z) and the vector perpendicular to the normal along the same axis, this will give you your rotation angle for that axis.

(incase you didnt know, dot product yeilds a scalar value that can be used to determin the angle between two normalised vectors: clicky)

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 9
• 19
• 9
• 31
• 16
• ### Forum Statistics

• Total Topics
632617
• Total Posts
3007459

×