#### Archived

This topic is now archived and is closed to further replies.

# Terrain getHeight() Function Not Returning Right Value

This topic is 5115 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I made a 64x64 quad terrain out of a .raw file, and I''m now trying to get my camera "on" the terrain by placing it at the terrains y value, the function taking in the x and z value and computing the y value depending on the surrounding vertices'' heights. Here is the code for the creation of the terrain:
void map::createMap(IDirect3DDevice9* device)  {
ifstream fin("land.raw",ios::binary);
vertex* v;
short* i;
device->CreateVertexBuffer(sizeof (vertex)*vertices,D3DUSAGE_WRITEONLY,vertex::FVF,D3DPOOL_MANAGED,&vb,NULL);
device->CreateIndexBuffer(sizeof (short)*primitives*3,D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&ib,NULL);
vb->Lock(0,0,(void**)&v,0);
ib->Lock(0,0,(void**)&i,0);
D3DCOLOR c;
for(int y=0;y < terrDepth+1;y++)  {
for(int x=0;x < terrWidth+1;x++)  {
fin.get(hmp[x+y*(terrWidth+1)]);

v[x+y*(terrWidth+1)].x=float(x)/(25.5/maxHeight);
v[x+y*(terrWidth+1)].y=float(hmp[x+y*(terrWidth+1)])/10/(25.5f/maxHeight);
v[x+y*(terrWidth+1)].z=float(y)/(25.5/maxHeight);

if(v[x+y*(terrWidth+1)].y < 0.25f*maxHeight)
c=D3DCOLOR_XRGB(100,40,0);
if((v[x+y*(terrWidth+1)].y >= 0.25f*maxHeight) && (v[x+y*(terrWidth+1)].y < 0.50f*maxHeight))
c=D3DCOLOR_XRGB(0,40,0);
if((v[x+y*(terrWidth+1)].y >= 0.50f*maxHeight) && (v[x+y*(terrWidth+1)].y < 0.75f*maxHeight))
c=D3DCOLOR_XRGB(100,100,100);
if(v[x+y*(terrWidth+1)].y >= 0.75f*maxHeight)
c=D3DCOLOR_XRGB(255,255,255);

v[x+y*(terrWidth+1)].color=c;
if(x < terrWidth && y < terrDepth) {
*i++=x+1+(y+1)*(terrWidth+1);
*i++=x+1+y*(terrWidth+1);
*i++=x+y*(terrWidth+1);
*i++=x+(y+1)*(terrWidth+1);
*i++=x+1+(y+1)*(terrWidth+1);
*i++=x+y*(terrWidth+1);
}
}
}
vb->Unlock();
ib->Unlock();
}

And here is the code for the getHeight() function:
float map::getHeight(float x,float z)  {
float height=0;
int cellX=floor(x);
int cellZ=floor(z);
float A=hmp[cellX+cellZ*(terrWidth+1)];
float B=hmp[cellX+1+cellZ*(terrWidth+1)];
float C=hmp[cellX+(cellZ+1)*(terrWidth+1)];
float D=hmp[cellX+1+(cellZ+1)*(terrWidth+1)];
float orgnX=x-cellX;
float orgnZ=z-cellZ;
if(orgnZ < 1.0f-orgnX)  {
float u=B-A;
float v=C-A;
height=A+LERP(0.0f,u,orgnX)+LERP(0.0f,v,orgnZ);
}
else  {
float u=C-D;
float v=B-D;
height=D+LERP(0.0f,u,1.0f-orgnX)+LERP(0.0f,v,1.0f-orgnZ);
}
return (height)/10.0f/(25.5f/maxHeight);
}

And here is the LERP() function that I "borrowed" from a book:
float LERP(float a,float b,float t)  {
return a-(a*t)+(b*t);
}

Everything works fine when maxHeight == 25.5f, but when I try 2.55f, it renders the terrain correctly, colors it correctly, but doesn''t return the correct height in the getHeight() function. This baffles me to no countable extent. The formula dividing the variable height is the same formula dividing the value in the hmp array when creating the vertex buffer, so I do not understand why this return value should not be correctly defined. I have been looking at this problem for days and I''m hoping new blood to the issue might help. Thanks, Joshua Merchant

##### Share on other sites
There are a few areas where you use 25.5f, you change all these to 2.55f where applicable, right?

##### Share on other sites
About the 25.5 in the "/10.0f/(25.5f/maxHeight)", that''s part of the formula, and it always comes out to be dividing by a power of ten, scaling the x, y, and z values down by that number. For example: if maxheight == 25.5, it would scale the heightmap down by 10. If maxheight is 2.55, it would scale the heightmap down by 10. In the formula that resides in the createMap() function, everything SEEMS to be working. Again, I say that the problem seems to be in the getHeight() function, as excluding it, everything else is fine.

• ### Forum Statistics

• Total Topics
628714
• Total Posts
2984353

• 23
• 11
• 10
• 13
• 14