Heightmap Collision

Started by
13 comments, last by brandonman 16 years, 6 months ago
I changed this from being about heightmaps to terrain Collision. I used the nehe heightmap stuff, now I need to figure out terrain collision. I tried using this in the draw function for the terrain:

			if(objCamera.mPos.x<x+16)
			{
                if(objCamera.mPos.x>x)
                {
                   if(objCamera.mPos.z<z+16)
                   {
                      if(objCamera.mPos.z>z)
                      {
                         objCamera.mPos.y=Height(pHeightMap, X + STEP_SIZE, Y );
                         }
                         }
                         }
                         }

at the end of the function, but it didn't even detect a collision. i originally had it right before glEnd(); but that got really weird results. It had me way up above the terrain. Can someone give a quick snippet for terrain collision, or a link with theory or code for it, or even better, both? [Edited by - brandonman on September 29, 2007 12:21:44 PM]
Advertisement
bumping to top with my new question.
I tried doing some searching but still can't find anything. Hate to bump again, but I can't seem to get this working.
Why in the draw function? How about the step function?
if(objCamera.mPos.x<x+16)			{                if(objCamera.mPos.x>x)                {                   if(objCamera.mPos.z<z+16)                   {                      if(objCamera.mPos.z>z)                      {                         objCamera.mPos.y=Height(pHeightMap, X + STEP_SIZE, Y );                         }                         }                         }                         }


That looks very messy and doesn't seem like it'll work. How about:
if(objCamera.mPos.x<x+16&&objCamera.mPos.x>x&&objCamera.mPos.z<z+16&&objCamera.mPos.z>z)                      {                         objCamera.mPos.y=Height(pHeightMap, X + STEP_SIZE, Y );                      }                     

I'm assuming 0 and 16 are the width and depth of your height map/terrain. Also the '&&' is an operator, in small terms which means, 'and'.
Hope that helps at all.
Holy crap, you can read!
Thanks for replying, but it didn't do the trick. and, there is no step function...

here's the whole render function:
void RenderHeightMap(BYTE pHeightMap[])				// This Renders The Height Map As Quads{	int X = 0, Y = 0;					// Create Some Variables To Walk The Array With.	int x, y, z;						// Create Some Variables For Readability	if(!pHeightMap) return;					// Make Sure Our Height Data Is Valid		glBegin( GL_QUADS );	for ( X = 0; X < (MAP_SIZE-STEP_SIZE); X += STEP_SIZE )		for ( Y = 0; Y < (MAP_SIZE-STEP_SIZE); Y += STEP_SIZE )		{			// Get The (X, Y, Z) Value For The Bottom Left Vertex			x = X;										y = Height(pHeightMap, X, Y );				z = Y;										// Set The Color Value Of The Current Vertex			if(y>220)			{			glColor3f(1.0f,1.0f,1.0f);        }            if(y<221 && y>190)            {            glColor3f(0.4f,0.15f,0.12f);            }            if(y<191 && y>150)            {               glColor3f(0.1f,0.8f,0.1f);               }               if(y<50)               {            glColor3f(0.4f,0.15f,0.12f);               }            y-=100;               			glVertex3i(x, y, z);			// Send This Vertex To OpenGL To Be Rendered			// Get The (X, Y, Z) Value For The Top Left Vertex			x = X;													y = Height(pHeightMap, X, Y + STEP_SIZE );  			z = Y + STEP_SIZE ;													// Set The Color Value Of The Current Vertex			if(y>220)			{			glColor3f(1.0f,1.0f,1.0f);        }            if(y<221 && y>190)            {            glColor3f(0.4f,0.15f,0.12f);            }            if(y<191 && y>150)            {               glColor3f(0.1f,0.8f,0.1f);               }               if(y<50)               {            glColor3f(0.4f,0.15f,0.12f);               }                          y-=100;                        			glVertex3i(x, y, z);			// Send This Vertex To OpenGL To Be Rendered			// Get The (X, Y, Z) Value For The Top Right Vertex			x = X + STEP_SIZE; 			y = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE ); 			z = Y + STEP_SIZE ;			// Set The Color Value Of The Current Vertex			if(y>220)			{			glColor3f(1.0f,1.0f,1.0f);        }            if(y<221 && y>190)            {            glColor3f(0.4f,0.15f,0.12f);            }            if(y<191 && y>150)            {               glColor3f(0.1f,0.8f,0.1f);               }               if(y<50)               {            glColor3f(0.4f,0.15f,0.12f);               }               			y-=100;                          			glVertex3i(x, y, z);			// Send This Vertex To OpenGL To Be Rendered			// Get The (X, Y, Z) Value For The Bottom Right Vertex			x = X + STEP_SIZE; 			y = Height(pHeightMap, X + STEP_SIZE, Y ); 			z = Y;			// Set The Color Value Of The Current Vertex			if(y>220)			{			glColor3f(1.0f,1.0f,1.0f);        }            if(y<221 && y>190)            {            glColor3f(0.4f,0.15f,0.12f);            }            if(y<191 && y>150)            {               glColor3f(0.1f,0.8f,0.1f);               }               if(y<50)               {            glColor3f(0.4f,0.15f,0.12f);               }                           y-=100;			glVertex3i(x, y, z);			// Send This Vertex To OpenGL To Be Rendered		}if(objCamera.mPos.x<x+1&&objCamera.mPos.x>x&&objCamera.mPos.z<z+1&&objCamera.mPos.z>z)                      {                         objCamera.mPos.y=Height(pHeightMap, X + STEP_SIZE, Y );                      }			glEnd();	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);			// Reset The Color	}	


[Edited by - brandonman on September 30, 2007 2:59:33 PM]
What are you trying to get at exactly? How wide and long is your terrain? What results are you getting? Are you trying to make the camera stop if it goes to far or what?
Holy crap, you can read!
trying to not walk through mountains:P
So you mean, walking over the height map, right?
Then you should put down anywhere, easily: objCamera.mPos.y=Height(pHeightMap, X + STEP_SIZE, Y );
That should do it. If not, post the results as in, you don't go up the height map at all, or you didn't actually walk over it, but you kinda did... stuff like that.
Holy crap, you can read!
now my camera ends up at the height of the tallest peak.

Thanks for all the help so far, btw. rating++.
So, my guess this is not what you want?
[looks over code] Do you have a GetHeight Function? You need to call the height from the X and Z position to find the Y, which will be your height.
Holy crap, you can read!

This topic is closed to new replies.

Advertisement