-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniverse.java
More file actions
291 lines (200 loc) · 6.91 KB
/
Universe.java
File metadata and controls
291 lines (200 loc) · 6.91 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
*
* @author Alex Weeks
*
*/
public class Universe extends JPanel implements Runnable {
protected ArrayList<Ball> actors = new ArrayList<Ball>();
protected double absoluteTime = 0;
protected double timeStep = 0.04;
protected DoubleVector gravity;
protected JLabel timeField;
protected double drawXScale = 1;
protected double drawYScale = 1;
protected Collision nextCollision;
//Upper and lower bounds for the region, a wall collision will be done when these are hit
DoubleVector lowerBounds = new DoubleVector( new double[] {0,0} );
DoubleVector upperBounds = new DoubleVector( new double[] {700, 650} );
/**
* Constructs a new Universe
* @param gravity Gravity acceleration vector
*/
public Universe( DoubleVector gravity ) {
this.gravity = gravity;
this.timeField = new JLabel();
this.timeField.setLocation(360, 660);
this.timeField.setText( Double.toString(this.absoluteTime ) );
this.timeField.setSize(100, 50);
this.timeField.setVisible(true);
this.timeField.setForeground(Color.WHITE);
}
/**
* Adds a new ball to the Universe
* @param ball The ball to add
*/
public void addActor( Ball ball ) {
this.add(ball);
ball.setLocation(ball.getX(), ball.getY());
ball.setSize(ball.getDrawWidth(), ball.getDrawHeight());
ball.setLayout(null);
ball.setVisible(true);
this.actors.add( ball );
//With the new actor calculate the next collision and store it
this.nextCollision = this.nextCollision();
this.refresh();
}
/**
* Adds randomized balls to random locations in the Universe. Checks to ensure a new ball is not placed inside another.
* @param num Number of balls to add
*/
public void addRandomActors( int num ) {
Random rnd = new Random();
for( int k = 0; k < num; k ++) {
//Radius in the range 25...55
double r = rnd.nextDouble() * 20 + 10;
//Something reasonable for the mass (In 2D at least...)
double m = r * r;
double xRange = this.upperBounds.comps[0]-this.lowerBounds.comps[0];
double yRange = this.upperBounds.comps[1]-this.lowerBounds.comps[1];
boolean inside;
DoubleVector pos;
DoubleVector vel = new DoubleVector( new double[] {rnd.nextDouble() * 300 - 150, rnd.nextDouble() * 300 - 150});
do{
//Create a new x and y in a reasonable range (at least 20 units from the edge)
double x = this.lowerBounds.comps[0] + rnd.nextDouble() * (xRange - 2*r - 40) + r + 20;
double y = this.lowerBounds.comps[1] + rnd.nextDouble() * (yRange - 2*r - 40) + r + 20;
pos = new DoubleVector( new double[] {x,y} );
//Check to make sure we're not putting the ball inside another one
inside = false;
for( Ball ball : actors ) {
if (ball.pos.subtract(pos).magnitude() < r + 30) {
inside = true;
}
}
} while( inside );
Ball newBall = new Ball(m, r, pos , vel);
this.addActor( newBall);
}
}
/**
* Repaints the Universe
*/
public void refresh() {
this.timeField.setText( Double.toString(this.absoluteTime ) );
this.paintImmediately(0, 0, this.getWidth(), this.getHeight());
}
/**
* @return Returns a string representation of the Universe
*/
public String toString() {
StringBuffer result = new StringBuffer();
result.append( "Time: " + this.absoluteTime + "\n");
for ( int n = 0; n < this.actors.size(); n++ ) {
result.append("Actor " + n + ": " + this.actors.get(n).toString() + "\n");
}
return result.toString();
}
/**
* Runs the Universe for the specified ammount of time
* @param time
*/
public void runFor( double time ) {
if ( time < 0 ) throw new IllegalArgumentException("Negative time specified");
double timeRemaining = time;
while( this.nextCollision != null ) {
//If the next collision is within our time frame, do it
if ( this.nextCollision.deltaT <= time ) {
//Update every actor's position to be the moment of the collision
this.updatePos( nextCollision.deltaT );
//Subtract time
timeRemaining -= nextCollision.deltaT;
//Perform the collision calculation, updating the velocity vectors of the objects.
nextCollision.doCollision();
//System.out.println("collision at t + " + this.absoluteTime + "seconds\n");
//System.out.println(this);
//Perform the next collision calculation
this.nextCollision = this.nextCollision();
}
//If the collision is too far into the future, break
else break;
}
//There are no more collisions in the time remaining, update everybody's position.
this.updatePos(timeRemaining);
if (this.nextCollision != null ) this.nextCollision.deltaT -= timeRemaining;
this.refresh();
}
/**
* Integrates and updates positions of all actors without regard for collisions for deltaT time from the current absolute time. Updates current absolute time.
* @param deltaT Time interval to integrate over
*/
public void updatePos( double deltaT ) {
for( Ball ball : this.actors ) {
//new position: p = p0 + v * t
ball.pos.addTo( ball.vel.scalarMult( deltaT ) );
//new position = p0 + 1/2 a * t^2
ball.pos.addTo( this.gravity.scalarMult( deltaT * deltaT / 2 ) );
//new velocity: v = v0 + a * t
ball.vel.addTo( this.gravity.scalarMult( deltaT));
}
this.absoluteTime += deltaT;
}
/**
* Calculates the exact deltaT (as limited by the precision of double arithmaic) from the current absolute time to the next collision between actors, or between an actor and a wall.
* @return Returns a new Collision object containing the calculated deltaT and references to both colliding objects, or a single object and a wall.
*/
public Collision nextCollision( ) {
//Find collision routine
ArrayList<Collision> collisions = new ArrayList<Collision>();
Collision check;
for ( Ball ball1 : this. actors) {
for ( Ball ball2 : this. actors) {
//Don't check for collisions with self
if ( ball1 == ball2 ) continue;
check = Physics.checkCollision(ball1, ball2);
if ( check != null ) {
collisions.add(check);
}
}
check = Physics.checkWallCollision(ball1, lowerBounds, upperBounds, gravity);
if ( check != null ) {
collisions.add(check);
}
}
//If there are no collisions, return null
if ( collisions.size() == 0 ) {
return null;
}
//If there are, find the soonest one, and return it
Collision result = collisions.get(0);
for( Collision next : collisions ) {
if( next.deltaT < result.deltaT ) result = next;
}
//System.out.println("collisions caculated");
return result;
}
/**
* Run the simulation and display it
*/
public void run() {
boolean cont = true;
while ( cont ) {
this.runFor(this.timeStep);
try {Thread.sleep(20);} catch (InterruptedException e) {
cont = false;
}
}
}
/**
* The answer to life, the Universe and everything
* @return Returns the answer
*/
public int answer() {
return 42;
}
}