Advertisement

Smooth character movement over a heightmapped terrain.

Started by August 17, 2012 08:30 AM
4 comments, last by Wilhelm van Huyssteen 12 years, 6 months ago
Hi.

Assume that Z is the up axis.

I have a heightmapped terrain. A 1024x1024 texture maps to a 1024x1024 vert mesh that represents a 1024m x1024m game area (In reality it actualy gets splitted into much smaller meshes so proper LOD and frustum culling can happen but thats irrelevant to this question). I have taken the characters x and y coords. Rounded them to the nearest Integer and used that to retrieve the height value from the heightmap texture and then I set the characters z coord to that value. Now my character can move over the terrain but its ofcourse horribly jumpy.

I need to take the height values of the 4 heightmap pixels around my character and based on where he is reletive to those 4 pixels work out the final height value but im strugling with that abit.

what i tried so far was simply this:

aDist,bDist,cDist,dDist; //characters distance to the 4 surrounding pixels (on the xy plane).
aHeight,bHeight,cHeight,dHeight; //the 4 surrounding pixels height value\

totalDist = aDist + bDist + cDist + dDist;
aDist = aDist / totalDist;
bDist = bDist / totalDist;
cDist = cDist / totalDist;
dDist = dDist / totalDist;
finalHeight = aHeight * aDist + bHeight * bDist + cHeight * cDist + dHeight * dDist;

Unless i made a mistake somewhere in my code (wich is very possible biggrin.png) this is not the solution.

Any help appreciated. Thnx in Advance!
assuming u use a regular rectangular grid in the mesh, u have to determine what triangle your player steps on

then use the three height values of the corressponding triangle as barycentric coordinates to calculate the height
Advertisement
with 4 surrounding pixels u mean the vertex positions?
Yes. I should have been more clear. with the 4 surrounding pixels I meant the vertex positions of the 4 surrounding vertices in the terrain mesh (wich corresponds with the "4 surrounding pixels" in the texture).

And yes i use a regular rectangular grid in the mesh.
http://en.wikipedia.org/wiki/Bilinear_interpolation
I got it working perfectly using barycentric interpolation. Thnx for pointing me in the right direction oggs91.

This topic is closed to new replies.

Advertisement