Repository navigation
Expand file tree
/
Copy pathEcosystem_Sim.pde
More file actions
464 lines (355 loc) · 9.76 KB
/
Copy pathEcosystem_Sim.pde
File metadata and controls
464 lines (355 loc) · 9.76 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
This is a simulation of an Ecosystem on a 2d plane. The purpose of this project is to use many different algorithms, libaries,
etc, to simulate creatures interacting with each other in an environment. This project is being made using the 'Nature of Code'
textbook as a guideline. The additions added will follow the chapters of the book.
The frog:
Jumps in random directions by a random distance, pauses and jumps again.
The butterfly:
Constantly loops as it flies
The snake:
Follows a curve by moving in a wave like motion
Fish:
Moves on really long smoothed curves.
Optimisations:
-Turn mag into magsq
-Precalculate cos and sin
-Remove unncessary creation of vectors outside of loops
*/
import java.util.*;
import shiffman.box2d.*;
import org.jbox2d.common.*;
import toxi.physics2d.*;
import toxi.physics2d.behaviors.*;
import toxi.geom.*;
import g4p_controls.*;
//Constants
public static final PVector FOOD_COLOUR = new PVector(255,165,0);
public static final PVector HERB_COLOUR = new PVector(0,255,0);
public static final PVector CARN_COLOUR = new PVector(255,0,0);
public static final float SOFT_BORDER = 40;
//Program Specifications
int width = 1024;
int height = 768;
int bg[][];
float perlin; //So creatures get different numbers
//Wind
PVector wind;
PVector currentWind;
float windRate = 0.01;
Random random;
//Global Game vaariables
int blC, blR;
float creatureScale = 1;
float gridRes;
ArrayList<Object> worldObjs;
ArrayList<Terrain> terrain;
ArrayList<ParticleSystem> systems;
ArrayList<Object>[][] binlatticeGrid;
Box2DProcessing box2d;
VerletPhysics2D physics;
//Rates
float creatureSpawnRate = 5;
float foodTime;
//Timers
float currentFoodTime;
//UI
GWindow wdwControls;
GButton btnPlay;
GButton btnReset;
GButton btnQuit;
GButton btnDebug;
//Testing Variables
TestCreature tc;
//Simulation States
boolean playing = true;
boolean debug = false;
//Temp
color flowerColour;
void settings() {
size(width, height);
}
Tree testTree;
void setup(){
background(220);
testTree = new Tree(width/2, height/2);
random = new Random();
worldObjs = new ArrayList<Object>();
terrain = new ArrayList<Terrain>();
systems = new ArrayList<ParticleSystem>();
//Bin-lattice setup
gridRes = 64;
blC = (int)Math.ceil(width/gridRes);
blR = (int)Math.ceil(height/gridRes);
binlatticeGrid = new ArrayList[blC][blR];
for (int i = 0; i < blC; i++) {
for (int j = 0; j < blR; j++) {
binlatticeGrid[i][j] = new ArrayList<Object>();
}
}
//Box2D set up
box2d = new Box2DProcessing(this);
box2d.createWorld();
box2d.listenForCollisions();
box2d.setGravity(0,0);
//Toxiclibs
physics = new VerletPhysics2D();
physics.setWorldBounds(new Rect(0,0,width,height));
//Wind
wind = new PVector(getPerlin(), getPerlin());
currentWind = new PVector();
//GUI
createGUI();
//BG Setup
float xoff = 0.0;
bg = new int[width][height];
for (int x = 0; x < width; x++) {
float yoff = 0.0;
for (int y = 0; y < height; y++) {
float bright = noise(xoff,yoff);
if(bright < 0.3){
bg[x][y] = color(40*bright,30*bright,10*bright);
}
else
bg[x][y] = color(30*bright,40*bright,10*bright);
yoff += 0.009;
}
xoff += 0.009;
}
//Simulation Spawning
spawnSpider();
AntHill antHill = new AntHill(random(width/5,width*4/5),random(height/5,4*height/5));
terrain.add(antHill);
terrain.add(new AntHill(random(width/5,width*4/5),random(height/5,4*height/5)));
//Test declarations
tc = new TestCreature(0,0,1, new PVector(255,0,0));
worldObjs.add(tc);
flowerColour = color(random(85,170),random(85,170),random(85,170));
worldObjs.add(new Shellman(100,100, 1, FOOD_COLOUR));
}
void draw(){
//Draw Background with Noise
loadPixels();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
pixels[x+y*width] = bg[x][y];
}
}
updatePixels();
//Update wind
wind.x = wind.x+windRate;
wind.y = wind.y+windRate;
currentWind.x = map(noise(wind.x),0,1,-1,1);
currentWind.y = map(noise(wind.y),0,1,-1,1);
//Update if playing
if(playing){
for(Terrain t : terrain)
t.update();
//Set up bin lattice
clearBinlatticeGrid();
for (Object o : worldObjs) {
ArrayList<Object> gridList = o.locInGrid();
if(gridList != null)
o.locInGrid().add(o);
}
//Update physics worlds
box2d.step();
physics.update();
//Update timers
foodTime+= 0.01;
}
//Add new food
if(foodTime > Food.SPAWN_RATE){
spawnFood();
foodTime -= Food.SPAWN_RATE;
}
//Display terrain under objects
for(Terrain t : terrain)
t.display();
//Iterate through objects to update and destroy
Iterator<Object> it = worldObjs.iterator();
Vec2 creatureLoc;
while(it.hasNext()){
Object object = it.next();
//Check to see if eaten
if(object.isDestroyed){
creatureLoc = object.getPos();
ParticleSystem ps = new ParticleSystem();
ps.origin.set(creatureLoc.x,creatureLoc.y);
systems.add(ps);
for(int i = 0;i<=25;i++)
ps.addParticle();
object.destroy();
it.remove();
continue;
}
//If not destroyed, update and display.
if(playing)
object.update();
object.display();
}
testTree.update();
testTree.display();
//Run custom simple particleSystem
Iterator<ParticleSystem> itps = systems.iterator();
ParticleSystem ps = null;
while(itps.hasNext()){
ps = itps.next();
ps.run();
if(ps.isEmpty()){
itps.remove();
}
}
/*
pushMatrix();
translate(width*2/3,height/3);
int s = 3;
fill(flowerColour);
stroke(50);
for(int i=0;i <6;i++){
ellipse(cos(2*PI/6*(i+1))*9*s,sin(2*PI/6*(i+1))*9*s,10*s,10*s);
}
fill(255,255,224);
stroke(255,255,224);
ellipse(0,0,10*s,10*s);
popMatrix();
*/
if(!debug)
return;
//Draw bin-lattice
stroke(255,120,120);
fill(255,120,120);
textSize(20);
float halfRes = gridRes/2;
for (int i = 0; i < blC; i++) {
if(i!=blC-1)
line((i+1)*gridRes, 0, (i+1)*gridRes, height);
for (int j = 0; j < blR; j++) {
if(i==0 && j!= blR-1)
line(0, (j+1)*gridRes, width, (j+1)*gridRes);
text(binlatticeGrid[i][j].size(),i*gridRes+halfRes,j*gridRes+halfRes);
}
}
//Draw terrain debug
for(Terrain t : terrain){
if(t instanceof Web){
Web w = (Web)t;
textSize(36);
fill(255,230,230);
text(w.inside.size(), w.pos.x, w.pos.y);
}
}
}
///
/// GUI
///
void createGUI(){
wdwControls = GWindow.getWindow(this, "My Window", 100, 50, 480, 70, JAVA2D);
btnPlay = new GButton(wdwControls, 10, 10, 50, 50, ">");
btnReset = new GButton(wdwControls, 70, 10, 50, 50, "Reset");
btnQuit = new GButton(wdwControls, 130, 10, 50, 50, "Quit");
btnDebug = new GButton(wdwControls, 190, 10, 50, 50, "Debug");
}
void handleButtonEvents(GButton button, GEvent event) {
if (button == btnPlay && event == GEvent.CLICKED) {
playing = !playing;
println(playing ? "Playing" : "Paused");
}
if (button == btnReset && event == GEvent.CLICKED) {
resetSimulation();
println("Simulation reset.");
}
if (button == btnQuit && event == GEvent.CLICKED) {
exit();
}
if (button == btnDebug && event == GEvent.CLICKED) {
debug = !debug;
println(playing ? "Debug: On" : "Debug: Off");
}
}
//Resets the currently active simulation
void resetSimulation(){
//purgeCreatures();
worldObjs.clear();
terrain.clear();
systems.clear();
clearBinlatticeGrid();
}
private void clearBinlatticeGrid(){
for (int i = 0; i < blC; i++) {
for (int j = 0; j < blR; j++) {
binlatticeGrid[i][j].clear();
}
}
}
///
///OBJECT HANDLING
///
//destroys all creatures
void purgeCreatures(){
for(Object o : worldObjs){
if(o instanceof Creature){
o.isDestroyed = true;
}
}
}
//Spawns one food item
void spawnFood(){
println("Spawning: Food");
worldObjs.add(new Food(random(SOFT_BORDER,width-SOFT_BORDER),random(SOFT_BORDER,height-SOFT_BORDER), 1, FOOD_COLOUR));
}
//Spawns a Spider
void spawnSpider(){
println("Spawning: Spider");
Spider spider = new Spider(random(width/5,width*4/5),random(height/5,4*height/5),3,HERB_COLOUR);
worldObjs.add(spider);
}
///
///BOX 2D
///
void beginContact(Contact cp){
Fixture f1 = cp.getFixtureA();
Fixture f2 = cp.getFixtureB();
Body b1 = f1.getBody();
Body b2 = f2.getBody();
checkContact(b1,b2);
checkContact(b2,b1);
}
void endContact(Contact cp){
Fixture f1 = cp.getFixtureA();
Fixture f2 = cp.getFixtureB();
Body b1 = f1.getBody();
Body b2 = f2.getBody();
checkEndContact(b1,b2);
checkEndContact(b2,b1);
}
//Use this on two different objects, check to see if there is a collision
void checkContact(Body o1, Body o2){
//CREATURE > WEB
if(o1.getUserData() instanceof Object && o2.getUserData() instanceof Web){
((Web)o2.getUserData()).addInside((Object)o1.getUserData());
return;
}
//SPIDER > ANT
if(o1.getUserData() instanceof Spider && o2.getUserData() instanceof Ant){
((Creature)o2.getUserData()).isDestroyed = true;
((Spider)o1.getUserData()).hunger = 0;
return;
}
//ANT > FOOD
if(o1.getUserData() instanceof Ant && o2.getUserData() instanceof Food){
((Food)o2.getUserData()).isDestroyed = true;
((Ant)o1.getUserData()).hunger = 0;
//((Ant)o1.getUserData()).foundFood = null;
return;
}
}
//Use these to check both objects that have finished colliding
void checkEndContact(Body o1, Body o2){
if(o1.getUserData() instanceof Object && o2.getUserData() instanceof Web){
((Web)o2.getUserData()).removeInside((Object)o1.getUserData());
return;
}
}
float getPerlin(){
return perlin+=1000;
}