diff --git a/src/break_out/model/Ball.java b/src/break_out/model/Ball.java index 845d24e..f6e4c82 100644 --- a/src/break_out/model/Ball.java +++ b/src/break_out/model/Ball.java @@ -31,8 +31,8 @@ public class Ball implements IBall{ this.direction.rescale(); // start at bottom-center - this.position.setX(Constants.SCREEN_WIDTH/2); - this.position.setY(Constants.SCREEN_HEIGHT-3*Constants.BALL_DIAMETER); + this.position.setX((Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER)/2); + this.position.setY(Constants.SCREEN_HEIGHT-Constants.BALL_DIAMETER); } /** @@ -67,16 +67,28 @@ public class Ball implements IBall{ */ public void reactOnBorder() { // reacts on left border - if(this.position.getX() < 0) this.direction.setDx(-(this.direction.getDx())); + 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.BALL_DIAMETER > Constants.SCREEN_WIDTH) 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.direction.setDy(-(this.direction.getDy())); + 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.BALL_DIAMETER > Constants.SCREEN_HEIGHT) this.direction.setDy(-(this.direction.getDy())); + 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())); + } } } diff --git a/src/break_out/model/Vector2D.java b/src/break_out/model/Vector2D.java index 1ec700a..1f8d01f 100644 --- a/src/break_out/model/Vector2D.java +++ b/src/break_out/model/Vector2D.java @@ -73,8 +73,8 @@ public class Vector2D implements IVector2D { */ public void rescale() { // calc unit vector and set it - double vectorlength = Math.sqrt(Math.pow(getDx(), 2) + Math.pow(getDy(), 2)); //using the square root of x and y - setDx((1/vectorlength) * getDx() * Constants.BALL_SPEED); - setDy((1/vectorlength) * getDy() * Constants.BALL_SPEED); + double vectorLength = Math.sqrt(Math.pow(getDx(), 2) + Math.pow(getDy(), 2)); //using the square root of x and y + setDx((1/vectorLength) * getDx() * Constants.BALL_SPEED); + setDy((1/vectorLength) * getDy() * Constants.BALL_SPEED); } } diff --git a/src/break_out/view/Field.java b/src/break_out/view/Field.java index 8dc9104..033af9e 100644 --- a/src/break_out/view/Field.java +++ b/src/break_out/view/Field.java @@ -119,14 +119,19 @@ public class Field extends JPanel { * @param g2 The graphics object */ private void drawGrid(Graphics2D g2) { - // vertical lines - for(int i = 1 ; i < Math.round(Constants.SCREEN_WIDTH/Constants.SQUARES_X); i++) { - g2.drawLine(i*Constants.SQUARES_X, 0, i*Constants.SQUARES_X, Constants.SCREEN_HEIGHT); + + // size of grid blocks + int blockWidth = Constants.SCREEN_WIDTH / Constants.SQUARES_X; + int blockHeight = Constants.SCREEN_HEIGHT / Constants.SQUARES_Y; + + // draw vertical lines + for(int i = 1 ; i < Constants.SQUARES_X; i++) { + g2.drawLine(i*blockWidth, 0, i*blockWidth, Constants.SCREEN_HEIGHT); } - // horizontal lines - for(int i = 1 ; i < Math.round(Constants.SCREEN_HEIGHT/Constants.SQUARES_Y); i++) { - g2.drawLine(0, i*Constants.SQUARES_Y, Constants.SCREEN_WIDTH, i*Constants.SQUARES_Y); + // draw horizontal lines + for(int i = 1 ; i < Constants.SQUARES_Y; i++) { + g2.drawLine(0, i*blockHeight, Constants.SCREEN_WIDTH, i*blockHeight); } }