Terrain - How to Get Height at [x,y] - Still Having Probs! Help Plz?

I’ve done my best to implement the code Dans gave me in my previous post, but with mixed results. It gets me in the ballpark but it’s chopping like mad on me. The transitions are not smooth between tris.

void getFaceNormal(int x, int y, float n[3])
float v1[3], v2[3], v3[3], v4[3];
float p1[3], p2[3];

float s=(float)step;

v1[0]=0.0f; v1[1]=0.0f; v1[2]=getMapZ(x,y);
v2[0]=0.0f; v2[1]=-s; v2[2]=getMapZ(x,y+1);
v3[0]=s; v3[1]=0.0f; v3[2]=getMapZ(x+1,y);
v4[0]=s; v4[1]=-s; v4[2]=getMapZ(x+1,y+1);

if (cam.y>=cam.x)


float getCamZ(void)
int mx=(int)mapX,

float n[3], p[3];

float c;



c=(n[1]*p[1] + n[2]*p[2]);

return (c - n[0]*cam.x - n[1]*cam.y) / n[2];

Basically I just get the face normal (somebody plz let me know if I’m doing that part wrong) and then find ‘c’ using the lower left point of each map square (which is common to both cells in that square), and finally calculate the height.

The result? My z values “jump” when I cross cell boundaries. At least, that’s what it looks like.

if you want to see what I’m talking about.

Thanks for any help,

Ok ill assume you have figured what cell/tri you are on and have its world coordinates. You also know your objects world x,z coordinates

static float tri[3][3];

tri[0][0] = (float)x1; // plug in cell coords
tri[0][1] = (float)y1;
tri[0][2] = (float)z1;

tri[1][0] = (float)x2;
tri[1][1] = (float)y2;
tri[1][2] = (float)z2;

tri[2][0] = (float)x3;
tri[2][1] = (float)y3;
tri[2][2] = (float)z3;

static float normal[3];
GetNormalised(tri, normal); // this func return a normal for tri (normalised/unit of 1) to array called normal… make sure the result IS normalised. These normals could and more often than not should be pre-calculated.

// Solve for c, the plane equation offset
float c = normal[0] * tri[0][0] + normal[1] * tri[0][1] + normal[2] * tri[0][2];

// Now solve for the y component of position
float ypos = (c - normal[0] * xpos - normal[2] * zpos) / normal[1];


Hope that helps you some more.

I finally got it. Thanks Dans for your help. I really appreciate it.

Persevering under your expert tutelage I finally found the cause of my problems. My “xpos” and “ypos” (and BTW I force the frustum to be x/y with z up, so I made the necessary changes to the calculations) I had clamped to 0.0f … 1.0f. Which was fine for the xpos but not the y. I clamped the ypos to 0.0f … -1.0f and it worked great.

Except I still experience a small “bump” at cell boundaries which causes the view to jump a small amount, but it’s almost negligible in effect. I’ll make it so the Z gradually changes instead of being absolutely set each frame, which should help, but if you could offer any insight I’d be most appreciative.

Thanks in advance.


To remove any obvious jumps in the view, you can just interpolate (actually slerp) between view vectors. Gotta chose my words carefully now or this thread is likely to degrade into a quaternion vs matrix mess.
But yea, just slerp your view matrix or view quarternion and you can turn that jump into a smooth gentle nudge.

[This message has been edited by DFrey (edited 04-27-2001).]