Skip to content

Commit e2e6b4f

Browse files
authored
Merge pull request #80 from jCodingStuff/FinalizingPhysics
Finalizing physics
2 parents 46898f2 + 4b20ea9 commit e2e6b4f

File tree

9 files changed

+376
-237
lines changed

9 files changed

+376
-237
lines changed

core/src/com/group/golf/Ball.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
package com.group.golf;
22

33
import com.badlogic.gdx.graphics.Texture;
4-
import com.badlogic.gdx.graphics.g2d.Batch;
54
import com.badlogic.gdx.math.Circle;
5+
import com.group.golf.Physics.Queue;
66
import com.group.golf.math.MathLib;
77
import com.group.golf.screens.CourseScreen;
88

9+
910
/**
1011
* A class to save the data of the ball
1112
*/
12-
public class Ball {
13+
public class Ball extends Queue<double[]>{
1314

1415
public static final float RADIUS = 10;
1516

17+
1618
private double mass;
1719
private double x;
1820
private double y;
1921
private double velocityX;
2022
private double velocityY;
23+
private double accelerationX;
24+
private double accelerationY;
25+
2126

2227
private Texture texture;
2328
private CourseScreen courseScreen;
@@ -51,6 +56,17 @@ public Ball(double mass) {
5156
this.collisionCircle = new Circle((float) this.x, (float) this.y, RADIUS);
5257
}
5358

59+
public double[] dequeue() {
60+
double[] coord = super.dequeue();
61+
setX(coord[0]);
62+
setY(coord[1]);
63+
return coord;
64+
}
65+
66+
public void addCoord(double[] coord) {
67+
super.enqueue(coord);
68+
}
69+
5470
/**
5571
* Create a new Ball using another one as a template
5672
* @param other the template ball
@@ -137,8 +153,7 @@ public double calcVelocity() {
137153
* Get access to the x-component of the velocity
138154
* @return the x-component of the velocity
139155
*/
140-
public double getVelocityX() {
141-
return velocityX;
156+
public double getVelocityX() {return velocityX;
142157
}
143158

144159
/**
@@ -186,10 +201,14 @@ public void setMass(double mass) {
186201
* @return the x-coordinate for position
187202
*/
188203
public double getX() {
189-
return x;
204+
// if (super.tail == null)
205+
return x;
206+
// return super.last()[0];
190207
}
191208

192209

210+
211+
193212
/**
194213
* Set a new x-position
195214
* @param x the new x-position
@@ -204,7 +223,9 @@ public void setX(double x) {
204223
* @return the y-coordinate for position
205224
*/
206225
public double getY() {
207-
return y;
226+
// if (super.tail == null)
227+
return y;
228+
// return super.last()[1];
208229
}
209230

210231
/**
@@ -226,4 +247,20 @@ public void setPosition(double x, double y) {
226247
this.y = y;
227248
this.updateCollisionCircle();
228249
}
250+
251+
public void setAccelerationX(double accelerationX) {
252+
this.accelerationX = accelerationX;
253+
}
254+
255+
public void setAccelerationY(double accelerationY) {
256+
this.accelerationY = accelerationY;
257+
}
258+
259+
public double getAccelerationX() {
260+
return accelerationX;
261+
}
262+
263+
public double getAccelerationY() {
264+
return accelerationY;
265+
}
229266
}

core/src/com/group/golf/Physics/Collision.java

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)