-
Notifications
You must be signed in to change notification settings - Fork 0
/
verlet2DTest.pde
201 lines (156 loc) · 2.92 KB
/
verlet2DTest.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
import toxi.geom.*;
import toxi.physics2d.*;
import toxi.physics2d.behaviors.*;
import toxi.physics2d.constraints.*;
import controlP5.*;
import java.util.*;
ControlP5 ctrl;
VerletPhysics2D physics;
CreatureManager cm;
Flowfield grid;
ArrayList<Food> foods;
PFont font;
boolean HUDflag;
boolean GRIDflag;
void setup()
{
<<<<<<< HEAD
size(800,600, P2D);
// size(displayWidth, displayHeight, P3D);
=======
size(800,600, P3D);
//size(displayWidth, displayHeight, P2D);
hint(DISABLE_DEPTH_MASK);
>>>>>>> depth check disabled
font = loadFont("Consolas-14.vlw");
cm = new CreatureManager();
colorMode(HSB);
initFood();
initPhysics();
initHUD();
initGui();
grid = new Flowfield(10);
GRIDflag = false;
}
void draw()
{
frame.setTitle("Fps: "+(int)frameRate);
background(0);
if(GRIDflag)
{
grid.display();
}
physics.update();
cm.run(grid);
cm.display();
if(cm.isGrown())
{
addNewCellsToSystem();
}
if(HUDflag)
{
viewHUD();
}
Iterator<Food> foodIterator = foods.iterator();
while(foodIterator.hasNext())
{
Food f = foodIterator.next();
f.update();
f.display();
if(f.age<0)
{
foodIterator.remove();
println("Removed!");
}
}
}
void initFood()
{
foods = new ArrayList<Food>();
}
void initGui()
{
ctrl = new ControlP5(this);
//ControlWindow cw = ctrl.addControlWindow("Control Panel", 300,300);
//cw.hideCoordinates();
}
void initHUD()
{
HUDflag = true;
}
void viewHUD()
{
fill(200);
textFont(font, 14);
text("Creature count: "+cm.getCreatureCount()+"\nTotal cell count: "+cm.getTotalCellCount(), 5,14);
}
void addNewCellsToSystem()
{
for(Cell c : cm.newCells)
{
physics.addParticle(c);
AttractionBehavior repel = new AttractionBehavior(c, 10, -2.0f);
physics.addBehavior(repel);
}
for(VerletSpring2D s : cm.newBonds)
{
physics.addSpring(s);
}
//println(cm.newCells.size()+" cells added on this epoch!");
}
void initPhysics()
{
physics = new VerletPhysics2D();
for(Creature cr : cm.creatures)
{
for(Cell c : cr.cells)
{
physics.addParticle(c);
AttractionBehavior repel = new AttractionBehavior(c, 5, -2.0f);
physics.addBehavior(repel);
if(c.isLocked())
{
AttractionBehavior corePull = new AttractionBehavior(c, 50, 1.0f);
physics.addBehavior(corePull);
AttractionBehavior corePush = new AttractionBehavior(c, 25, -3.0f);
physics.addBehavior(corePush);
}
}
for(VerletSpring2D s : cr.bonds)
{
physics.addSpring(s);
}
}
Rect border = new Rect(new Vec2D(-10,-10), new Vec2D(width+10, height+10));
physics.setWorldBounds(border);
}
void mousePressed()
{
int count = (int) random(0,5);
//println(count);
for(int i=0; i<count;i++){
// println("i = "+i);
foods.add(new Food());
}
}
void keyPressed()
{
switch(key){
case 'r' :
{
grid.init();
println("grid rearranged!");
break;
}
case 'h' :
{
HUDflag = !HUDflag;
break;
}
case 'g' :
{
GRIDflag = !GRIDflag;
break;
}
}
}