Height test question (for terrain)

I was wondering if anyone could give me a suggestion (or tutorial link) that could help me in finding the correct “y” value anywhere on a given terrain.

I’ve loaded a mountain terrain (from 3d studio max)into my opengl program and I’m not sure of a way to calulate the “y” value of what’s directly under the “camera” when I move around in first person. Obviously I don’t want to walk through a mountain but climb it. Thank you

I never did it myself, but I see 2 methods… One easy (not efficient actually), and one a bit trickier…

btw I’m pretty sure they are better methods!


(1) find the triangle above which you are (work with 2D coords)
(2) do the calculations depending on your exact position using gradients (in 3D)


When you move, if you go out of the triangle above which you were, just consider the ones next to the previous triangle. If you can’t, you probably moved too quickly (in that case, seek among all the triangles). ** This method require that you keep an array of pointers to a list in which you describe what are the triangles next to another one … So begin with the first method, and when it works, if it’s slow you’ll be able to optimize…

Hello Ace_Man!

Well, if you render your landscape with triangles like this (sorry about the pic) :

1 ----- 2
|A /|
| / |
|/ B|
0 ----- 3

Then if you are above the triangle A use the formula y=(y1-y0)(z1-Pz)/(z1-z0)-(y2-y0)(x2-Px)/(x2-x0) and if you are above B then y=(y3-y0)(x3-Px)/(x3-x0)-(y2-y0)(x2-Px)/(x2-x0) where Px and Pz are your x and z coordinates respectively. Hope this works, I’m not able to test it right now.

Cheers Osku