These Squares with the length ‘dist’ are
put next to eachother. The amount of
squares in x-Direction is ‘xlength’.
The amount in z-Direction is ‘zlength’.
Now the Terrain works perfectly so I
will not go into the creation code.
Here comes the Problem:
I made an Algorithm that calculates the
Height of the Terrain in any Point (x;z)
but it doesn’t seem to work perfectly.
Heres the code maybe you can give me a
better solution or correct mine:
Try this (i’ll describe it only for the X axis and assume you’re using a heightmap, rewriting it for getting the height from a mesh should be easy as long as you’re on a regular grid).
First, find out which square you’re on (in other words, in between which vertices of your mesh or pixels of your heightmap you are). For example, if you have a 512 pixel wide heightmap that stretch from 0 to say 100 in world coordinates (playerX is your player’s x position on that grid):
xP = playerX*(512.0/100.0);
then xp is your position on the map in pixels (i’ll go with the heightmap 'cause it’s easier to explain).
now, you can find the heightmap pixel left and right of your player position with:
StarNut, you make the problem too complex for nothing. We’ve an even much more complex built landscape built of diamonds which can be made of 8, 4 or 2 tris depending on LOD, but we absolutely don’t care about that on any heighttests and this works now since more than 2 years very fine, because the user anyway won’t see any difference in general, if you check the exact triangles or not, so I think you make yourself much more work than needed. So simply use something like this:
float height(float fx,float fz)
{
float x2=x-floor(fx),z2=z-floor(fz);
int x=(int) fx;
int z=(int) fz;
return (height[x,z](1-x2)+height[x+1,z]x2)(1-z2)+(height[x,z+1](1-x2)+height[x+1,z+1]*x2)*z2;
};
I think that should work. If you really want to test your triangles…hm…lemme see…something like (pseudocode)
float height(float fx,float fz)
{
float x2=x-floor(fx),z2=z-floor(fz);
int x=(int) fx;
int z=(int) fz;
cvector v1=vec(0,0,height[x,z]); //lower left
cvector v2=vec(0,1,height[x,z+1]); //upper left
cvector v3=vec(1,1,height[x+1,z+1]); //upper right
cvector v4=vec(1,0,height[x+1,z]); //lower right
cvector v5;
if((x2+z2)<1) //lower left tri?
{
v5=StretchVec(VSumm(v1,v2),0.5); //middlepoint between upper right and lower left fieldcorner
v4=VSumm(v2,StretchVec(VDiff(v5,v2),2)); //v4 now contains the 4th edge, if we had a flat quad using points v1,v2,v3 as first three points
return (v1*(1-x2)+v4x2)(1-z2)+(v2*(1-x2)+v3*x2)*z2;
}
else
{
…
};
VSumm = V+V2 //simple addition of all components
VDiff = V2-V //vector from V2 → V
StretchVec = V*f //multiplication of all components with 2nd parameter
I think this would work (in the case that a field had a width and depth of 1).
Michael
[This message has been edited by BlackJack (edited 12-31-2001).]