🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Primative Collision Detection...

Started by
3 comments, last by Pentode 24 years ago
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.
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
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
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
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
Sorry for the double post... My connection dropped and I thought it wouldn''t have gotten through

This topic is closed to new replies.

Advertisement