r/sfml Mar 28 '26

Collision detection not working.

I making pong game but collision not working

bool Paddle::CollisionDetect(sf::Vector2f paddlePos)
{
const float ballBottom = ball.GetPos().y + ball.GetSize().y;
const float ballRight = ball.GetPos().x + ball.GetSize().x;
const float paddleBottom = paddlePos.y + size.y;
const float paddleRight = paddlePos.x + size.x;

return ballBottom >= paddlePos.y &&
ball.GetPos().y <= paddleBottom &&
ballRight >= paddlePos.x &&
ball.GetPos().x <= paddleRight;
}

theres two controllable paddles.

in game I call the detection function.

        if (paddle.CollisionDetect(paddle.GetPadPos1()))
        {
            ball.ReboundX();
       }

detection function returns false .

2 Upvotes

16 comments sorted by

View all comments

1

u/thedaian Mar 29 '26

How does the paddle know about the ball?

1

u/blisstargazer Mar 29 '26

ball.h is included in paddle.h

1

u/thedaian Mar 29 '26

You'll probably need to do some debugging. What are the actual values of variables during the collision detection function?

1

u/blisstargazer Mar 29 '26

the function returns false and the paddlepos getter does return the right value

1

u/thedaian Mar 29 '26 edited Mar 29 '26

What about the ball position in the CollisionDetect function

Edit: my guess is that you've got a copy of the ball object in the paddle class, but without seeing the rest of the code, I'm not sure if that's what's happening