Need help Detecting Collision

Started by
5 comments, last by Narf the Mouse 14 years, 2 months ago
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
Advertisement
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.
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;elsereturn false;
You should probably put an .x .y and .z after "size", too.
Quote:Original post by Narf the Mouse
You should probably put an .x .y and .z after "size", too.


Well, we are dealing with cubes, so same size :D.
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.)
True. I guess I'm not awake yet.

But, on a relevant note, there's no real reason to have a separate AABB class for cubes and boxes.

This topic is closed to new replies.

Advertisement