-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBall.java
195 lines (182 loc) · 7.59 KB
/
Ball.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import java.awt.Color;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
public class Ball extends PitchObject{
private double xVelocity = 0;
private double yVelocity = 0;
public double getxVelocity() {
return xVelocity;
}
public void setxVelocity(double xVelocity) {
this.xVelocity = xVelocity;
}
public double getyVelocity() {
return yVelocity;
}
public void setyVelocity(double yVelocity) {
this.yVelocity = yVelocity;
}
public Shape getShape(){
//Returns the shape for collision detection
return new Ellipse2D.Double(getX(), getY(), getWidth(), getHeight());
}
public Ball(Color color, double x, double y, double width, double height){
super(color, x, y, width, height);
}
public void updateLocation() {
//Updates location using velocity
setY(getY() - getyVelocity());
setX(getX() + getxVelocity());
//Gravity
setyVelocity(getyVelocity() - 0.01);
}
public void detectCollison(Slime SlimeOne, Slime SlimeTwo){
//Centre of each object used for collision detection
double centreBallX = getX() + 10;
double centreBallY = getY() + 10;
double centreSlimeOneX = SlimeOne.getX() + 37.5;
double centreSlimeOneY = SlimeOne.getY() + 37.5;
double centreSlimeTwoX = SlimeTwo.getX() + 37.5;
double centreSlimeTwoY = SlimeTwo.getY() + 37.5;
//Checks if the ball collides with one of the slimes
if(Math.pow(Math.max(centreBallX, centreSlimeOneX) - Math.min(centreBallX, centreSlimeOneX), 2) + Math.pow(Math.max(centreBallY, centreSlimeOneY) - Math.min(centreBallY, centreSlimeOneY), 2) < Math.pow(47.5, 2)){
//Makes sure the collision isn't with the non-existent half of the slime semi-circle
if(centreBallY < centreSlimeOneY + 10){
//Adjust velocity TODO figure out want the hell I put here
double angle;
if(centreBallX > centreSlimeOneX){
//Angle of the ball to the centre of the slime
angle = Math.atan((centreSlimeOneY - centreBallY) / (centreBallX - centreSlimeOneX));
setX(centreSlimeOneX + Math.cos(angle) * 48 - 10);
} else if (centreBallX < centreSlimeOneX){
angle = Math.atan((centreSlimeOneY - centreBallY) / (centreSlimeOneX - centreBallX));
setX(centreSlimeOneX - Math.cos(angle) * 48 - 10);
//remove divide by zero error
} else {
angle = Math.PI / 2;
}
setY(centreSlimeOneY - Math.sin(angle) * 48 - 10);
//Sets velocity
double xDiff = (getX() - SlimeOne.getX() * 2);
double yDiff = getY() - SlimeOne.getY();
double xVDiff = getxVelocity() - SlimeOne.getxVelocity();
double yVDiff = getyVelocity() - (SlimeOne.getyVelocity());
double distance = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
double temp = (xDiff * xVDiff + yDiff * yVDiff) / distance;
setyVelocity((getyVelocity() + SlimeOne.getyVelocity() - (2 * yDiff * temp) / distance) * 1);
setxVelocity((getxVelocity() + SlimeOne.getxVelocity() - (2 * xDiff * temp) / distance) * 0.7);
if(getxVelocity() > 2.25){
setxVelocity(2.25);
} else if (getxVelocity() < -2.25){
setxVelocity(-2.25);
}
if(getyVelocity() > 1.4){
setyVelocity(1.4);
} else if (getyVelocity() < -1.4){
setyVelocity(-1.4);
}
}
}
//Repeats for other slime
if(Math.pow(Math.max(centreBallX, centreSlimeTwoX) - Math.min(centreBallX, centreSlimeTwoX), 2) + Math.pow(Math.max(centreBallY, centreSlimeTwoY) - Math.min(centreBallY, centreSlimeTwoY), 2) < Math.pow(47.5, 2)){
if(centreBallY < centreSlimeTwoY + 10){
double angle;
if(centreBallX > centreSlimeTwoX){
angle = Math.atan((centreSlimeTwoY - centreBallY) / (centreBallX - centreSlimeTwoX));
setX(centreSlimeTwoX + Math.cos(angle) * 48 - 10);
} else if (centreBallX < centreSlimeTwoX){
angle = Math.atan((centreSlimeTwoY - centreBallY) / (centreSlimeTwoX - centreBallX));
setX(centreSlimeTwoX - Math.cos(angle) * 48 - 10);
} else {
angle = Math.PI / 2;
}
setY(centreSlimeTwoY - Math.sin(angle) * 48 - 10);
double xDiff = (getX() - SlimeTwo.getX() * 2);
double yDiff = getY() - SlimeTwo.getY();
double xVDiff = getxVelocity() - SlimeTwo.getxVelocity();
double yVDiff = getyVelocity() - (SlimeTwo.getyVelocity());
double distance = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
double temp = (xDiff * xVDiff + yDiff * yVDiff) / distance;
setyVelocity((getyVelocity() + SlimeTwo.getyVelocity() - (2 * yDiff * temp) / distance) * 1);
setxVelocity((getxVelocity() + SlimeTwo.getxVelocity() - (2 * xDiff * temp) / distance) * 0.7);
if(getxVelocity() > 2.25){
setxVelocity(2.25);
} else if (getxVelocity() < -2.25){
setxVelocity(-2.25);
}
if(getyVelocity() > 1.4){
setyVelocity(1.4);
} else if (getyVelocity() < -1.4){
setyVelocity(-1.4);
}
}
}
//Checks if the ball has hit the floor
if(getShape().intersects(0, 295, 725, 65) && getyVelocity() < 0){
//Reverses y directions and slows down
setyVelocity(getyVelocity() * -0.75);
setxVelocity(getxVelocity() * 0.75);
setY(275);
//Checks if the ball has hit the left wall
} else if (getX() <= 0) {
setxVelocity(getxVelocity() * -0.75);
setX(0);
//Checks if the ball has hit the right wall
} else if (getX() >= 705) {
setxVelocity(getxVelocity() * -0.75);
setX(705);
//Checks if the ball has hits the left crossbar
} else if (getShape().intersects(0, 222, 40, 1)){
//Collision from above
if(centreBallX <= 40 && centreBallY < 222){
setyVelocity(getyVelocity() * -0.75);
setxVelocity(getxVelocity() * 0.75);
setY(202);
//Collision from below
} else if(centreBallX <= 40 && centreBallY >= 222){
setyVelocity(getyVelocity() * -0.75);
setxVelocity(getxVelocity() * 0.75);
setY(223);
//Collision on edge
} else {
double angle = Math.atan((222 - centreBallY) / (centreBallX - 40));
double velocity = Math.sqrt(Math.pow(getyVelocity(), 2) * Math.pow(getxVelocity(), 2));
setyVelocity(velocity * Math.sin(angle));
setxVelocity(velocity * Math.cos(angle));
setY(212 - 11 * Math.sin(angle));
setX(30 + 11 * Math.cos(angle));
}
//Checks if the ball hits the right crossbar
} else if (getShape().intersects(685, 222, 40, 1)){
if(centreBallX >= 685 && centreBallY < 222){
setyVelocity(getyVelocity() * -0.75);
setxVelocity(getxVelocity() * 0.75);
setY(202);
//Collision from below
} else if(centreBallX >= 685 && centreBallY >= 222){
setyVelocity(getyVelocity() * -0.75);
setxVelocity(getxVelocity() * 0.75);
setY(223);
//Collision on edge
} else {
double angle = Math.atan((222 - centreBallY) / (685 - centreBallX));
double velocity = Math.sqrt(Math.pow(getyVelocity(), 2) * Math.pow(getxVelocity(), 2));
setyVelocity(velocity * Math.sin(angle));
setxVelocity(-velocity * Math.cos(angle));
setY(212 - 11 * Math.sin(angle));
setX(675 - 11 * Math.cos(angle));
}
//Slows down ball if in the net
} else if ((getX() <= 20 && getY() >= 222) | (getX() >= 685 && getY() >= 222)){
setxVelocity(getxVelocity() * 0.98);
setyVelocity(getyVelocity() * 0.98);
}
}
public ArrayList<ShapeColor> drawObject() {
//Passes information to draw the ball to the pitch
ArrayList<ShapeColor> array = new ArrayList<ShapeColor>();
array.add(new ShapeColor(getColor(), new Ellipse2D.Double(getX(), getY(), getWidth(), getHeight())));
return array;
}
}