pong collision detection
I am having trouble with pong collision detection this is what i got but know it isnt right
bG_deltaX = -5;
bG_deltaY = -5;
if(Collision(rect,rect2))
{
//MessageBox(NULL,"TEST","COLLISION",MB_OK);
if(top)
{
MessageBox (NULL,"TEST","COLLISION",MB_OK);
bG_deltaY *= -1;
//bG_deltaX *=-1;
top = false;
}
else
bG_deltaX *=-1;
}//end if
int Collision(RECT Rect1,RECT Rect2)
{
if((Rect1.right >= Rect2.left) && (Rect1.top <= Rect2.bottom) && (Rect1.bottom >= Rect2.top) && (Rect1.left <= Rect2.right))
{
if(Rect2.left < Rect1.right && Rect2.bottom > Rect1.top)
{
top = true;
}
return 1;
}
else
return 0;
}
If I'm reading your code correctly, the problem is occurring in your collision function. In your first if statement, you're checking to see if the boundaries of your pong ball are colliding with the paddle. Problem is, the function will always return false unless the entire ball is inside the paddle (since you used && instead of || ). If any of those are true, there will be collision, so start by changing the "and" to "or", and see if that changes things.
Also, beyond just checking boundaries, try checking the corners of your pong ball. If any corner of your pong ball is inside the paddle's rectangle, collision has occurred, and that will eliminate the need for that second "if" statement.
While you're not using Allegro as your game library, this explains corner collision quite well and how to write your code to try it.
Hope that helps!
Also, beyond just checking boundaries, try checking the corners of your pong ball. If any corner of your pong ball is inside the paddle's rectangle, collision has occurred, and that will eliminate the need for that second "if" statement.
While you're not using Allegro as your game library, this explains corner collision quite well and how to write your code to try it.
Hope that helps!
Quote:Original post by dk32321
I am having trouble with pong collision detection this is what i got but know it isnt right
How do you know it isn't right? Does it not compile? Not run? Not run correctly? If strange things happen, what are these things? The more you tell us, the better our chances are of helping you out.
Quote:code snip
When posting code like this, it's handy (and encouraged) to put the code inside of [source] and [/source] tags, to format it nicely:
bG_deltaX = -5;bG_deltaY = -5;if(Collision(rect,rect2)){ //MessageBox(NULL,"TEST","COLLISION",MB_OK); if(top) { MessageBox (NULL,"TEST","COLLISION",MB_OK); bG_deltaY *= -1; //bG_deltaX *=-1; top = false; } else bG_deltaX *=-1; }//end ifint Collision(RECT Rect1,RECT Rect2){ if((Rect1.right >= Rect2.left) && (Rect1.top <= Rect2.bottom) && (Rect1.bottom >= Rect2.top) && (Rect1.left <= Rect2.right)) { if(Rect2.left < Rect1.right && Rect2.bottom > Rect1.top) { top = true; } return 1; } else return 0;}
Now, as to why it's not working, I have a couple suggestions/questions:
1) What's this top variable about? It's pretty tricky business modifying variables like this that don't seem to have anything related. Just by looking at your if (Collision(...)) code, there's no way to tell that top is modified. If it's there to indicate what type of collision occurred, that should be taken care of with your return value, since its an int. Later, you can try making it an enumeration, which is much clearer (better).
2) Your giant if statement in Collision(...) doesn't check for what you want it to, I don't think. Assuming that a rectangles "top" is always a greater value than it's "bottom," when is the following ever going to be true:
(Rect1.top <= Rect2.bottom) && (Rect1.bottom >= Rect2.top)
(Hint: it's not!)This should get you rolling, but post back with any more questions, or if I can make something more clear.
-jouley
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement