Jump to content
  • Advertisement
Sign in to follow this  
dellatorre

Need help Detecting Collision

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

Share this post


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

Share this post


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

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!