package break_out.model; import break_out.Constants; /** * This class contains the information about the balls characteristics and behavior * * @author iSchumacher * @author modified by Gruppe 175: Moritz Henseleit, Ruben Meyer * */ public class Ball implements IBall{ /** * The balls position on the playground */ private Position position; /** * The balls direction */ private Vector2D direction; /** * The constructor of a ball * The balls position and direction are initialized here. */ public Ball() { this.position = new Position(0, 0); this.direction = new Vector2D(Constants.BALL_SPEED,Constants.BALL_SPEED); this.direction.rescale(); // start at bottom-center this.position.setX((Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER)/2); this.position.setY(Constants.SCREEN_HEIGHT-Constants.BALL_DIAMETER); } /** * The getter for the balls position * @return position The balls current position */ public Position getPosition() { return this.position; } /** * The getter for the balls direction * @return direction The balls current direction */ public Vector2D getDirection() { return this.direction; } /** * updates ball position */ public void updatePosition() { // sets X position this.position.setX(this.position.getX()+this.direction.getDx()); // sets Y position this.position.setY(this.position.getY()+this.direction.getDy()); } /** * Ball reacts to contact with the borders */ public void reactOnBorder() { // reacts on left border if(this.position.getX() <= 0) { this.position.setX(0); this.direction.setDx(-(this.direction.getDx())); } // reacts on right border (-Diameter because of hitbox) if(this.position.getX() >= Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER) { this.position.setX(Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER); this.direction.setDx(-(this.direction.getDx())); } // reacts on top border if(this.position.getY() <= 0) { this.position.setY(0); this.direction.setDy(-(this.direction.getDy())); } // reacts on bottom border (+Diameter because of hitbox) if(this.position.getY() >= Constants.SCREEN_HEIGHT - Constants.BALL_DIAMETER) { this.position.setY(Constants.SCREEN_HEIGHT - Constants.BALL_DIAMETER); this.direction.setDy(-(this.direction.getDy())); } } }