Jump to content
  • Advertisement

Archived

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

Primative Collision Detection...

This topic is 6540 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''ve still got a lot to learn as far as OpenGL goes.. I''ve been playing with GLUT the past few days, and I decided to jump into simple collision detection. Just for some practice, I decided to write a simple OpenGL based pong type game.. Can anyone throw me in the right direction for collision detection? I don''t even know where to start.

Share this post


Link to post
Share on other sites
Advertisement
Bounding sphere collision detection is the most universal. There are many others of course. Many easier, many harder. Just run a simple search and you''ll surely find something.

JoeMont001@aol.com

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''m not sure if this code is clean or not... can''t remember...
Wrote it the other day. It''s 3D, not 2D, but is easily modified to be 2D... and you can get a general idea of the method.

/* Player coords are self-explanatory, block 1 is one corner of the 3D box, and block 2 is a diagonally opposite corner (ie. Block1 = 0,0,0 whereas Block2 = 2,2,2). The function returns 0 if the player isn''t inside block, and 1 if player is. */

int checkblockcollision(int playerx, int playery, int playerz, int block1x, int block1y, int block1z, int block2x, int block2y, int block2z)
{
int temp=0;

if(block1x > block2x)
{
temp=block1x;
block1x=block2x;
block2x=temp;
}

if(block1y < block2y)
{
temp=block1y;
block1y=block2y;
block2y=temp;
}

if(block1z > block2z)
{
temp=block1z;
block1z=block2z;
block2z=temp;
}

if(playerx > block1x && playerx < block2x && playery < block1y && playery > block2y && playerz > block1z && playerz < block2z)
{
return 1; // player would be inside block!!
}
else
{
return 0; // player wouldn''t be inside block.
}

}

Hope that helps abit... basically you can say
if(funtion returns 0)
{
// we can move to that position
}

Of course... you''ll need to break up how far you move each time and calculate it in increments to avoid passing through objects when traveling at high velocities or through thin objects.
The bit at the start sorts out the corners btw.

Protozone

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''m not sure if this code is clean or not... can''t remember...
Wrote it the other day. It''s 3D, not 2D, but is easily modified to be 2D... and you can get a general idea of the method.

/* Player coords are self-explanatory, block 1 is one corner of the 3D box, and block 2 is a diagonally opposite corner (ie. Block1 = 0,0,0 whereas Block2 = 2,2,2). The function returns 0 if the player isn''t inside block, and 1 if player is. */

int checkblockcollision(int playerx, int playery, int playerz, int block1x, int block1y, int block1z, int block2x, int block2y, int block2z)
{
int temp=0;

if(block1x > block2x)
{
temp=block1x;
block1x=block2x;
block2x=temp;
}

if(block1y < block2y)
{
temp=block1y;
block1y=block2y;
block2y=temp;
}

if(block1z > block2z)
{
temp=block1z;
block1z=block2z;
block2z=temp;
}

if(playerx > block1x && playerx < block2x && playery < block1y && playery > block2y && playerz > block1z && playerz < block2z)
{
return 1; // player would be inside block!!
}
else
{
return 0; // player wouldn''t be inside block.
}

}

Hope that helps abit... basically you can say
if(funtion returns 0)
{
// we can move to that position
}

Of course... you''ll need to break up how far you move each time and calculate it in increments to avoid passing through objects when traveling at high velocities or through thin objects.
The bit at the start sorts out the corners btw.

Protozone

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sorry for the double post... My connection dropped and I thought it wouldn''t have gotten through

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!