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. :^)
This commit is contained in:
Andreas Kling 2020-11-10 14:13:09 +01:00
parent 844c2e1f3b
commit 959038d410
Notes: sideshowbarker 2024-07-19 01:28:16 +09:00

View file

@ -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) {