Why is my ball clipping through the paddle?


I’m a young C++ developer, and it’s my first time transitioning to real programs instead of boring terminal ones like hangman or calculators.

I tried making Pong. I made 2 player paddles, a net, and a moving ball.

However, I encountered an error when I wanted to add collision detection between the paddle and the ball. I tried making my own one, but I ended up looking up code and implementing it my own way.

Here’s the code for my collision detection:

void CheckPaddleAndBallCollision(Paddle& paddle, Ball& ball)
{
    if (SDL_IntersectRect(&paddle.getPaddleRect(), &ball.getBallRect(), nullptr))
    {
        ball.setVelocityX(-ball.getVelocityX());
    }
}

I thought reversing the velocity automatically makes the ball go the opposite direction when it touches the paddle, but it’s not consistently deflecting away.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *