snapping an object (box/vehicle) to a terrain

Started by
4 comments, last by Pirosan 18 years ago
Hey, I'm working on an rts and i'm looking for an equasion or set of equasions that takes the 4 corners of the bottom of a box or cube and snaps them to a point on a terrain. I'm doing this so when the terrain slopes, the units (vehicles) will be able to tilt and rotate with it. For the input, i have the height of each corner in relation to the terrain.
Bow before me... for i am l33t!
Advertisement
There are 2 things you need:
a) the position of your object
b) the orientation of your object

For (a): The xyz for your object should not be hard to get. It seems like you have the x and y of your object, and you can figure out the z based on the heights of the terrain corners.

For (b): Compute the normal to your terrain. Get 2 vectors that are in the plain (point1 - point2 and point2 - point3) and take the cross product. This will give you a vector pointing "up" from the plain. Then you will need to rotate your units such that their up vector is the same as this vector. The easiest way to do this is if each unit has its own vectors for forward, right, and up. You can simply set the units->up to the terrain->up. Also, you will need to change the units forward vector because it is now looking in a different direction. The new forward vector will be the crossproduct of the units->up with the units->right. The units->right does not change because the unit is driving forward and only rotating its nose up (rotating about its right vector). This is my favorite world matrix - DIRECTX matrices only. (this will be different for openGL).

world._11 = vRight.x,
world._12 = vRight.y,
world._13 = vRight.z,
world._14 = 0;
world._21 = vUp.x,
world._22 = vUp.y,
world._23 = vUp.z,
world._24 = 0;
world._31 = vForward.x,
world._32 = vForward.y,
world._33 = vForward.z,
world._34 = 0;
world._41 = position.x,
world._42 = position.y,
world._43 = position.z,
world._44 = 1.0f;

This matrix will position and rotate any object. The only thing it won't do is scale your object.
-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
Quote:Original post by blaze02
There are 2 things you need:
a) the position of your object
b) the orientation of your object

For (a): The xyz for your object should not be hard to get. It seems like you have the x and y of your object, and you can figure out the z based on the heights of the terrain corners.

For (b): Compute the normal to your terrain. Get 2 vectors that are in the plain (point1 - point2 and point2 - point3) and take the cross product. This will give you a vector pointing "up" from the plain. Then you will need to rotate your units such that their up vector is the same as this vector. The easiest way to do this is if each unit has its own vectors for forward, right, and up. You can simply set the units->up to the terrain->up. Also, you will need to change the units forward vector because it is now looking in a different direction. The new forward vector will be the crossproduct of the units->up with the units->right. The units->right does not change because the unit is driving forward and only rotating its nose up (rotating about its right vector). This is my favorite world matrix - DIRECTX matrices only. (this will be different for openGL).

world._11 = vRight.x,
world._12 = vRight.y,
world._13 = vRight.z,
world._14 = 0;
world._21 = vUp.x,
world._22 = vUp.y,
world._23 = vUp.z,
world._24 = 0;
world._31 = vForward.x,
world._32 = vForward.y,
world._33 = vForward.z,
world._34 = 0;
world._41 = position.x,
world._42 = position.y,
world._43 = position.z,
world._44 = 1.0f;

This matrix will position and rotate any object. The only thing it won't do is scale your object.
That doesn't actually address the OP's question, which is how to get a model to 'sit' correctly on the terrain in terms of four corner positions.

@The OP: I know the 'four corners' question has been discussed here on gdnet, but I can't recall if there's a standard/easy solution; maybe someone else knows. Another option is a physics-based solution (shock absorbers and so on), but that's quite a bit more complicated.

If you're ok with 'fudging' a little (which might involve the vehicle being elevated a bit off of the terrain and/or occasionally clipping into the terrain), the process of simply aligning an object with the terrain normal is much more manageable. blaze02's method isn't the way I would do it, but it might be a perfectly good solution (can't say for sure).
Well there are two problems with setting it to the normal of the terrain.

First is that the object may be on two planes at once so there will be quite a lot of room for error if i'm just picking the polygon thats right under the object. The second is that moving over one polygon to the other will cause the object to snap into place... i want it to move smoothly, which is why i wanted to take in the four points so that it could be on two polygons at once and kind of interpolate between the two.
Bow before me... for i am l33t!
If you just want to find the height of the terrain at the four corners and adjust the vertical position of those parts of the object accordingly, that's easy. (Select the tile on the terrain and then interpolate, four times.) But that only works on low gradients, because it assumes that the plan view of the object's contact points doesn't change, whereas of course it gets smaller due to the rotation. A two-pass technique should be OK for a rough and ready solution (bring in all four corners by sin(gradient) and recalculate).

If you want to do it properly, you need a physics library and stuff.

Edit: Okay, so you already have the four heights. Those four points define a plane only if you're very lucky, so either you have to deform your object (maybe you have two axles with variable suspension) or accept that only three corners will touch the ground.
Yes, i agree. In many cases only three wheels may touch the ground, but i'll probably avarage out the difference to make it as close as possibe.

But now that I have that hypothetical plane, how do i find the rotation for the object to set it against that plane?
Bow before me... for i am l33t!

This topic is closed to new replies.

Advertisement