# Collision detection in Pong clone

Well I'm trying to create a pong clone using SDL and I had some problems with the frames and some times the ball would go through the paddles despite the fact that at other times the collision worked perfectly. So I searched it a bit and I came to the conclusion that it was because in one frame the ball was already through the paddle and in another frame the ball was not or something like this. So thought that if I would manually adjust the position of the ball, like move it a couple of pixels away the moment it hits a paddle or the walls too, this would solve the problem. But now when the ball goes right past a paddle its movement is getting weird and it goes right in the playing field. for reference also some code if you want to look it . I feel bad for doing the whole collision like these but I couldn't think some other way. Please enlighten me if there are better ways to do it that I can avoid the frame problem I mentioned above. Here is the code that handles the ball's movement :

   void Ball::move(float xLeftPaddle, float yLeftPaddle,float xRightPaddle, float yRightPaddle, Uint32 deltaTicks)
{

y +=  yVelocity * ( deltaTicks / 1000.f );
x +=  xVelocity * ( deltaTicks / 1000.f );

if(   y + BALL_HEIGHT > SCREEN_HEIGHT )
{
x = x-1;
y = y-1;
yVelocity = -yVelocity;

}
else if( y < 0 )
{
x = x+1;
y = y+1;
yVelocity = -yVelocity ;
}
{
x = x-1;
y = y-1;
xVelocity = -xVelocity;

}
{
x = x+1;
y = y+1;

xVelocity = -xVelocity ;
}
else if(x < 0 || x > SCREEN_WIDTH)
{
x = SCREEN_WIDTH/2;
y = SCREEN_HEIGHT/2;
}


}

• You don't check if the ball has past the paddle. This allows you to "hit" the ball when it's already gone past the paddle. – MichaelHouse May 11 '14 at 17:17
• Can you give me any pointers on how to fix this ? Should I add two more if conditions for each paddle to check this ? – user3385993 May 11 '14 at 17:20
• You want to check if the position you're moving to next is inside the bounds of the paddle. Similar to how you ensured the ball was between the top and bottom of the paddle, you need to check to ensure it's between the front and back too. – MichaelHouse May 11 '14 at 17:22