|
|
@ -52,19 +52,25 @@ int main() {
|
|
|
|
pad2.velocity.y = 0;
|
|
|
|
pad2.velocity.y = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Update ball velocity based on collision detection*/
|
|
|
|
/* Update ball velocity based on collision detection */
|
|
|
|
if (pad1.getRect().CheckCollision(ball.pos, ball.radius) || pad2.getRect().CheckCollision(ball.pos, ball.radius)) {
|
|
|
|
if (pad1.getRect().CheckCollision(ball.pos, ball.radius)) { /* Collision with paddle 1 */
|
|
|
|
ball.vel.x = ball.vel.x * (-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();
|
|
|
|
pad1.incrementPoints();
|
|
|
|
ball.pos = raylib::Vector2(window.GetWidth() / 2, window.GetHeight() / 2);
|
|
|
|
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();
|
|
|
|
pad2.incrementPoints();
|
|
|
|
ball.pos = raylib::Vector2(window.GetWidth() / 2, window.GetHeight() / 2);
|
|
|
|
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;
|
|
|
|
ball.vel.y = ball.vel.y * -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|