From 6ddf6936bf28a416fb3983b9247902b887c976e6 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Wed, 17 Jan 2024 23:40:14 -0500 Subject: [PATCH] Added more comments, and fixed bug where ball would sometimes get 'stuck' inside paddle --- main.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index fcdeba5..efe934b 100644 --- a/main.cpp +++ b/main.cpp @@ -52,19 +52,25 @@ int main() { pad2.velocity.y = 0; } - /* Update ball velocity based on collision detection*/ - if (pad1.getRect().CheckCollision(ball.pos, ball.radius) || pad2.getRect().CheckCollision(ball.pos, ball.radius)) { + /* Update ball velocity based on collision detection */ + if (pad1.getRect().CheckCollision(ball.pos, ball.radius)) { /* Collision with paddle 1 */ ball.vel.x = ball.vel.x * (-1); + ball.pos.x = pad1.getRect().x + pad1.getRect().GetWidth() + ball.radius + 1; /* Ensuring that the ball doesn't get stuck inside the paddle */ } - if (ball.pos.x + ball.radius >= window.GetWidth()) { + if (pad2.getRect().CheckCollision(ball.pos, ball.radius)) { /* Collision with paddle 2 */ + ball.vel.x = ball.vel.x * (-1); + ball.pos.x = pad2.getRect().x - ball.radius - 1; + } + + if (ball.pos.x + ball.radius >= window.GetWidth()) { /* Collision with right wall */ pad1.incrementPoints(); ball.pos = raylib::Vector2(window.GetWidth() / 2, window.GetHeight() / 2); } - if (ball.pos.x - ball.radius <= 0) { + if (ball.pos.x - ball.radius <= 0) { /* Collision with left wall */ pad2.incrementPoints(); ball.pos = raylib::Vector2(window.GetWidth() / 2, window.GetHeight() / 2); } - if (ball.pos.y - ball.radius <= 0 || ball.pos.y + ball.radius >= window.GetHeight()) { + if (ball.pos.y - ball.radius <= 0 || ball.pos.y + ball.radius >= window.GetHeight()) { /* Collision with top and bottom wals */ ball.vel.y = ball.vel.y * -1; }