@@ -18,39 +18,28 @@ public class Collision {
1818 private Ball ball ;
1919 private final Course course ;
2020
21- private double [] offsets ;
22- private double [] scales ;
23-
2421 private double lastX ;
2522 private double lastY ;
2623
24+
25+
2726 /**
2827 * Create a new instace of Collision
2928 * @param ball the ball to evaluate
3029 * @param course the course in which the ball rolls
31- * @param offsets the offsets of the course
32- * @param scales the scales of the course
3330 */
34- public Collision (Ball ball , Course course , double [] offsets , double [] scales ) {
31+ public Collision (Ball ball , Course course ) {
3532 this .ball = ball ;
3633 this .course = course ;
37- this .offsets = offsets ;
38- this .scales = scales ;
3934 this .lastX = this .course .getStart ()[0 ];
4035 this .lastX = this .course .getStart ()[1 ];
4136 }
4237
43- /**
44- * Create a new instance of Collision from a template
45- * @param other the template
46- */
4738 public Collision (Collision other ) {
48- this .ball = other .ball ;
4939 this .course = other .course ;
50- this .offsets = other .offsets ;
51- this .scales = other .scales ;
40+ this .ball = other .ball ;
5241 this .lastX = other .lastX ;
53- this .lastX = other .lastY ;
42+ this .lastY = other .lastY ;
5443 }
5544
5645 /**
@@ -66,16 +55,17 @@ public boolean isGoalAchieved() {
6655
6756 /**
6857 * React when the ball hits a wall
58+ * @param ballX the pixel-x position of the ball
59+ * @param ballY the pixel-y position of the ball
6960 */
70- public void checkForWalls () {
71- double [] real = MathLib .toPixel (new double []{this .ball .getX (), this .ball .getY ()}, this .offsets , this .scales );
61+ public void checkForWalls (double ballX , double ballY ) {
7262 double vx = this .ball .getVelocityX ();
7363 double vy = this .ball .getVelocityY ();
74- if ((real [ 0 ] < Ball .RADIUS && vx < 0 ) || (real [ 0 ] > Golf .VIRTUAL_WIDTH - Ball .RADIUS && vx > 0 )) {
64+ if ((ballX < Ball .RADIUS && vx < 0 ) || (ballX > Golf .VIRTUAL_WIDTH - Ball .RADIUS && vx > 0 )) {
7565 this .ball .setVelocityX (-this .ball .getVelocityX ());
7666
7767 }
78- if ((real [ 1 ] < Ball .RADIUS && vy < 0 ) || (real [ 1 ] > Golf .VIRTUAL_HEIGHT - Ball .RADIUS && vy > 0 )) {
68+ if ((ballY < Ball .RADIUS && vy < 0 ) || (ballY > Golf .VIRTUAL_HEIGHT - Ball .RADIUS && vy > 0 )) {
7969 this .ball .setVelocityY (-this .ball .getVelocityY ());
8070 }
8171 }
@@ -87,8 +77,8 @@ public void checkForWalls() {
8777 public boolean ballInWater () {
8878 boolean water = false ;
8979
90- double ballX = this .ball .getX () ;
91- double ballY = this .ball .getY () ;
80+ double ballX = this .ball .last ()[ 0 ] ;
81+ double ballY = this .ball .last ()[ 1 ] ;
9282 Line2D path = new Line2D (this .lastX , this .lastY , ballX , ballY );
9383
9484 // Evaluate the line
@@ -106,9 +96,15 @@ public boolean ballInWater() {
10696 }
10797 }
10898
109- // Make the current position of the ball the last
110- this .lastX = ballX ;
111- this .lastY = ballY ;
99+
100+ if (water ) {
101+ // Make the current position of the ball the last
102+ this .lastX = Physics .hitCoord [0 ];
103+ this .lastY = Physics .hitCoord [1 ];
104+ } else {
105+ this .lastX = ballX ;
106+ this .lastY = ballY ;
107+ }
112108
113109 return water ;
114110 }
@@ -145,24 +141,4 @@ public Ball getBall() {
145141 public void setBall (Ball ball ) {
146142 this .ball = ball ;
147143 }
148-
149- public Course getCourse () {
150- return course ;
151- }
152-
153- public double [] getOffsets () {
154- return offsets ;
155- }
156-
157- public void setOffsets (double [] offsets ) {
158- this .offsets = offsets ;
159- }
160-
161- public double [] getScales () {
162- return scales ;
163- }
164-
165- public void setScales (double [] scales ) {
166- this .scales = scales ;
167- }
168144}
0 commit comments