From 959038d410ea0fbc9c592f6cb7a5dce763768c39 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 10 Nov 2020 14:13:09 +0100 Subject: [PATCH] Breakout: Change ball x velocity depending on where it hits paddle This makes the game less deterministic and more fun. The "physics" definitely feel a little goofy, and I'm sure they can be greatly improved, but still it's already better. :^) --- Games/Breakout/Game.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Games/Breakout/Game.cpp b/Games/Breakout/Game.cpp index 30e4f065fb..21b7405972 100644 --- a/Games/Breakout/Game.cpp +++ b/Games/Breakout/Game.cpp @@ -198,6 +198,10 @@ void Game::tick() 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); + + 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(); + new_ball.velocity.set_x(relative_impact_point * 7); } for (auto& brick : m_bricks) {