private void onGameStatePlaying() { // ball Point position = ball.getPosition(); if (shadowCount >= SHADOW_SKIP) { if (previousBallPositions.size() >= NUMBER_OF_BALL_SHADOWS) { previousBallPositions.removeLast(); } previousBallPositions.addFirst(new Point(position)); shadowCount = 0; } else { shadowCount += 1; } position.add((ballSpeed * ballUnitVector.getX()), -(ballSpeed * ballUnitVector.getY())); ball.setPosition(position); ball.setVisible(true); for (int i = 0; i < NUMBER_OF_BALL_SHADOWS; i++) { VisualControl visualControl = ballShadows.get(i); if (previousBallPositions.size() > i) { visualControl.setPosition(previousBallPositions.get(i)); visualControl.setVisible(true); } else { visualControl.setVisible(false); } } // bounce if (bouncedControl != null) { if (!bouncedControl.isHit(ball.getPosition())) { bouncedControl = null; } } else { for (BounceableControl bounceableControl : paddles.values()) { bounceableControls.add(bounceableControl); } for (BounceableControl bounceableControl : bounceableControls) { if (bounceBall(bounceableControl)) { bouncedControl = bounceableControl; break; } } for (BounceableControl bounceableControl : paddles.values()) { bounceableControls.remove(bounceableControl); } } // goal if ((position.getX() <= 0.0f) || (position.getX() >= (float) displayMode.getWidth())) { gameState = GameState.End; } }
private boolean bounceBall(BounceableControl control) { /* log.debug("Cheking..."); log.debug("\tcontrol: "+control.getPosition().toString()+", ball: "+ball.getPosition().toString()); log.debug("\tvector: x="+ballUnitVector.getX()+", y="+ballUnitVector.getY()); */ boolean isHit = false; if ((control.isVisible()) && (control.isHit(ball.getPosition()))) { log.debug("bounced!!!!!!!!!!!!!!"); float angleDelta = 90.0f - control.getRotation(); // get current angle of ball vector Point previousBallPosition = new Point(0.0f, 0.0f); Point currentBallPosition = new Point(ballUnitVector.getX(), ballUnitVector.getY()); // float ballVectorMagnitude = PointUtility.getDistance(previousBallPosition, // currentBallPosition); float currentBallAngle = PointUtility.getAngle(previousBallPosition, currentBallPosition); log.debug("\t\tcurrentBallAngle=" + currentBallAngle); // get new vector float targetBallAngle = (currentBallAngle + angleDelta); float newX = (float) Math.cos(Math.toRadians(targetBallAngle)); float newY = (float) Math.sin(Math.toRadians(targetBallAngle)); log.debug("\t\tnew vector: x=" + newX + ", y=" + newY); newX = (-1 * newX); // back to original angle previousBallPosition.set(0.0f, 0.0f); currentBallPosition.set(newX, newY); log.debug("\t\tnewX=" + newX + ", newY=" + newY); float newAngle = PointUtility.getAngle(previousBallPosition, currentBallPosition); targetBallAngle = (newAngle - angleDelta); log.debug("\t\tnew angle=" + newAngle + ", targetBallAngle=" + targetBallAngle); newX = (float) Math.cos(Math.toRadians(targetBallAngle)); newY = (float) Math.sin(Math.toRadians(targetBallAngle)); ballUnitVector.set(newX, newY); ballUnitVector = ballUnitVector.normalise(null); log.debug("\t\tfinal: x=" + ballUnitVector.getX() + ", y=" + ballUnitVector.getY()); isHit = true; } return isHit; }