diff --git a/src/break_out/model/Ball.java b/src/break_out/model/Ball.java index 905bf78..4758721 100644 --- a/src/break_out/model/Ball.java +++ b/src/break_out/model/Ball.java @@ -27,6 +27,7 @@ public class Ball implements IBall{ 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/2); diff --git a/src/break_out/model/IVector2D.java b/src/break_out/model/IVector2D.java new file mode 100644 index 0000000..0353f3c --- /dev/null +++ b/src/break_out/model/IVector2D.java @@ -0,0 +1,12 @@ +package break_out.model; + +public interface IVector2D { + + // Exercise 1 + public double getDx(); + public void setDx(double dx); + public double getDy(); + public void setDy(double dy); + + public void rescale(); +} \ No newline at end of file diff --git a/src/break_out/model/Vector2D.java b/src/break_out/model/Vector2D.java index 3fb2dd6..4dbaa9a 100644 --- a/src/break_out/model/Vector2D.java +++ b/src/break_out/model/Vector2D.java @@ -8,7 +8,7 @@ import break_out.model.Position; * * @author I. Schumacher */ -public class Vector2D { +public class Vector2D implements IVector2D { /** * The x part of the vector @@ -67,4 +67,13 @@ public class Vector2D { this.dy = dy; } + /* + * Rescale part + */ + public void rescale() { + // calc unit vector + double vectorlength = Math.sqrt(Math.pow(getDx(), 2) + Math.pow(getDy(), 2)); + setDx((1/vectorlength) * getDx() * Constants.BALL_SPEED); + setDy((1/vectorlength) * getDy() * Constants.BALL_SPEED); + } }