|See what's going on with flipcode!|
Question submitted by (09 December 1999)
|Return to The Archives|
|I am programming a 3D spaceship game in whhich the ship flies across a landscape.I am using D3D IM and need to know how to do accurate collision detection on a landscape.All me games up until now have not needed to hug a land scape so i have used bounding boxes,obviously this is no good for a land scape.|
I assume that you want a ship, much like a hover-craft to follow not
only the altitudes of the landscape, but also the angle of the landscape
(pitching up when going up hill and pitching sideways when going down a
slope at an angle, etc.)
To get the angle of the craft, you can use the normal of the ground polygon below it. This normal is your up (Y) vector. By doing a cross product of the direction (Z) vector (which way the craft is pointing) with the up vector, you'll get the third (X) vector required to form a complete matrix.
The problem isn't solved yet. First, this will not produce an orthogonal matrix. Second, as your ship hovers the dynamics of the terrain, you'll need to adjust your direction vector to accommodate. Fortunately, these are both solved with one extra step to the preceding explanation.
Just cross the resulting X vector with the resulting Y vector to get the new direction (Z) vector. This vector will also replace the original direction vector in the matrix, causing it to be an orthogonal matrix.
You might want to keep your ship's altitude relatively consistent with the terrain. You can do this by determining the altitude of the terrain at a point directly below your ship. You can use this to adjust the altitude of the ship. By keeping track of the last few altitudes used in previous frames, you have all the information you need to filter (in this case, average) the new altitude with the past altitudes. This can be used to get the effect of a slowly bobbing ship over bumpy terrain, or even slight "jumps" when the terrain drops abruptly away from the ship.
You'll probably have more work to do still. You'll probably want to smooth out your craft's movement over the terrain. Rather than follow the abrupt changes inherent in planar polygons, you can filter it's movement by interpolating the normals of the neighboring polygons so that the input "up" vector (used in the procedure explained above) causes smoother transitions from polygon to polygon across the terrain.
Response provided by Paul Nettle
This article was originally an entry in flipCode's Fountain of Knowledge, an open Question and Answer column that no longer exists.