Collision Detection Problem

This is a discussion on Collision Detection Problem within the Game Programming forums, part of the General Programming Boards category; I am trying to program a pong game, and I am using openGL and Glut for the graphics.
In my ...

Collision Detection Problem

I am trying to program a pong game, and I am using openGL and Glut for the graphics.

In my main loop I draw the sprites and then test to see if they have collided, if so then I alter the velocites, either way I add to the coords of the ball and then redraw (to move the ball). Now, when I comment out the collide condition the ball moves across the screen fine, except it goes through the paddle. When I uncomment out the collide test, however, the ball just stays in the center, yet I can still move the paddles so the redraw portion is being exectuted properly. It seems as though the code after the test is not being executed. I am also lead to believe there is a problem with my collide function. Everything in the game is rectangluar even the ball (for now) and are initialize in a struct fyi. Here are my functions:

It sounds like the ball always thinks it's colliding, and it looks that way to with your code. Your conditions don't look right. You only have a collision if all those if's succeed for the given objects.

I also find it odd that you have x1, x2, y1, and y2 which are the corners of the objects, but still add the width or offset for your checks.

As is, the if statement is always true, unless the top of object2 is below its bottom (not necessarily a nonsensical statement if the collision box rotates).

Hope that helps.

EDIT: Also, use more parentheses to make reading your code easier. For example, in this line

Code:

if (Collide(&ball,&Paddle1)){vx=-vx;vy=-vy;}

It's hard to tell if you mean "assign value of inverted (hope that's right) vx to vx", or if you meant to say "calculate value of vx minus vx, then assign the result to vx" and got the '-' and '=' the wrong way round.

Last edited by ruthgrin; 06-08-2006 at 12:06 AM.
Reason: Looked over the code again

After yet another look through, I realized that there was at least one more error where you assign the values for the corners. Perhaps you could try assigning the corner locations inside each box (which reminds me, are you programming this in C or C++?) and update them as the box's location changes.

Also, in the comparison statements, I don't think the tests will yield the desired results, though it took me a while to realize what skorman00 was saying. Basically, it is quite possible for the top of box 1 to be higher than the bottom of box 2 without a collision taking place (same for all the other individual conditions). In order to make sure the boxes are meeting, you have to make sure that they occupy the same space by narrowing down the area of potential intersection.

For example, if box 1's top is within the range defined by box 2's top and box 2's bottom, then you know the boxes are in the valid y-axis range for a collision, but not necessarily on the x-axis, so you test again, this time with, say, box 1's right, box 2's left and box 2's right. If this test also returns true, then the boxes are intersecting (try working it out with your hands, possibly some other props). This calc could be worth considering:

I'm unable to test this with my compiler right now, but hopefully there aren't any errors worse than a missing parentheses or curly brace. If none of this makes sense please show the structure of your collision box, as I may be making incorrect assumptions about its contents.