Sign in to follow this  
Marmin

problem with terrain following

Recommended Posts

Marmin    523
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 this post


Link to post
Share on other sites
jyk    2094
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 this post


Link to post
Share on other sites
Marmin    523
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 this post


Link to post
Share on other sites
jyk    2094
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 this post


Link to post
Share on other sites
oliii    2196
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 this post


Link to post
Share on other sites
Aph3x    288
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this