Jump to content
  • Advertisement


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


Terrain getHeight() Function Not Returning Right Value

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

If you intended to correct an error in the post then please contact us.

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);
	for(int y=0;y < terrDepth+1;y++)  {
		for(int x=0;x < terrWidth+1;x++)  {


			if(v[x+y*(terrWidth+1)].y < 0.25f*maxHeight)
			if((v[x+y*(terrWidth+1)].y >= 0.25f*maxHeight) && (v[x+y*(terrWidth+1)].y < 0.50f*maxHeight))
			if((v[x+y*(terrWidth+1)].y >= 0.50f*maxHeight) && (v[x+y*(terrWidth+1)].y < 0.75f*maxHeight))
			if(v[x+y*(terrWidth+1)].y >= 0.75f*maxHeight)

			if(x < terrWidth && y < terrDepth) {
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;
	else  {
		float u=C-D;
		float v=B-D;
	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 this post

Link to post
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.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!