# Simple OpenGL Collision Detection: For Newbs

This topic is 4735 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.

##### Share on other sites
Quote:
 Original post by fprotono 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 theabsolute value. since your values are always 1. if your cubescollide, 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.

Gonna have to claim ignorance on that one..

Maybe I should study more before diving into collision detection ey?

##### Share on other sites
Picture intersecting cubes which intersect in a minimal way, i.e. the centres are as far apart as possible while the cubes still intersect. In the limit, these cubes are just touching at the corner. Thus the distance between the centres of the cubes is one unit in each direction: .5 units to cover the distance 'out' from the centre of one cube to its side (corner, once you account for all three dimensions), and another .5 to cover the distance 'in' from there to the centre of the other cube.

##### Share on other sites
I usually just do spherical collision detection(elegant name don't know the real one). Basicaly, you know the radius of the sphere that surrounds you object, then you check and see if the distance of the two objects is less than the two radiis added together. Don't use square root, instead square the radiis to compare(it is cheaper). This method is good for any type of model except long skinny ones or something like that. The cubes method shown above also works, but in some cases isn't as accurate as the sphere, then some times it is more accurate depending on the shape of the object. In the above case, the objects ARE the cubes so definately use the above method in this case.

##### Share on other sites
Quote:
 Original post by ZahlmanPicture intersecting cubes which intersect in a minimal way, i.e. the centres are as far apart as possible while the cubes still intersect. In the limit, these cubes are just touching at the corner. Thus the distance between the centres of the cubes is one unit in each direction: .5 units to cover the distance 'out' from the centre of one cube to its side (corner, once you account for all three dimensions), and another .5 to cover the distance 'in' from there to the centre of the other cube.

Wow... that makes so much more sense now! Thank you so very much!!!

The sphere detection is awesome as well.. thank you all very much!