From cd97e8432e4c802cf642da3f98bbc3c8fdc0805a Mon Sep 17 00:00:00 2001 From: "Joshua \"Shadow\" Monson" <4204089+ShadowLordAlpha@users.noreply.github.com> Date: Thu, 21 Nov 2024 01:57:28 -0700 Subject: [PATCH 1/3] Correct Lower Paddle Collision When colliding with the lower wall the paddle would incorrectly go to far before stopping. This fixes the order of operations to correct when the paddle stops --- Chapter01/Game.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Chapter01/Game.cpp b/Chapter01/Game.cpp index 3b18199e..aacf99a0 100644 --- a/Chapter01/Game.cpp +++ b/Chapter01/Game.cpp @@ -141,9 +141,9 @@ void Game::UpdateGame() { mPaddlePos.y = paddleH/2.0f + thickness; } - else if (mPaddlePos.y > (768.0f - paddleH/2.0f - thickness)) + else if (mPaddlePos.y > (768.0f - (paddleH/2.0f - thickness))) { - mPaddlePos.y = 768.0f - paddleH/2.0f - thickness; + mPaddlePos.y = 768.0f - (paddleH/2.0f - thickness); } } From e049f0ad319e3c3602df9c073812fef5132c8a73 Mon Sep 17 00:00:00 2001 From: "Joshua \"Shadow\" Monson" <4204089+ShadowLordAlpha@users.noreply.github.com> Date: Thu, 21 Nov 2024 02:11:25 -0700 Subject: [PATCH 2/3] Fix Ball collision with walls The Ball was going half way into the side walls instead of bouncing off directly due to the collision code not taking into account the thickness of the ball to its center. As we are storing the position of the ball as its center we also need to take that into account when colliding --- Chapter01/Game.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Chapter01/Game.cpp b/Chapter01/Game.cpp index aacf99a0..03148e03 100644 --- a/Chapter01/Game.cpp +++ b/Chapter01/Game.cpp @@ -172,18 +172,18 @@ void Game::UpdateGame() mIsRunning = false; } // Did the ball collide with the right wall? - else if (mBallPos.x >= (1024.0f - thickness) && mBallVel.x > 0.0f) + else if (mBallPos.x >= (1024.0f - thickness * 1.5) && mBallVel.x > 0.0f) { mBallVel.x *= -1.0f; } // Did the ball collide with the top wall? - if (mBallPos.y <= thickness && mBallVel.y < 0.0f) + if (mBallPos.y <= thickness * 1.5 && mBallVel.y < 0.0f) { mBallVel.y *= -1; } // Did the ball collide with the bottom wall? - else if (mBallPos.y >= (768 - thickness) && + else if (mBallPos.y >= (768 - thickness * 1.5) && mBallVel.y > 0.0f) { mBallVel.y *= -1; From ab54e145f988343b1ff6280f056a85209cb901fa Mon Sep 17 00:00:00 2001 From: "Joshua \"Shadow\" Monson" <4204089+ShadowLordAlpha@users.noreply.github.com> Date: Thu, 21 Nov 2024 02:20:08 -0700 Subject: [PATCH 3/3] Correct Ball collision with Paddle Adjust the x positions for checking the ball collision with the paddle so the ball bounces before getting half way through the paddle --- Chapter01/Game.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter01/Game.cpp b/Chapter01/Game.cpp index 03148e03..8a210cda 100644 --- a/Chapter01/Game.cpp +++ b/Chapter01/Game.cpp @@ -160,7 +160,7 @@ void Game::UpdateGame() // Our y-difference is small enough diff <= paddleH / 2.0f && // We are in the correct x-position - mBallPos.x <= 25.0f && mBallPos.x >= 20.0f && + mBallPos.x <= 30.0f && mBallPos.x >= 25.0f && // The ball is moving to the left mBallVel.x < 0.0f) {