Skip to content

Commit

Permalink
Fix reset bug, improve collision logic
Browse files Browse the repository at this point in the history
The collision logic has been improved more. Still not perfect but it won't be a problem at normal ball speeds.

I also fixed a problem with resetting the game for a multiplayer match after having played in singleplayer.
  • Loading branch information
KyleRAnderson committed Jan 21, 2019
1 parent 9573c48 commit 8dae5b0
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 10 deletions.
12 changes: 2 additions & 10 deletions ICS4UC_RST/src/games/pong/Pong.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ private boolean testBallCollision(Paddle testingPaddle, final long nanosSinceLas
if (didIntersect) {
final Side ballSide = testingPaddle.getSide();
// Determine how long it's been since the ball would've collided. time = distance / velocity
final double timePassedSinceCollision = (Math.abs(ball.getX(ballSide) - paddlePoint)) / (ball.getRunPerNanoSecond());
final double timePassedSinceCollision = Math.abs((ball.getX(ballSide) - paddlePoint) / ball.getRunPerNanoSecond());
// Determine where the paddle would have been at that time. distance = velocity * time.
final double paddleTopAtTime = testingPaddle.getY(Side.TOP) - timePassedSinceCollision * testingPaddle.getVelYNanos();
// Also get the bottom position here.
Expand All @@ -459,16 +459,8 @@ private boolean testBallCollision(Paddle testingPaddle, final long nanosSinceLas
// Determine the ball's height at that time.
final double ballTopYAtTime = ball.getY(Side.TOP) - timePassedSinceCollision * (ball.getRisePerNanoSecond());
final double ballCenterAtTime = PongBall.getY(ball.getRadius(), ballTopYAtTime, Side.CENTER);
double goodBallPos;
if (ballCenterAtTime >= paddleTopCollisionPoint) {
goodBallPos = ballTopYAtTime;
} else if (ballCenterAtTime <= paddleBottomCollisionPoint) {
goodBallPos = PongBall.getY(ball.getRadius(), ballTopYAtTime, Side.BOTTOM);
} else {
goodBallPos = ballCenterAtTime;
}

didIntersect = doesBallIntersect(goodBallPos, paddleTopCollisionPoint, paddleBottomCollisionPoint);
didIntersect = doesBallIntersect(ballCenterAtTime, paddleTopCollisionPoint, paddleBottomCollisionPoint);

// If there was an intersection, we need to continue even more with determining the ball's new location.
if (didIntersect) {
Expand Down
1 change: 1 addition & 0 deletions ICS4UC_RST/src/games/pong/ui/PongUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ public PongUI getWindow() {
@Override
public void reset() {
hasInitializedPlayers = false;
getChildren().remove(selector); // Make sure the selector isn't on screen anymore
game = new Pong(); // Initialize new pong game with the correct type of players
resetKeyBindings();
game.addEventListener(this::gameEventHappened);
Expand Down
Binary file added ICS4UC_RST/src/res/images/arcade.ico
Binary file not shown.

0 comments on commit 8dae5b0

Please sign in to comment.