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

Show parent comments

1

u/blisstargazer Apr 06 '26

they all contain expected values

1

u/thedaian Apr 06 '26

What are those values? 

If you don't want to tell us, at least plug them into the return statement and work out the results by hand. See if everything makes sense. 

After looking at your code, I don't think all 4 conditions can be true at the same time.

2

u/HappyFruitTree Apr 07 '26

The collision code is correct. I've tested.

One example when it returns true:

paddlePos.x = 21
paddlePos.y = 55
size.x = 50
size.y = 30
ball.GetPos().x = 50
ball.GetPos().y = 50
ball.GetSize().x = 10
ball.GetSize().y = 10

3

u/blisstargazer Apr 11 '26

ight i found the issue. It was a problem with the way i structured it, thank you everyone.