SFML community forums

Help => Graphics => Topic started by: n3wt on October 14, 2022, 08:42:44 pm

Title: Check collisions on certain part sf::RectangleShape
Post by: n3wt on October 14, 2022, 08:42:44 pm
Hey all,
I'm currently making an Arkanoid clone and I would like to change the direction of the ball depending on where the ball has touched the paddle. Previously, had it working with getGlobalBounds and intersects below:

if (this->ball.getGlobalBounds().intersects(this->player))
{
        // Do stuff
}

This is good and gets the basic bounce working but I would like to take it a step further. I would like to know where exactly the ball has intersected the paddle so I can do some different stuff with ball speed and direction.

My question is: How can I check where exactly a collision happened on a sf::RectangleShape?

Thanks!
Title: Re: Check collisions on certain part sf::RectangleShape
Post by: G. on October 15, 2022, 04:25:04 pm
Hey.

Not necessarily an exact answer to your question, but I'd compute the position of the middle of the paddle, the position of the center of the ball, the distance between the two (= how far from the middle it hit), then react according to the ratio "length from the middle" / "half length of the paddle"