# maintaining terrain/coll'n detection

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

## Recommended Posts

I see a lot of articles on collision detection and was hoping someone could point me to some of the more useful ones. I'm looking for collision detection with the terrain, keeping the char's feet on the ground, as well as preventing forward motion through obstructions. I was hoping these two tests could be performed in one fell swoop. If it makes a difference, I'm coding in DX (C++).

##### Share on other sites
One easy way to deal with collision with terrain is to use a heightmap.

Load the heightmap into memory, and sample it to get the height. You can use interpolation to get a smooth and accurate height.

This way you avoid having to shoot rays to it.

There are other ways, but this is one of the methods I have seen on some games and that seems to be fairly cheap.

i.e.

// pseudocode
CPhysics::GetTerrainHeightAtPoint(point)
{
// find x/z point on the heightmap
float x=0; float y=0;
TranslateCoordinatesToTexture(m_heightMap, &x, &y);

// interpolate
int x0= (int)x;
int y0= (int)y;
int x1= x0+1;
int y1= y0;
int x2= x0;
int y2= y0+1;
int x3= x0+1;
int y3= y0+1;

// sample at four points and interpolate
float h0 = SampleTexture(m_heightMap, x0, y0);
float h1 = SampleTexture(m_heightMap, x1, y1);
float h2 = SampleTexture(m_heightMap, x3, y3);
float h3 = SampleTexture(m_heightMap, x4, y4);

return interpolate(h0, h1, h2, h3, x, y); // interpolate based on fractional parts of x, y

}

##### Share on other sites
will this cover obstructions (walls, trees, etc), or only work for terrain-walking coll'n detection?
Also, I'm making most my terrain in 3ds max and loading the .x files in dx as meshes, breaking them down with octree to keep from rendering too large a mesh at a time. Will heightmap coll'n detection work for .x meshes?

##### Share on other sites
Quote:
 Original post by frankypoowill this cover obstructions (walls, trees, etc), or only work for terrain-walking coll'n detection?Also, I'm making most my terrain in 3ds max and loading the .x files in dx as meshes, breaking them down with octree to keep from rendering too large a mesh at a time. Will heightmap coll'n detection work for .x meshes?

No. The method described above works by samping a greyscale image of a landscape. For what you've described you might want to look at General Collision Detection for Games Using Ellipsoids.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634076
• Total Posts
3015353
×