# Orienting a vehicle on Terrain

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

## Recommended Posts

Okay, so I have a rectangular vehicle sitting on a terrain made by a height map. What I want to do is rotate it so that all four wheels are touching the ground. What I have done is calculated a normal for the coordinates that each tire should be touching the ground at. So what I end up having is a vector that represents the transformed Y axis of the vehicle. I thought I really had something until I found out that I don't know how to efficiently transform this vector into rotational values for the world matrix. I know that I can do this with trigonometry but it would take a lot of code to do it the way I know how to and I think there should be a better way. Is there an easy way to transform this vector into rotational values or is there a D3DX function that will do it? Or am I going about this completely wrong?

##### Share on other sites
I was doing the same thing a little while ago, but never really went into it due to time constraints forcing me to abandon the project. I had the same issue, something that sounds so simple, but when it comes to implementing it I was left staring at the screen for a while wondering how the hell to actually go about it.

It's been something thats stayed in the back of my mind as I've done other things and I believe the solution is in "rigid body dynamics". I haven't actually read into much or tried to solve the problem recently, but when I do that's going to be the first place I look.

Hope it helps, because if it does that's my question answered too. ;)

##### Share on other sites
Keep in mind that it will be difficult to find a perfect solution; that's why cars have shock absorbers.

##### Share on other sites
Make a simple point-ground collision test a few centimeters under every tire. If one of the front wheels hit the ground, move the car up a few centimers and rotate it backwards 1-2degrees around the X-axis, if one of the right wheels hit, move it up and rotate it left around the Z-axis etc etc...

I did this for a box-box collision-test in two dimensions with just some random rotation-factor and it works much much better than you'd expect such a simple sollution to do.
If you take time to adjust the rotation/offset-factors i belive it can become realy realistic. You can try to use ray-ground test instead of points and calculate the length of the ray that intersects with the ground and use that length to calculate an even more exact rotationfactor.

##### Share on other sites
instinKt-

Thanks. That seems like a very good way to solve the problem however I have neither the math skills or patience to finish learning it :). I wish you luck on your project. You can try that and you would probably get really good results however I am personally using Chimaira's way and I would recommend that if you need another way to try that.

Chimaira-

Thanks. That solution works well.

##### Share on other sites
Unless you're willing to move the wheels up and down on there suspensions relative to the car body you might not actualy find a solution for exactly the same reason why 3 legged stools never rock but 4 legged stools can. The usual way to handle the problem is to follow these steps each update loop:

1) Shoot rays from your car's suspension hard points along the car's negative y axis and intersect these with the terrain.

2) Place the wheels at the intersection points (+ wheel radius * car Y axis).

3) Calculate the displacement of each suspension spring from its rest point (distance between wheel and suspension hard point along car y axis - rest length) and use this to apply forces to the car body according to your spring equation. (You'll also want to track wheel velocity along the car y axis so you can apply damping forces too).

4) Intergrate your car's rigidbody equations forward in time.

Over several loops the car body and wheels will eventualy come to rest in their equilibrium positions.

##### Share on other sites
Okay thanks Motorherp. I actually put that in when I started collision for the wheels as Chimaira suggested. Thanks to everyone for your help!

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002131
×