Make Freelook and you see: It is not a bug. This is a feature!
hmpf. okay
double Interpolate (double fA, double fB, double fX)
{
return (fA * (1 - fX) + fB * fX);
}
double GetHeight (double fx, double fy)
{
if (fx < 0 || fx > WIDTH)
return 0.0; // ERROR!
if (fy < 0 || fy > HEIGHT)
return 0.0; // ERROR!
int x = (int) fx;
int y = (int) fy;
int x1 = x + 1;
int y1 = y + 1;
double dX = fx - (double) x;
double dY = fy - (double) y;
double fHeight1 = m_afHeightmap[x + y * WIDTH];
double fHeight2 = m_afHeightmap[x1 + y * WIDTH];
double fHeight3 = m_afHeightmap[x + y * WIDTH];
double fHeight4 = m_afHeightmap[x1 + y1 * WIDTH];
return Interpolate (Interpolate (fHeight1, fHeight2, dX),
Interpolate (fHeight3, fHeight4, dX),
dY);
}
This works like this:
Actual_Height_On_Actual_Camera_Position = Interpolated 1-, 2- 3, or 4- terrain-vertex-height in position of the camera.
If Camera_Y > Actual_Height_On_Actual_Camera_Position Then ... oh my god ... End If
Here is more information for you: http://www.gamedev.net/community/forums/topic.asp?topic_id=205690
Edit: I think its only fair for posting a optimized sample, too.
//------------------------------------------------------------
//
// example
// -------
//
// (x0, h0, y0) (x1, h1, y0)
// +---------------+
// | ./|
// | . / |
// | . / |dy
// | . / |
// | . / |
// | . +-----+ ^
// | . dx | (1-dy) |d_hy =(1-dy)(h1 - h3)
// +---------------+ v
// (x0, h2, y1) (x1, h3, y1)
//
// <--->
// d_hx = dx(h2-h3)
//
//
// h = h3 + hx + hy
//
//------------------------------------------------------------
double GetHeight (double fx, double fy)
{ if (fx < 0 || fx > (WIDTH - 1))
return 0.0; // ERROR!
if (fy < 0 || fy > (HEIGHT - 1))
return 0.0; // ERROR!
int x0 = (int) fx;
int y0 = (int) fy;
int x1 = x0 + 1;
int y1 = y0 + 1;
double dX = (double) x1 - fx;
double dY = fy - (double) y0;
double h0 = g_afHeightMap[x0 + y0 * WIDTH];
double h1 = g_afHeightMap[x1 + y0 * WIDTH];
double h2 = g_afHeightMap[x0 + y1 * WIDTH];
double h3 = g_afHeightMap[x1 + y1 * WIDTH];
double h;
// top left triangle
// interpolate with origin at (x0), and going towards
// x1 and x2
if (dx > dy)
{
h = h0 + (1 - dx) * (h1 - h0) + dy * (h2 - h0);
}
// bottom right triangle
// interpolate with origin at (x3), and going towards
// x2 and x1
else
{
h = h3 + dx * (h3 - h2) + (1 - dy) * (h1 - h3);
}
return h;
}
|