Jump to content
  • Advertisement
Sign in to follow this  
brandonman

Heightmap Collision

This topic is 4005 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 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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!