Browse Source

Fix broken collisions for walls and paddles

Occasionally the ball would get stuck inside the top and bottom walls or
one of the paddles if it didn't have the velocity to move outside of it
in a single cycle.

Now when a collision occurs, the ball is immediately moved out of the
wall or paddle it has collided with, solving this issue.
master
Jake Wakeling 2 years ago
parent
commit
5948c793eb
  1. 2
      CMakeLists.txt
  2. 33
      src/pong.cpp

2
CMakeLists.txt

@ -1,5 +1,5 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.12)
PROJECT(Pong VERSION 0.1.0 LANGUAGES CXX)
PROJECT(Pong VERSION 0.1.1 LANGUAGES CXX)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
ADD_COMPILE_DEFINITIONS(PROJECT_VERSION="${PROJECT_VERSION}")

33
src/pong.cpp

@ -95,37 +95,40 @@ int main(int ac, char *av[]) {
}
// Handle ball collision with top and bottom walls
if (ball.getPosition().y - bSize.y / 2.0f <= 0.0f) { bAngle = -bAngle; sounds.playSound1(); }
if (ball.getPosition().y + bSize.y / 2.0f >= sizeY) { bAngle = -bAngle; sounds.playSound1(); }
if (ball.getPosition().y - bSize.y / 2.0f <= 0.0f) {
ball.setPosition(ball.getPosition().x, bSize.x / 2.0f);
bAngle = -bAngle; sounds.playSound1();
}
if (ball.getPosition().y + bSize.y / 2.0f >= sizeY) {
ball.setPosition(ball.getPosition().x, sizeY - bSize.x / 2.0f);
bAngle = -bAngle; sounds.playSound1();
}
// Handle ball collision with left paddle
if (ball.getPosition().x - bSize.x / 2.0f < lPaddle.getPosition().x + pSize.x / 2.0f &&
ball.getPosition().x - bSize.x / 2.0f > lPaddle.getPosition().x &&
ball.getPosition().x + bSize.x / 2.0f > lPaddle.getPosition().x - pSize.x / 2.0f &&
ball.getPosition().y + bSize.y / 2.0f >= lPaddle.getPosition().y - pSize.y / 2.0f &&
ball.getPosition().y - bSize.y / 2.0f <= lPaddle.getPosition().y + pSize.y / 2.0f) {
if (ball.getPosition().y >= lPaddle.getPosition().y) {
bAngle = M_PI - bAngle + (std::rand() % 20) * M_PI / 180.0f;
sounds.playSound2();
}
else {
bAngle = M_PI - bAngle - (std::rand() % 20) * M_PI / 180.0f;
sounds.playSound2();
}
else { bAngle = M_PI - bAngle - (std::rand() % 20) * M_PI / 180.0f; sounds.playSound2(); }
ball.setPosition(lPaddle.getPosition().x + pSize.x / 2.0f + bSize.x / 2.0f, ball.getPosition().y);
}
// Handle ball collision with right paddle
if (ball.getPosition().x - bSize.x / 2.0f < rPaddle.getPosition().x - pSize.x / 2.0f &&
ball.getPosition().x + bSize.x / 2.0f > rPaddle.getPosition().x &&
if (ball.getPosition().x - bSize.x / 2.0f < rPaddle.getPosition().x + pSize.x / 2.0f &&
ball.getPosition().x + bSize.x / 2.0f > rPaddle.getPosition().x - pSize.x / 2.0f &&
ball.getPosition().y + bSize.y / 2.0f >= rPaddle.getPosition().y - pSize.y / 2.0f &&
ball.getPosition().y - bSize.y / 2.0f <= rPaddle.getPosition().y + pSize.y / 2.0f) {
if (ball.getPosition().y >= rPaddle.getPosition().y) {
bAngle = M_PI - bAngle + (std::rand() % 20) * M_PI / 180.0f;
sounds.playSound2();
}
else {
bAngle = M_PI - bAngle - (std::rand() % 20) * M_PI / 180.0f;
sounds.playSound2();
bAngle = M_PI - bAngle + (std::rand() % 20) * M_PI / 180.0f; sounds.playSound2();
}
else { bAngle = M_PI - bAngle - (std::rand() % 20) * M_PI / 180.0f; sounds.playSound2(); }
ball.setPosition(rPaddle.getPosition().x - pSize.x / 2.0f - bSize.x / 2.0f, ball.getPosition().y);
}
// Clear and redraw window

Loading…
Cancel
Save