# Smooth character movement over a heightmapped terrain.

## Recommended Posts

EternityZA    1226
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;
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 [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]) this is not the solution.

Any help appreciated. Thnx in Advance! Edited by Wilhelm van Huyssteen

##### Share on other sites
oggs91    421
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

##### Share on other sites
oggs91    421
with 4 surrounding pixels u mean the vertex positions?

##### Share on other sites
EternityZA    1226
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. Edited by Wilhelm van Huyssteen

##### Share on other sites
Inferiarum    739
http://en.wikipedia.org/wiki/Bilinear_interpolation

##### Share on other sites
EternityZA    1226
I got it working perfectly using barycentric interpolation. Thnx for pointing me in the right direction oggs91.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account