Jump to content
  • Advertisement
Sign in to follow this  
tobiasknight

Simple OpenGL Collision Detection: For Newbs

This topic is 4916 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 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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by fproto
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;


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

Share this post


Link to post
Share on other sites
that means all your vertices in your cube are either
0.5, or -0.5.
it means the center of your cube. :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Never confuse the difference between your graphics API and collision code. There is no collision detection in OpenGL.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Never 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 this post


Link to post
Share on other sites
Quote:
Original post by fproto
that means all your vertices in your cube are either
0.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 this post


Link to post
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
absolute value. since your values are always 1. if your cubes
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!