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:
parent
844c2e1f3b
commit
959038d410
Notes:
sideshowbarker
2024-07-19 01:28:16 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/959038d410e
1 changed files with 4 additions and 0 deletions
|
@ -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) {
|
||||
|
|
Loading…
Add table
Reference in a new issue