mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:47:44 +00:00
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:
parent
d0245b5e6b
commit
61f573fa67
1 changed files with 4 additions and 2 deletions
|
@ -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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue