From 61f573fa67267de4277001dc7969dbccbf0821d4 Mon Sep 17 00:00:00 2001 From: Damien Firmenich Date: Fri, 10 Sep 2021 16:59:33 +0200 Subject: [PATCH] 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. --- Userland/Games/Breakout/Game.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Userland/Games/Breakout/Game.cpp b/Userland/Games/Breakout/Game.cpp index dea1c43390..5722194a5b 100644 --- a/Userland/Games/Breakout/Game.cpp +++ b/Userland/Games/Breakout/Game.cpp @@ -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();