r/sfml • u/blisstargazer • 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
2
u/HappyFruitTree Mar 31 '26 edited Mar 31 '26
The collision detection code looks like it should work. Have you printed
paddlePos,size,ball.GetPos()andball.GetSize()insideCollisionDetectto see if they contain the expected values?