# Finding the height of a point inside of a triangle.

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

## Recommended Posts

My question concerns finding the height of an individual point in a quad, or triangle. This has to do with a heightmap, and everything is scaled down so the triangle has a side =1. I have gotten the column and row of the quad the camera is in, and I have also gotten the height of the 4 vertices that the point is in. My problem is, the code that I was using to find the height produces bumps when the camera moves inside on the quad from the top to the bottom quad (~1-2 units up or down) The effect makes the entire screen bounce when it should be smooth. Does anyone have an equasion for this? I have row, column heights for the vertices, how far from vertex A the point is (to determine what triangle). I just need to know the proper way to put everything to remove the bumps.

A---------------B< top triangle
|              /|
|     X      /  |
|          /    | < as soon as the camera moves from
|        /      |   tri 1 to tri 2 the height changes ~1-2 units causing bumps
|      /        |
|    /          |
|  /            |< bottom triangle
|/              |
C---------------D

What is the proper code/equasion for interpolating this (the best/fastest way)?

##### Share on other sites
When you say that which triangle you're in is determined by the distance from vertex A, that doesn't sound quite right (unless there's more to it than that). For debugging you might try drawing in red the outline of the triangle corresponding to the object position (maybe an object other than the camera so you can track it more easily). You might find that this is where your algorithm is messing up. Otherwise, intersecting a vertical ray or using barycentric coordinates are two effective ways of getting the height. If this doesn't help, you might post your code so that we can more easily see what might be wrong.

##### Share on other sites
well, It is determined that when the distance from A is 0 the object is directly on A, and when it is 1, it is directly on B or C (depending on x, or z) these values only are in the range 0,1.

here is how I do it:

x,z are the x and z coordinates of the object
unsigned short column = floorf(x);unsigned short row = floorf(z); //after much testing, the rows and columns are correct for the given positionfloat dx = x - column;float dz = z - row;	if(dz < 1.0f - dx) //Upper triangleelse = bottom triangle

##### Share on other sites
Perhaps?

(HA+X(HA-HB)+HA+Y(HA-HC))/2
and
(HA+X(HA-HB)+HA+Y(HB-HD))/2

##### Share on other sites
Assuming that AB and CD are parallel to the x axis, and CA and DB are parallel to the Y axis, and Z is the height, and AY-CY == 1 and BX-CX == 1, then ...

If X is in the upper triangle,
    XZ = AZ + (XX-AX)*(BZ-AZ) + (XY-AY)*(CZ-AZ)
If X is in the lower triangle,
    XZ = DZ + (XX-DX)*(CZ-DZ) + (XY-DY)*(BZ-DZ)