Ball - reflectOnPaddle - structuring and comments
This commit is contained in:
parent
af40f082d6
commit
131dedb22e
@ -140,29 +140,43 @@ public class Ball implements IBall {
|
|||||||
/**
|
/**
|
||||||
* Ball got hit by Paddle paddle
|
* Ball got hit by Paddle paddle
|
||||||
* @param paddle hitbox mechanism of paddle
|
* @param paddle hitbox mechanism of paddle
|
||||||
* @todo implementation
|
|
||||||
*/
|
*/
|
||||||
public void reflectOnPaddle(Paddle p) {
|
public void reflectOnPaddle(Paddle paddle) {
|
||||||
// get the Position of the middle of the paddle
|
// reflection point / offset point
|
||||||
Position newOffset = new Position(
|
Position reflectionPoint = new Position(
|
||||||
p.getPosition().getX() + p.getWidth() / 2.0,//the middle of the x coordinates
|
paddle.getPosition().getX() + (paddle.getWidth() / 2.0),
|
||||||
p.getPosition().getY() + p.getHeight() / 2.0);//the middle of the y coordinates
|
paddle.getPosition().getY() + (paddle.getHeight() / 2.0)
|
||||||
|
);
|
||||||
|
// new direction vector; assignment not here
|
||||||
|
Vector2D reflectionVector;
|
||||||
|
|
||||||
|
// no general solution, estimation required
|
||||||
|
// only two paddles defined in the game design, therefore greater or smaller than middle of screen
|
||||||
|
|
||||||
//deciding if the paddle is at the top or bottom to adjust if its +or- y direction
|
//deciding if the paddle is at the top or bottom to adjust if its +or- y direction
|
||||||
if (p.getPosition().getY() == 0) {
|
if (paddle.getPosition().getY() <= Constants.SCREEN_HEIGHT) {
|
||||||
newOffset.setY(newOffset.getY() - Constants.REFLECTION_OFFSET);// paddle at the top with negative direction
|
// top paddle
|
||||||
} else {
|
reflectionPoint.setY(reflectionPoint.getY() - Constants.REFLECTION_OFFSET);
|
||||||
newOffset.setY(newOffset.getY() + Constants.REFLECTION_OFFSET); // the paddle at the bottom with positive direction
|
} else {
|
||||||
|
// bottom paddle
|
||||||
|
reflectionPoint.setY(reflectionPoint.getY() + Constants.REFLECTION_OFFSET);
|
||||||
}
|
}
|
||||||
|
|
||||||
// calculating the center of the ball by dividing by zero
|
// calculating the center of the ball; needed for correct vector calculation
|
||||||
Position ballCenter = new Position(
|
Position ballCenter = new Position(
|
||||||
position.getX() + Constants.BALL_DIAMETER/2.0, //the middle of the x coordinates
|
position.getX() + (Constants.BALL_DIAMETER/2.0),
|
||||||
position.getY() + Constants.BALL_DIAMETER/2.0); //the middle of the y coordinates
|
position.getY() + (Constants.BALL_DIAMETER/2.0)
|
||||||
|
);
|
||||||
|
|
||||||
// The direction is set to the middle of the offset point and the ball
|
// The direction is set to the vector between offset point and the ball's center
|
||||||
direction = new Vector2D(newOffset, ballCenter);
|
reflectionVector = new Vector2D(reflectionPoint, ballCenter);
|
||||||
// rescaling to adjust the balls speed
|
|
||||||
direction.rescale();
|
// normalize vector
|
||||||
|
reflectionVector.rescale();
|
||||||
|
|
||||||
|
// replace direction vector
|
||||||
|
this.direction.setDx(reflectionVector.getDx());
|
||||||
|
this.direction.setDy(reflectionVector.getDy());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user