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 ;
}
else if(x + BALL_WIDTH >= xRightPaddle && y <= yRightPaddle + PADDLE_HEIGHT && y >=yRightPaddle )
{
x = x-1;
y = y-1;
xVelocity = -xVelocity;
}
else if( x <= xLeftPaddle + PADDLE_WIDTH && y <= yLeftPaddle + PADDLE_HEIGHT && y >= yLeftPaddle)
{
x = x+1;
y = y+1;
xVelocity = -xVelocity ;
}
else if(x < 0 || x > SCREEN_WIDTH)
{
x = SCREEN_WIDTH/2;
y = SCREEN_HEIGHT/2;
}
}