I’ve tried for hours through blood sweat and tears and searched high and low for some tips on how to get terrain walking working correctly, but there’s still a problem in my code.
Ok, so first the background. I’m drawing a heightmap using triangle strips. One ‘quad’ made up of 2 triangles is oriented in the following way (it’s supposed to be square!) with point (0,0,0) at point (p1) :
p2........p4
\ |
|\ |
| \ |
| \ |
| \ |
| \ |
| \ |
| \ |
| \ |
| \|
p1........p3
With my heightmap stored as an array of bytes containing the height value at that point, I’m using the following code to determine the height by interpolating between the points. It’s a mixture of my own plus some stuff I saw on flipcode ages ago:
// pass in the heightmap array, plus exact x,z of player
static final double HeightAtPointTris (byte[] pHeightMap, double xPos, double zPos)
{
double retVal = 0.0;
int ixPos = (int)(xPos);
int izPos = (int)(zPos);
g_p1.x = (int) xPos;
g_p1.z = (int) zPos;
g_p2.x = (int) xPos+1;
g_p2.z = (int) zPos;
g_p3.x = (int) xPos;
g_p3.z = (int) zPos+1;
g_p4.x = (int) xPos+1;
g_p4.z = (int) zPos+1;
g_p1.y = Height (pHeightMap, (int)g_p1.x, (int)g_p1.z);
g_p2.y = Height (pHeightMap, (int)g_p2.x, (int)g_p2.z);
g_p3.y = Height (pHeightMap, (int)g_p3.x, (int)g_p3.z);
g_p4.y = Height (pHeightMap, (int)g_p4.x, (int)g_p4.z);
// 1 unit here is the length side of 1 quad.
int quadlength = 1;
double coordX = xPos - ixPos;
double coordZ = zPos - izPos;
double h1, h2, h3, yfactor, xfactor;
if (coordX + coordZ < quadlength)
{
// closest to p1 point (upper left triangle)
h1 = g_p1.y;
h2 = g_p3.y;
h3 = g_p2.y;
yfactor = coordZ * (h2 - h1);
}
else
{
// closest to p4 point (lower right triangle)
h1 = g_p3.y;
h2 = g_p2.y;
h3 = g_p4.y;
yfactor = coordZ * (h2 - h3);
}
double hleft = yfactor + h1;
double hright = yfactor + h3;
xfactor = coordX * (hright - hleft);
retVal = xfactor + hleft;
return retVal;
}
Now this seems to work for the most part. However if I refer you to an image of a 3x3 tile (18 triangle) terrain I’m drawing:
http://vault101.co.uk/screenshots/s051015.jpg
…if you imagine the point closest to the camera (just out of view at the bottom of the screen) as being the origin (0,0,0) and the eight tiles around the center one being points on a compass:
NW - N - NE
| | |
W - C - E
| | |
SW - S - SE
then when the character runs from the SW tile to the SE tile, he disappears below into the S tile only to reappear at the SE tile.
Also, when the character is running from the NE tile to the NW tile,
he ‘jumps’ in the air as he enters the N tile, and slowly descends until reaching the NW tile’s correct height.
Anyway, go play. Hope someone can make use of the code I’ve posted and suggestions on how to fix would be great! This is doing my head in !