problem with terrain following

Recommended Posts

OK- I have succesfully generated a curving terrain, a fast 'terrain following' routine, and a car that moves in all 3 dimensions. But, I am having trouble matching my car's 'normal' to the terrain! I have tried some sin/cos/tan functions, and yes, I got some results, but I get the feeling that this is just not the right way to do. The main problem is that if I start moving the car around in X and Y directions, things 'don't work anymore'. - How do I find my car normal, and how do I set the car normal exact with the terrain normal, no matter what the direction of the car is? - And more difficult, how do I set the camera view (yaw/[pitch/roll), as seen from a person that DRIVES the car? - Should I use d3dxmatrix calculations, and if so, how? (pseudocode, or C++ , or Pascal- I can understand them all) Thanks for any help you can give me, and I hope I was clear in my questions, M.

Share on other sites
If you have the normal to which you want to align, and your object orientation is represented by a quaternion, you can find the quaternion that will rotate your object into alignment with the normal. Then you can multiply that by your current quaternion to get your goal quaternion, and then slerp to that for added smoothness. Obviously there are some details involved, but it works very well.

Share on other sites
I have not yet used quaternions into my application.. I was hoping to get things really as simple as possible, but if that's the only solution I'll study that topic.. But are matrices that come with Direct3D not enough to get the job done?

Share on other sites
Ok, then I would try this.

1. Check to see if your current up vector is very close to the goal up vector. If so, just snap it to the new up vector, and update your forward and side vectors accordingly.

2. Otherwise, take the cross product of your current up vector and the goal vector and normalize it. This is the axis you will be rotating around. Then (assuming your up vector and the goal vector are normalized), take acos(up.Dot(goal)). This is the angle by which you want to rotate. (You may have to switch the cross product arguments around to get the right results.)

3. If you want a smooth transition to the new alignment, scale the angle by some value. If you don't mind just snapping to the new alignment, skip this step.

4. Create an axis-angle matrix from this info (I would think D3D has a function for this?). Multiply it by your current orientation matrix. The result is your new orientation.

I don't know that this is exactly equivalent to the quaternion approach - I've never tried it. But it's the best solution I can think of at the moment using matrices. Perhaps someone else can suggest something better.

Share on other sites
Thank you, I appreciate it!

Share on other sites
another procedure to snap objects to a terrain is to generate an orthonormal matrix (orientation matrix) using the terrain normal, and another vector (usually, the current foward direction of the object).

Vector SideVector = NormalVector.Cross(ForwardVector);
Side.Normalise();
ForwardVector = SideVector.Cross(NormalVector);

Matrix_3x3 Orientation;
Orientation.SetColumn(0, SideVector);
Orientation.SetColumn(1, NormalVector);
Orientation.SetColumn(2, ForwardVector);

... or set rows, if your matrix ssytem is row-major (one is the transpose of the other).

To smooth it out, you can use quaternion (convert the matrix to quaternion form), and slerp towards it from the current orientation quaternion.

Share on other sites
same as jyk basically :)

Share on other sites
OK, thanks, I will let this sink in. (I'm not a genius unfortunately..)

Share on other sites
I use the heights of the terrain and then simple trigonometry to find new pitch and roll angles.
Very simple though quite restricted.
More details available if interested.

Share on other sites
This link is the same/similar solution as Oliii and jyk, but there's a picture explaining how to obtain the orthonormal vectors you need

Create an account

Register a new account

• Forum Statistics

• Total Topics
628318
• Total Posts
2982043

• 9
• 9
• 13
• 11
• 15