Terrain Renderer - How to Get Height at [x, y]?

I’ve checked a few other posts but I’m still not getting this.

My map is a height field, with fixed distance x/y and variable z.

My eyepoint goes from [0.0…1.0] before you wrap to the next x/y.

Finally, my map is composed of two tris per “square”.

| / |

How in the world do I get the exact z value wherever I’m at over my map?

Any help would be greatly appreciated.


Your current x/z position should point to the triangle you are currently on. (cell) Calculate this however you see fit. Remember a cell is the triangle you are on and position is the real grid coordinate you are on.

Solve the plane equation for the point that you’re standing on.

The plane equation is (x)nx + (y)ny + (z)nz + c = 0.

{nx, ny, nz} is the plane normal of the current cell.

c is a constant offset which can be solved by plugging in
the coordinates of one of the corner points of the cell(triangle).

Your current position on the map is {x,z} solve for y.

In pseudocode:

// Get my current position on the map
pos = {something}; // (x,z)

// Figure out the current cell I’m standing in
cell = FindCell(pos);// use (x,z) to figure to calculate this - remember cell is the triangle that you are on

// Get the plane normal of the cell or triangle
normal = cell.normal; // just like calculating a normal for lighting

// Get one of the corner points of the cell
point = cell.point(1);

// Solve for c, the plane equation offset
c = normal.x * point.x + normal.y * point.y + normal.z * point.z;

// Now solve for the y component of my position
pos.y = (c - normal.x * pos.x - normal.z * pos.z) / normal.y;

This works like an absolute charm and is quite cpu friendly.

the source code turns out to be quite lengthy as you are normalising and calculating your current position and cell coordinates. But all in all it is not really complicated.

yes that is how i figured out the terrain height for my program. a suggestion though i assume you will be using lighting so you will need a normal for each square so if you store the normals in a global array you could use the precalculated normals in Findheight function which would speed up the calculations a little. you can see my program at www.geocities.com/porter_loring/
if you want to see how i did it just email me