private void handleExplode() { if (ballBounds.getMinY() > areaBounds.getMaxY()) { isExploding = false; resetBall(); } else { ball.setTranslateX(ball.getTranslateX() + dx); ball.setTranslateY(ball.getTranslateY() - dy); } }
private void advanceBall() { double refAngle = 0; boolean doBounce = false; double jitter = 0; if (ballBounds.getMaxY() >= rectBounds.getMinY()) { if (ballBounds.getMaxX() < rectBounds.getMinX() || ballBounds.getMinX() > rectBounds.getMaxX()) { isExploding = true; ball.setFill(Color.RED); } else { // calculate a value between -0.5 and 0.5 which corresponds to the // position within the platform where the ball bounces double pos = (ball.getTranslateX() - rect.getTranslateX()) / rect.getWidth(); if (pos < 0) { pos = 0; } if (pos > 1) { pos = 1; } pos = pos - 0.5; // system.err.printf("%s\n", pos); jitter = pos; doBounce = true; } } if (ballBounds.getMinY() <= areaBounds.getMinY() || ballBounds.getMaxY() >= areaBounds.getMaxY()) { // this must never happen! doBounce = true; } else if (ballBounds.getMinX() <= areaBounds.getMinX() || ballBounds.getMaxX() >= areaBounds.getMaxX()) { doBounce = true; refAngle = Math.PI / 2; } if (doBounce) { ballAngle = calculateExitAngle(ballAngle, refAngle) + jitter; dx = ballSpeed * Math.cos(ballAngle); dy = ballSpeed * Math.sin(ballAngle); } ball.setTranslateX(ball.getTranslateX() + dx); ball.setTranslateY(ball.getTranslateY() - dy); }