Advertisement Jump to content
Sign in to follow this  
Sir Sapo

Terrain Collision

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

What is the method used to determine the height of the ground is at a certain x,y point on a heightmap? I heard somewhere that you have to find what square in the grid an object is and then somehow average the height of those 4 points together to get the final height, is this correct?

Share this post

Link to post
Share on other sites
A gridcell consist out of 4 points. If you take the average height of each point, then you got the relative height of the gridcell itself.

A heightmap usually is a 8-bit bitmap, meaning every pixels can have a ranged value between 0 and 255. You could say to your engine, that value 127 is the sealevel for example and every value higher or lower is 1.0f or 100.0f in absolute z-coordinates higher or lower.
Every pixel in that bitmap could be a point in the gridfield.

I hope this is what would like to know, else feel free to ask.

Share this post

Link to post
Share on other sites
Basically yeah, that's the process. You get the 4 points for that cell and do a bilinear interpolation.

Here's my source for the meat of the algo:

//the x and y coords are all either int(x) or int(x)+1
//use all 4 combos
//x0 <= x <= x1
//z0 <= z <= z1
int x0, z0, x1, z1;
x0 = int(x);
x1 = x0 + 1;
z0 = int(z);
z1 = z0 + 1;

//we are just slightly off the edge of the terrain if x1 or z1 is off the terrain
if( x0 < 0.0 || z0 < 0.0 )
return std::numeric_limits<float>::min();

if( x1 >= m_Width || z1 >= m_Height )
return std::numeric_limits<float>::min();

float h00, h01, h10, h11;
h00 = m_Heights[x0*m_Height+z0];
h01 = m_Heights[x0*m_Height+z1];
h10 = m_Heights[x1*m_Height+z0];
h11 = m_Heights[x1*m_Height+z1];

//NOTE: The faces are indexed like this:
//one tri is h00, h01, h10
//other tri is h01, h10, h11 --- NOT h00 in this tri
//This is important; most terrain engines appear to wind the verts along the other diagonal

//find distance from camera to x0,z0
float tx, ty;
tx = x - x0;
ty = z - z0;

//Now, how do we figure out the height from all this info?
//We could try a bilerp...
float txty = tx * ty;
float Height = h00 * (1.0f - ty - tx + txty)
+ h10 * (tx - txty)
+ h11 * txty
+ h01 * (ty - txty);

return Height;

Fairly simple...get the four points, look up the heights, get the factors on x and z, and average them all together. If you work out the sum of the 4 factors, you'll find they add up to 1.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!