-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle.java
More file actions
210 lines (186 loc) · 5.69 KB
/
Particle.java
File metadata and controls
210 lines (186 loc) · 5.69 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import java.awt.*;
import java.util.*;
public class Particle {
private String _name;
private double _x, _y;
private double _vx, _vy;
private double _radius;
private double _lastUpdateTime;
/**
* Helper method to parse a string into a Particle.
* DO NOT MODIFY THIS METHOD
* @param str the string to parse
* @return the parsed Particle
*/
public static Particle build (String str) {
String[] tokens = str.split("\\s+");
double[] nums = Arrays.stream(Arrays.copyOfRange(tokens, 1, tokens.length))
.mapToDouble(Double::parseDouble)
.toArray();
return new Particle(tokens[0], nums[0], nums[1], nums[2], nums[3], nums[4]);
}
/**
* @name name of the particle (useful for debugging)
* @param x x-coordinate of the particle
* @param y y-coordinate of the particle
* @param vx x-velocity of the particle
* @param vy y-velocity of the particle
* @param radius radius of the particle
*/
Particle (String name, double x, double y, double vx, double vy, double radius) {
_name = name;
_x = x;
_y = y;
_vx = vx;
_vy = vy;
_radius = radius;
}
/**
* Draws the particle as a filled circle.
* DO NOT MODIFY THIS METHOD
*/
void draw (Graphics g) {
g.fillOval((int) (_x - _radius), (int) (_y - _radius), (int) (2*_radius), (int) (2*_radius));
}
/**
* Useful for debugging.
*/
public String toString () {
return (_name.equals("") ? "" : _name + " ") + _x + " " + _y + " " + _vx + " " + _vy + " " + _radius;
}
/**
* Updates the position of the particle after an elapsed amount of time, delta, using
* the particle's current velocity.
* @param delta the elapsed time since the last particle update
*/
public void update (double delta) {
double newX = _x + delta * _vx;
double newY = _y + delta * _vy;
_x = newX;
_y = newY;
}
/**
* Updates this particle's velocity after a collision with a wall.
* @param w the Wall that it hit
* @param now the current time in the simulation
*/
public void updateAfterWalls (Wall w, double now) {
if (w.getSide().equals("Left") || w.getSide().equals("Right")) {
_vx *= -1;
}
else if (w.getSide().equals("Top") || w.getSide().equals("Bottom")) {
_vy *= -1;
}
_lastUpdateTime = now;
}
/**
* Updates both this particle's and another particle's velocities after a collision between them.
* DO NOT CHANGE THE MATH IN THIS METHOD
* @param now the current time in the simulation
* @param other the particle that this one collided with
*/
public void updateAfterCollision (double now, Particle other) {
double vxPrime, vyPrime;
double otherVxPrime, otherVyPrime;
double common = ((_vx - other._vx) * (_x - other._x) +
(_vy - other._vy) * (_y - other._y)) /
(Math.pow(_x - other._x, 2) + Math.pow(_y - other._y, 2));
vxPrime = _vx - common * (_x - other._x);
vyPrime = _vy - common * (_y - other._y);
otherVxPrime = other._vx - common * (other._x - _x);
otherVyPrime = other._vy - common * (other._y - _y);
_vx = vxPrime;
_vy = vyPrime;
other._vx = otherVxPrime;
other._vy = otherVyPrime;
_lastUpdateTime = now;
other._lastUpdateTime = now;
}
/**
* Computes and returns the time when (if ever) this particle will collide with the wall,
* or infinity if the particle doesn't collide with the wall given its current velocities.
* @param w the Wall to consider
* @param width the width/height of the screen
* @return the time with the particles will collide with the wall, or infinity if they will never collide
*/
public double getWallCollisionTime(Wall w, int width) {
if (w.getSide().equals("Left") && _vx < 0) {
return (0 - _x + _radius) / _vx;
}
else if (w.getSide().equals("Right") && _vx > 0) {
return (width - _x - _radius) / _vx;
}
else if(w.getSide().equals("Top") && _vy < 0) {
return (0 - _y + _radius) / _vy;
}
else if(w.getSide().equals("Bottom") && _vy > 0) {
return (width - _y - _radius) / _vy;
}
return Double.POSITIVE_INFINITY;
}
/**
* Computes and returns the time when (if ever) this particle will collide with another particle,
* or infinity if the two particles will never collide given their current velocities.
* DO NOT CHANGE THE MATH IN THIS METHOD
* @param other the other particle to consider
* @return the time with the particles will collide, or infinity if they will never collide
*/
public double getCollisionTime (Particle other) {
// See https://en.wikipedia.org/wiki/Elastic_collision#Two-dimensional_collision_with_two_moving_objects
double a = _vx - other._vx;
double b = _x - other._x;
double c = _vy - other._vy;
double d = _y - other._y;
double r = _radius;
double A = a*a + c*c;
double B = 2 * (a*b + c*d);
double C = b*b + d*d - 4*r*r;
// Numerically more stable solution to QE.
// https://people.csail.mit.edu/bkph/articles/Quadratics.pdf
double t1, t2;
if (B >= 0) {
t1 = (-B - Math.sqrt(B*B - 4*A*C)) / (2*A);
t2 = 2*C / (-B - Math.sqrt(B*B - 4*A*C));
} else {
t1 = 2*C / (-B + Math.sqrt(B*B - 4*A*C));
t2 = (-B + Math.sqrt(B*B - 4*A*C)) / (2*A);
}
// Require that the collision time be slightly larger than 0 to avoid
// numerical issues.
double SMALL = 1e-6;
double t;
if (t1 > SMALL && t2 > SMALL) {
t = Math.min(t1, t2);
} else if (t1 > SMALL) {
t = t1;
} else if (t2 > SMALL) {
t = t2;
} else {
// no collision
t = Double.POSITIVE_INFINITY;
}
return t;
}
/* Getters */
public String getName () {
return _name;
}
public double getX () {
return _x;
}
public double getY () {
return _y;
}
public double getVx () {
return _vx;
}
public double getVy () {
return _vy;
}
public double getRadius () {
return _radius;
}
public double getLastUpdateTime() {
return _lastUpdateTime;
}
}