Ok.. so im working on pong and my collision sucks. Ive read a bunch of previous posts about pong collision and basic 2d bitmap collision. All the algorithms seem basically the same, use bounding boxes and check to see if a point in either box is in another, if so , then you have colliderated.
In pong this exact method doesnt work. The most noticeable and ugly error is if the ball contacts the top edge of your paddle, the ball follows down the paddle and then shoots off. So it seems like what id have to do is just test for collision with the outer edges of the paddle?
Heres my collision code as it is right now:
// bool Collision ( RECT &pRect1, RECT &pRect2 )
//
// Check to see if two rectangles are overlapping
bool CBall::Collision ( RECT &pRect1, RECT &pRect2 )
{
// Test if pRect1 top-left corner falls in pRect2
if ( pRect1.left > pRect2.left &&
pRect1.left < pRect2.right &&
pRect1.top > pRect2.top &&
pRect1.bottom < pRect2.bottom )
{
return true;
}
// Test if pRect2 top-left corner falls in pRect1
if ( pRect2.left > pRect1.left &&
pRect2.left < pRect1.right &&
pRect2.top > pRect1.top &&
pRect2.bottom < pRect1.bottom )
{
return true;
}
// Test if pRect1 bottom-right corner falls in pRect2
if ( pRect1.right > pRect2.right &&
pRect1.right < pRect2.left &&
pRect1.top > pRect2.top &&
pRect1.bottom < pRect2.bottom )
{
return true;
}
// Test if pRect2 bottom-right corner falls in pRect1
if ( pRect2.right > pRect1.right &&
pRect2.right < pRect1.left &&
pRect2.top > pRect1.top &&
pRect2.bottom < pRect1.bottom )
{
return true;
}
return false;
}
And the calling condition and function
RECT player = { GetPlayerOnSide( x )->GetX(), GetPlayerOnSide( x )->GetY(), GetPlayerOnSide( x )->GetWidth(), GetPlayerOnSide( x )->GetHeight() };
if ( Collision( ball, player ) )
{
mXInc = -mXInc;
x += mXInc;
}
Lemme know what you guys think!