Search code examples
collisionheightmap

Finding height of point on height map triangles


This question has to exist somewhere, but I'm not sure what it would be called.

Following a height map, a terrain exists of right angle triangles. Each point of each triangle takes its height (y) from the height map.

If the character is at a particular set of coordinates, you can figure out which triangle they are in. What I forget how to do is determine the height of that point on the triangle by using the height of the other 3 points.


Solution

  • One way to do this is using barycentric coordinates. Then you can use linear interpolation to get the height coordinate. (Source: https://en.wikipedia.org/wiki/Barycentric_coordinate_system)

    For convenience I'll relabel the horizontal coordinates as x and y, and your vertical coordinate from y to z.

    If you have a point (x, y), within some triangle defined by three points (x1, y1, z1), (x2, y2, z2), (x3, y3, z3), use these to calculate the parameters:

    enter image description here

    Then use the parameters to calculate the z-coordinate:

    enter image description here