I have designed a height mapped terrain, made of triangles. I want to find the exact height of a point on the height map. I want to use this to have an object hug the terrain.
The terrain is built so that two triangles make a square.
I have the code to check which triangle the point falls in but cannot find out how to get the height at the point. My code only returns the height of one corner of the triangle.
Also my code to find the correct triangle could be better.
Let’s say the actual unit values for your terrain range from x1 to x2 (let’s leave out the z-axis for now) If you have an array, it would range from 0 to n. If you want a point on the map, let’s say x3, first check that it is between x1 and x2. The coordinate of the point would be x3/length, or x3/(x2-x1). Notice that these two expressions are equal: x3/(x2-x1)=p/(n-0), where p is the point you are looking for. To find p, you get: p=n*x3/(x2-x1). Now, do the same for the z-axis and you have:
px = nxx3/(x2-x1)
pz = nzz3/(z2-z1)
Now, what you do to get the point is:
array[(int)px][(int)pz]
This will give you the height of the triangle closest to it, but it will not give you the exact height in the middle of the triangle. I do not know the formula for this, but I have seen it before and it contains 4 sqrt() calls. You do not want to be calling sqrt() hundreds of times per frame. This way is easy, fast, and effective. If anyone has the formula to find the exact height of a point, please post it! If I have any mistakes in my math, please say so.
Can you please clairfy what the vars in your equasion are.
[/b]
Oh, I guessed I just assumed you had some knowledge of linear algebra. Unfortunately it doesn’t get more trivial than that. I suggest you look up some basic linear algebra docs.
Plane equation for triangle: ax + by + cz +d = 0
(a,b,c) = triangle’s normal
d - distance(shortest) from origin to plane
(x,y,z) = point in 3d space, on plane