terrain height math

This commit is contained in:
UbitUmarov
2022-08-20 12:53:38 +01:00
parent 6be01a8815
commit 3a201f7e3d
2 changed files with 91 additions and 87 deletions

View File

@@ -5642,44 +5642,65 @@ Environment.Exit(1);
// does a linear approximation of the height at this intermediate point.
public float GetGroundHeight(float x, float y)
{
int ix;
int iy;
float dx;
float dy;
// make position fit into array
if (x < 0)
x = 0;
if (x >= Heightmap.Width)
x = Heightmap.Width - 1;
{
ix = 0;
dx = 0;
}
else if (x < Heightmap.Width - 1)
{
ix = (int)x;
dx = x - ix;
}
else // out world use external height
{
ix = Heightmap.Width - 2;
dx = 0;
}
if (y < 0)
y = 0;
if (y >= Heightmap.Height)
y = Heightmap.Height - 1;
{
iy = 0;
dy = 0;
}
else if (y < Heightmap.Height - 1)
{
iy = (int)y;
dy = y - iy;
}
else
{
iy = Heightmap.Height - 2;
dy = 0;
}
Vector3 p0 = new Vector3(x, y, (float)Heightmap[(int)x, (int)y]);
Vector3 p1 = p0;
Vector3 p2 = p0;
float h1;
float h2;
float h0 = Heightmap[ix, iy]; // 0,0 vertice
p1.X += 1.0f;
if (p1.X < Heightmap.Width)
p1.Z = (float)Heightmap[(int)p1.X, (int)p1.Y];
p2.Y += 1.0f;
if (p2.Y < Heightmap.Height)
p2.Z = (float)Heightmap[(int)p2.X, (int)p2.Y];
Vector3 v0 = new Vector3(p1.X - p0.X, p1.Y - p0.Y, p1.Z - p0.Z);
Vector3 v1 = new Vector3(p2.X - p0.X, p2.Y - p0.Y, p2.Z - p0.Z);
v0.Normalize();
v1.Normalize();
Vector3 vsn = new Vector3(
(v0.Y * v1.Z) - (v0.Z * v1.Y),
(v0.Z * v1.X) - (v0.X * v1.Z),
(v0.X * v1.Y) - (v0.Y * v1.X)
);
vsn.Normalize();
float xdiff = x - (float)((int)x);
float ydiff = y - (float)((int)y);
return (((vsn.X * xdiff) + (vsn.Y * ydiff)) / (-1 * vsn.Z)) + p0.Z;
if (dy > dx)
{
++iy;
h2 = Heightmap[ix, iy]; // 0,1 vertice
h1 = (h2 - h0) * dy; // 0,1 vertice minus 0,0
++ix;
h2 = (Heightmap[ix, iy] - h2) * dx; // 1,1 vertice minus 0,1
}
else
{
++ix;
h2 = Heightmap[ix, iy]; // vertice 1,0
h1 = (h2 - h0) * dx; // 1,0 vertice minus 0,0
++iy;
h2 = (Heightmap[ix, iy] - h2) * dy; // 1,1 vertice minus 1,0
}
return h0 + h1 + h2;
}
private void CheckHeartbeat()