Breakout: Improve collision response between ball and paddle

When the ball hits the side of the paddle, it would get stuck because
the paddle moves faster than the ball. This commit forces the post-
collision vertical velocity of the ball to be going up and makes sure
that new ball's y-position is higher than in the previous frame.
This commit is contained in:
Damien Firmenich 2021-09-10 16:59:33 +02:00 committed by Andreas Kling
parent d0245b5e6b
commit 61f573fa67
Notes: sideshowbarker 2024-07-18 04:19:32 +09:00

View File

@ -266,8 +266,10 @@ void Game::tick()
update(enclosing_int_rect(new_ball.rect()));
if (new_ball.rect().intersects(m_paddle.rect)) {
new_ball.position.set_y(m_ball.y());
new_ball.velocity.set_y(new_ball.velocity.y() * -1);
if (m_ball.y() < new_ball.y()) {
new_ball.position.set_y(m_ball.y());
}
new_ball.velocity.set_y(fabs(new_ball.velocity.y()) * -1);
float distance_to_middle_of_paddle = new_ball.x() - m_paddle.rect.center().x();
float relative_impact_point = distance_to_middle_of_paddle / m_paddle.rect.width();