-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulation.pde
334 lines (286 loc) · 8.65 KB
/
Simulation.pde
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
class Simulation {
ArrayList<CollidableWall> fieldPerimeter;
ArrayList<CollidableWall> boilerLeft;
ArrayList<CollidableWall> boilerRight;
ArrayList<CollidableWall> airshipLeft;
ArrayList<CollidableWall> airshipRight;
CollidableWall ulHopper;
CollidableWall urHopper;
CollidableWall llHopper;
CollidableWall lmHopper;
CollidableWall lrHopper;
ArrayList<Fuel> ballList;
PShape normals;
PShape ballsShape;
int particleCount = 50;
Simulation() {
normals = createShape(GROUP);
initializeField();
initializeAirships();
initializeBalls();
}
void initializeField() {
fieldPerimeter = new ArrayList<CollidableWall>();
PVector twL = new PVector(twlX, twY);
PVector twR = new PVector(twrX, twY);
PVector lwT = new PVector(lwX, swtY);
PVector lwB = new PVector(lwX, swbY);
PVector rwT = new PVector(rwX, swtY);
PVector rwB = new PVector(rwX, swbY);
PVector bwL = new PVector(bwlX, bwY);
PVector bwR = new PVector(bwrX, bwY);
addCollidable(twR, twL, true, fieldPerimeter);
addCollidable(twL, lwT, true, fieldPerimeter);
addCollidable(lwT, lwB, true, fieldPerimeter);
addCollidable(lwB, bwL, true, fieldPerimeter);
addCollidable(bwL, bwR, true, fieldPerimeter);
addCollidable(bwR, rwB, true, fieldPerimeter);
addCollidable(rwB, rwT, true, fieldPerimeter);
addCollidable(rwT, twR, true, fieldPerimeter);
for (CollidableWall c : fieldPerimeter) {
normals.addChild(c.getShape());
}
ulHopper = new CollidableWall(new PVector(465, 59), new PVector(414, 59), true);
urHopper = new CollidableWall(new PVector(864, 59), new PVector(814, 59), true);
llHopper = new CollidableWall(new PVector(321, 583), new PVector(270, 583), false);
lmHopper = new CollidableWall(new PVector(666, 583), new PVector(614, 583), false);
lrHopper = new CollidableWall(new PVector(1008, 583), new PVector(958, 583), false);
}
void initializeAirships() {
airshipLeft = new ArrayList<CollidableWall>();
airshipRight = new ArrayList<CollidableWall>();
PVector lT = new PVector(hexLM, hexTopY);
PVector llmT = new PVector(hexLL, hexTopMidY);
PVector llmB = new PVector(hexLL, hexBotMidY);
PVector lB = new PVector(hexLM, hexBotY);
PVector lrmB = new PVector(hexLR, hexBotMidY);
PVector lrmT = new PVector(hexLR, hexTopMidY);
addCollidable(lT, llmT, false, airshipLeft);
addCollidable(llmT, llmB, false, airshipLeft);
addCollidable(llmB, lB, false, airshipLeft);
addCollidable(lB, lrmB, false, airshipLeft);
addCollidable(lrmB, lrmT, false, airshipLeft);
addCollidable(lrmT, lT, false, airshipLeft);
for (CollidableWall c : airshipLeft) {
normals.addChild(c.getShape());
}
PVector rT = new PVector(hexRM, hexTopY);
PVector rlmT = new PVector(hexRL, hexTopMidY);
PVector rlmB = new PVector(hexRL, hexBotMidY);
PVector rB = new PVector(hexRM, hexBotY);
PVector rrmB = new PVector(hexRR, hexBotMidY);
PVector rrmT = new PVector(hexRR, hexTopMidY);
addCollidable(rT, rlmT, false, airshipRight);
addCollidable(rlmT, rlmB, false, airshipRight);
addCollidable(rlmB, rB, false, airshipRight);
addCollidable(rB, rrmB, false, airshipRight);
addCollidable(rrmB, rrmT, false, airshipRight);
addCollidable(rrmT, rT, false, airshipRight);
for (CollidableWall c : airshipRight) {
normals.addChild(c.getShape());
}
}
void initializeBalls() {
ballsShape = createShape(GROUP);
ballList = new ArrayList<Fuel>();
//for (int i = 0; i < particleCount; i++) {
// addRandomBall();
//}
}
void resetFuel() {
ballList = new ArrayList<Fuel>();
ballsShape = createShape(GROUP);
hopperUL = false;
hopperUR = false;
hopperLL = false;
hopperLM = false;
hopperLR = false;
}
void addCollidable(PVector p1, PVector p2, boolean negNormal, ArrayList<CollidableWall> coll) {
CollidableWall c = new CollidableWall(p1, p2, negNormal);
coll.add(c);
}
float spread = 100;
float entropy = 200;
void addRandomBall() {
PVector randomPos = PVector.random2D().mult(spread);
PVector randomVel = PVector.random2D().mult(entropy);
//println("Velocity: " + randomVel.x + "," + randomVel.y);
Fuel b = new Fuel(randomPos.add(center), randomVel, 3);
ballList.add(b);
ballsShape.addChild(b.getShape());
}
void updateBalls() {
for (Fuel b : ballList) {
b.update();
for (Fuel c : ballList) {
if (b.onGround && c.onGround) {
if (b.collides(c)) {
if (c.velocity.x != b.velocity.x && c.velocity.y != b.velocity.y) {
b.fixCollision(c);
c.bounce(b);
b.bounce(c);
}
}
}
}
fixCollisionsWalls(b, fieldPerimeter);
if (b.onGround) {
fixCollisionsWalls(b, airshipLeft);
fixCollisionsWalls(b, airshipRight);
fixCollisionsWalls(b, r.walls);
}
if (b.location.y < 52) {
float dist = 52 - b.location.y;
b.move(new PVector(0, dist + 3));
}
if (b.location.y > 589) {
float dist = 589 - b.location.y;
b.move(new PVector(0, dist - 3));
}
}
}
void addBall(PVector location, PVector velocity) {
Fuel b = new Fuel(location, velocity, 3);
ballList.add(b);
ballsShape.addChild(b.getShape());
}
void addFlyingBall(PVector location, PVector velocity) {
Fuel b = new Fuel(location, velocity, 3);
b.makeFlying();
ballList.add(b);
ballsShape.addChild(b.getShape());
}
float hopperIntensity = 300;
float hopperSpread = 20;
boolean hopperUL = false;
boolean hopperUR = false;
boolean hopperLL = false;
boolean hopperLM = false;
boolean hopperLR = false;
void dumpHopper(int i) {
PVector leftCenter = new PVector(0, 0);
PVector rightCenter = new PVector(0, 0);
boolean top = true;
boolean dump = false;
switch(i) {
case 0:
leftCenter = new PVector(392, 60);
rightCenter = new PVector(486, 60);
top = true;
if (!hopperUL) {
dump = true;
}
hopperUL = true;
break;
case 1:
leftCenter = new PVector(790, 60);
rightCenter = new PVector(885, 60);
top = true;
if (!hopperUR) {
dump = true;
}
hopperUR = true;
break;
case 2:
leftCenter = new PVector(247, 584);
rightCenter = new PVector(341, 584);
top = false;
if (!hopperLL) {
dump = true;
}
hopperLL = true;
break;
case 3:
leftCenter = new PVector(591, 584);
rightCenter = new PVector(686, 584);
top = false;
if (!hopperLM) {
dump = true;
}
hopperLM = true;
break;
case 4:
leftCenter = new PVector(934, 584);
rightCenter = new PVector(1028, 584);
top = false;
if (!hopperLR) {
dump = true;
}
hopperLR = true;
break;
default:
}
if (dump) {
for (int j = 0; j < 50; j++) {
float angle = random(10*PI/32, 20*PI/32);
if (!top) {
angle -= PI;
}
PVector randAngle = PVector.fromAngle(angle);
addBall(PVector.add(leftCenter, PVector.mult(randAngle, hopperSpread)), PVector.mult(randAngle, hopperIntensity));
angle = random(10*PI/32, 20*PI/32);
if (!top) {
angle -= PI;
}
randAngle = PVector.fromAngle(angle);
addBall(PVector.add(rightCenter, PVector.mult(randAngle, hopperSpread)), PVector.mult(randAngle, hopperIntensity));
}
}
}
void dumpHopperUL() {
dumpHopper(0);
}
void dumpHopperUR() {
dumpHopper(1);
}
void dumpHopperLL() {
dumpHopper(2);
}
void dumpHopperLM() {
dumpHopper(3);
}
void dumpHopperLR() {
dumpHopper(4);
}
void fixCollisionsWalls(Fuel b, ArrayList<CollidableWall> cw) {
for (CollidableWall c : cw) {
if (b.collides(c)) {
b.fixCollision(c);
b.bounce(c);
}
}
}
void drawNormals2() {
for (CollidableWall w : fieldPerimeter) {
shape(w.getShape());
}
for (CollidableWall w : airshipLeft) {
shape(w.getShape());
}
for (CollidableWall w : airshipRight) {
shape(w.getShape());
}
}
void drawBalls() {
shape(ballsShape);
}
PShape getBallsShape() {
return ballsShape;
}
void drawNormals() {
shape(normals);
}
PShape getNormals() {
return normals;
}
ArrayList<ArrayList<CollidableWall>> getFieldCollides() {
ArrayList<ArrayList<CollidableWall>> l = new ArrayList<ArrayList<CollidableWall>>();
l.add(fieldPerimeter);
//l.add(boilerLeft);
//l.add(boilerRight);
l.add(airshipLeft);
l.add(airshipRight);
return l;
}
}