-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyWorld.java
219 lines (198 loc) · 4.99 KB
/
MyWorld.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* MyWorld class
* @author Pedro Espinoza, Luis Ojeda, Felipe Veas
*/
import java.util.*;
import java.io.*;
import javax.swing.Timer;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class MyWorld implements ActionListener {
private PrintStream out;
private ArrayList<PhysicsElement> elements; // array to hold everything in my world.
private MyWorldView view; // NEW
private Timer passingTime; // NEW
private double t; // simulation time
private double delta_t; // in seconds
private double refreshPeriod; // in seconds
private double gravity;
private ArrayList<PhysicsElement> inpos;
/**
* Constructor Myworld. Initialize a new physic World.
*/
public MyWorld() {
this(System.out, -9.8);
}
/**
* Constructor Myworld. Initialize a new physic World with custom params.
* @param output text out
*/
public MyWorld(PrintStream output) {
this(output, -9.8);
}
/**
* Constructor Myworld. Initialize a new physic World with custom params.
* @param output text out
* @param gravity World's gravity
*/
public MyWorld(PrintStream output, double gravity) {
view = null;
this.gravity = gravity;
out = output;
t = 0;
refreshPeriod = 0.06; // 60.00 [ms]
delta_t = 0.00001; // 0.01 [ms]
elements = new ArrayList<PhysicsElement>();
inpos = new ArrayList<PhysicsElement>();
passingTime = new Timer((int)(refreshPeriod*1000), this);
}
/**
* Get gravity value from this world.
* @return MyWorld's gravity
*/
public double getGravity(){
return gravity;
}
/**
* Set gravity value in this world.
* @param gr Gravity's value
*/
public void setGravity(double gr){
gravity = gr;
}
/**
* Add a new physic element.
* @param e Physic element
*/
public void addElement(PhysicsElement e) {
elements.add(e);
view.repaintView();
}
/**
* Asociate a graphic world class.
* @param view graphic world
*/
public void setView(MyWorldView view) {
this.view = view;
}
/**
* Set delta time in this world.
* @param delta asign delta
*/
public void setDelta_t(double delta) {
delta_t = delta;
}
/**
* Set sample-rate in this world.
* @param rp sample-rate value
*/
public void setRefreshPeriod(double rp) {
refreshPeriod = rp;
passingTime.setDelay((int)(refreshPeriod*1000)); // convert from [s] to [ms]
}
/**
* Start simulation.
*/
public void start() {
if (passingTime.isRunning())
return;
passingTime.start();
view.desableMouseListener();
}
/**
* Stop Simulation.
*/
public void stop() {
passingTime.stop();
view.enableMouseListener();
}
/**
* Simulate iterative way.
* @param event param for listener
*/
public void actionPerformed(ActionEvent event) {
/*
* Like simulate method of Assignment 1,
* The arguments are attributes here.
*/
double nextStop = t + refreshPeriod;
for (; t<nextStop; t += delta_t) {
// Compute each element next state based on current global state
for (PhysicsElement e: elements) {
if (e instanceof Simulateable) {
Simulateable s = (Simulateable) e;
s.computeNextState(delta_t,this);
}
}
// For each element update its state
for (PhysicsElement e: elements) {
if (e instanceof Simulateable) {
Simulateable s = (Simulateable) e;
s.updateState();
repaintView();
}
}
}
}
/**
* Update graphic world.
*/
public void repaintView() {
view.repaintView();
}
/**
* Check that elements is colliding with something.
* @return Ball colliding
* @param me consulting ball
*/
public SpringAttachable findCollidingBall(SpringAttachable me) {
for (PhysicsElement e: elements)
if ((e instanceof Ball) || (e instanceof Block)) {
SpringAttachable b = (SpringAttachable)e;
if ((b!=me) && b.collide(me)) return b;
}
return null;
}
/**
* Check elements asociated with elastic elements.
* @return Foundnd attachable elements
* @param s Spring source
*/
public SpringAttachable findAttachableElement(Elastic s){
for (PhysicsElement e: elements) {
if (e instanceof SpringAttachable) {
if (s.isAattachedTo((SpringAttachable)e)) continue;
if (s.isBattachedTo((SpringAttachable)e)) continue;
double pos_e = ((SpringAttachable)e).getPosition();
double rad = ((SpringAttachable)e).getRadius();
double posA = s.getAendPosition();
double posB = s.getBendPosition();
if (((posA > pos_e - rad) && (posA < pos_e + rad)) || ((posB > pos_e - rad) && (posB < pos_e + rad)))
return (SpringAttachable) e;
}
}
return null;
}
/**
* Get all physic elements asociated with this world.
* @return Elements from this world
*/
public ArrayList<PhysicsElement> getPhysicsElements() {
return elements;
}
/**
* Get all physic elements in (x,y) point.
* @return Elements in a point
* @param x x position
* @param y y position
*/
public ArrayList<PhysicsElement> find(double x, double y) {
inpos.clear();
for (PhysicsElement e: elements) {
if (e.contains(x,y)) inpos.add(e);
}
return inpos;
}
}