Public Group

# Simple OpenGL Collision Detection: For Newbs

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

## Recommended Posts

I've such a massive ego it hurts to call myself a newb, but that I am. Was just wondering if someone could point me to a simple method for collision detection in OpenGl. And by simple I mean: I have two cubes each 1x1x1 that do not rotate and want to detect when they collide. Yea... and stuff.. A tutorial on the subject would be swell.

##### Share on other sites
I would go to the collision detection section of the articles page and read 'When Two Hearts Collide: Axis-aligned Bounding Boxes', and 'Simple Intersection Tests for Games.'

As simple as it is, the actual intersection test for two AABBs may not show up in the articles. So just in case, here it is:

bool IntersectAABBs(const Vector3& min1, const Vector3& max1,    const Vector3& min2, const Vector3& max2){    if (min1[0] > max2[0])        return false;    if (min1[1] > max2[1])        return false;    if (min1[2] > max2[2])        return false;    if (max1[0] < min2[0])        return false;    if (max1[1] < min2[1])        return false;    if (max1[2] < min2[2])        return false;	    return true;}

That should be enough to get you started :-)

##### Share on other sites
well, assuming you have the positions of your cubes...
and your center of gravity is actually at the center of the cubes...
it's as simple as:

if((x1-x2)*(x1-x2) > 1 ||   (y1-y2)*(y1-y2) > 1 ||   (z1-z2)*(z1-z2) > 1 ) return FALSE;else return TRUE;

##### Share on other sites

So pretty much it's just comparing two vectors? Cool thanks!

##### Share on other sites
Quote:
 Original post by fprotowell, assuming you have the positions of your cubes... and your center of gravity is actually at the center of the cubes...it's as simple as:if((x1-x2)*(x1-x2) > 1 || (y1-y2)*(y1-y2) > 1 || (z1-z2)*(z1-z2) > 1 ) return FALSE;else return TRUE;

I think i get that, but explain what you mean by center of gravity in these terms?

##### Share on other sites
0.5, or -0.5.
it means the center of your cube. :)

##### Share on other sites
Never confuse the difference between your graphics API and collision code. There is no collision detection in OpenGL.

##### Share on other sites
Quote:
 Original post by Anonymous PosterNever confuse the difference between your graphics API and collision code. There is no collision detection in OpenGL.

Well yes... now that I think of it.. that's quite obvious.. Thanks =)

##### Share on other sites
Quote:
 Original post by fprotothat means all your vertices in your cube are either0.5, or -0.5. it means the center of your cube. :)

okok, well this is good and all but the problem is, with 1x1x1 cubes it really sets up for the cubes only to collide when the centers touch..

. ___
--|- |
| | | |
| ~~~
~~~~

doesn't collide with that (as i see it)

correct me if i'm wrong.. i'm horrible with this and just starting out =)

##### Share on other sites
no you completely misunderstood lol.
i'm taking the square of the delta x, delta y, and delta z.
it's just to simplify the code a bit. it's a cheap way to get the
collide, the delta x, delta y, AND the delta z must be <= 1.
the centers must be at most 1 unit off for them to collide.

• ### What is your GameDev Story?

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

• 21
• 15
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634097
• Total Posts
3015514
×