Skip to content

Commit 8dae5b0

Browse files
committed
Fix reset bug, improve collision logic
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.
1 parent 9573c48 commit 8dae5b0

File tree

3 files changed

+3
-10
lines changed

3 files changed

+3
-10
lines changed

ICS4UC_RST/src/games/pong/Pong.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ private boolean testBallCollision(Paddle testingPaddle, final long nanosSinceLas
449449
if (didIntersect) {
450450
final Side ballSide = testingPaddle.getSide();
451451
// Determine how long it's been since the ball would've collided. time = distance / velocity
452-
final double timePassedSinceCollision = (Math.abs(ball.getX(ballSide) - paddlePoint)) / (ball.getRunPerNanoSecond());
452+
final double timePassedSinceCollision = Math.abs((ball.getX(ballSide) - paddlePoint) / ball.getRunPerNanoSecond());
453453
// Determine where the paddle would have been at that time. distance = velocity * time.
454454
final double paddleTopAtTime = testingPaddle.getY(Side.TOP) - timePassedSinceCollision * testingPaddle.getVelYNanos();
455455
// Also get the bottom position here.
@@ -459,16 +459,8 @@ private boolean testBallCollision(Paddle testingPaddle, final long nanosSinceLas
459459
// Determine the ball's height at that time.
460460
final double ballTopYAtTime = ball.getY(Side.TOP) - timePassedSinceCollision * (ball.getRisePerNanoSecond());
461461
final double ballCenterAtTime = PongBall.getY(ball.getRadius(), ballTopYAtTime, Side.CENTER);
462-
double goodBallPos;
463-
if (ballCenterAtTime >= paddleTopCollisionPoint) {
464-
goodBallPos = ballTopYAtTime;
465-
} else if (ballCenterAtTime <= paddleBottomCollisionPoint) {
466-
goodBallPos = PongBall.getY(ball.getRadius(), ballTopYAtTime, Side.BOTTOM);
467-
} else {
468-
goodBallPos = ballCenterAtTime;
469-
}
470462

471-
didIntersect = doesBallIntersect(goodBallPos, paddleTopCollisionPoint, paddleBottomCollisionPoint);
463+
didIntersect = doesBallIntersect(ballCenterAtTime, paddleTopCollisionPoint, paddleBottomCollisionPoint);
472464

473465
// If there was an intersection, we need to continue even more with determining the ball's new location.
474466
if (didIntersect) {

ICS4UC_RST/src/games/pong/ui/PongUI.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ public PongUI getWindow() {
451451
@Override
452452
public void reset() {
453453
hasInitializedPlayers = false;
454+
getChildren().remove(selector); // Make sure the selector isn't on screen anymore
454455
game = new Pong(); // Initialize new pong game with the correct type of players
455456
resetKeyBindings();
456457
game.addEventListener(this::gameEventHappened);

ICS4UC_RST/src/res/images/arcade.ico

23.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)