Sign in to follow this  
dellatorre

Need help Detecting Collision

Recommended Posts

dellatorre    100
I have a moving cube and a fixed cube. I'm trying to figure out how to detect when my moving cube hits the fixed cube. here is my moving cube: //red car //back glColor3f( 1, 0, 0 ); glBegin( GL_POLYGON ); glVertex3f( -1,3,-1 ); glVertex3f( 1,3,-1 ); glVertex3f( 1,5,-1 ); glVertex3f( -1,5,-1 ); glEnd(); //bottom glColor3f( 1, 0, 0 ); glBegin( GL_POLYGON ); glVertex3f( -1,3,1 ); glVertex3f( 1,3,1 ); glVertex3f( 1,3,-1 ); glVertex3f( -1,3,-1 ); glEnd(); //right glColor3f( 1, 0, 0 ); glBegin( GL_POLYGON ); glVertex3f( 1,3,-1 ); glVertex3f( 1,3,1 ); glVertex3f( 1,5,1 ); glVertex3f( 1,5,-1 ); glEnd(); //left glColor3f( 1, 0, 0 ); glBegin( GL_POLYGON ); glVertex3f( -1,3,1 ); glVertex3f( -1,3,-1 ); glVertex3f( -1,5,-1 ); glVertex3f( -1,5,1 ); glEnd(); //top glColor3f( 1, 0, 0 ); glBegin( GL_POLYGON ); glVertex3f( -1,3,-1 ); glVertex3f( 1,3,-1 ); glVertex3f( -1,3,-1 ); glVertex3f( -1,3,1 ); glEnd(); //front glColor3f( 1, 0, 0 ); glBegin( GL_POLYGON ); glVertex3f( 1,3,1 ); glVertex3f( -1,3,1 ); glVertex3f( -1,5,1 ); glVertex3f( 1,5,1 ); glEnd(); glEnable( GL_TEXTURE_2D); and my fixed cube: glPushMatrix(); glDisable(GL_TEXTURE_2D); glTranslatef(-30.0, -50.0, -20.0); glutSolidCube(3.0); glEnable(GL_TEXTURE_2D); glPopMatrix(); can anyone help me with this? thanks

Share this post


Link to post
Share on other sites
jyk    2094
In your example, you show how you're rendering the moving object, but you don't show how you're moving it, so it's a little difficult to offer a specific answer to your question.

However, the general idea is pretty straightforward. The first thing to realize is that (aside from GPU-based algorithms, which probably aren't particularly relevant here) collision detection and OpenGL have very little to do with each other. In other words, the code you posted is more or less irrelevant to the question at hand.

What you probably want here is a simple AABB-vs.-AABB intersection test, which is super-simple to implement and is well documented online and elsewhere. The AABB corresponding to the non-moving object can remain static (obviously), but the parameters (min and max points or a center-extents pair) of the moving AABB will need to be updated on a per-frame basis. Then, you simply run an AABB intersection test each frame and respond accordingly if an intersection is detected.

If you need more detailed help, you might post some more of your code (be sure to use [ source ] tags when doing so). Also, if this is a homework assignment, keep in mind that we can't give you answers per se, only hints and suggestions.

Share this post


Link to post
Share on other sites
Deliverance    387
First of all it may be a good idea to represent the cubes using mathematical methods. You can represent a cube using a center and size. Now the collision detection algorithm becomes:



if (fabs(center1.x-center2.x)<=size && fabs(center1.y-center2.y)<=size && fabs(center1.z-center2.z)<=size)

return true;

else

return false;


Share this post


Link to post
Share on other sites
jyk    2094
Quote:
You should probably put an .x .y and .z after "size", too.
Deliverance and the OP both specified that the shapes are cubes, so the code is correct as is. (However, if the shapes were general axis-aligned boxes, you would need a separate size/extent value for each dimension, as you suggest.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this