• ### What is your GameDev Story?

#### Archived

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

# Primative Collision Detection...

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

## 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 on other sites
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 on other sites
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 on other sites
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 on other sites
Sorry for the double post... My connection dropped and I thought it wouldn''t have gotten through

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634112
• Total Posts
3015580
×