forked from jackaperkins/boids
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboids.pde
204 lines (175 loc) · 4.57 KB
/
boids.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
// Three rules: (friend = spatially close boid)
// Rule one : take same direction as friends
// Rule two : stay close to friends
// Rule three: don't get in very close contact with friends
ArrayList<Boid> boids;
ArrayList<Avoid> avoids;
float globalScale = .91;
float eraseRadius = 20;
String tool = "boids";
// boid control
float maxSpeed;
float friendRadius;
float crowdRadius;
float avoidRadius;
float coheseRadius;
boolean option_friend = true;
boolean option_crowd = true;
boolean option_avoid = true;
boolean option_noise = true;
boolean option_cohese = true;
// gui crap
int messageTimer = 0;
String messageText = "";
void setup () {
size(1024, 576);
textSize(16);
recalculateConstants();
boids = new ArrayList<Boid>();
avoids = new ArrayList<Avoid>();
for (int x = 100; x < width - 100; x+= 100) {
for (int y = 100; y < height - 100; y+= 100) {
// boids.add(new Boid(x + random(3), y + random(3)));
// boids.add(new Boid(x + random(3), y + random(3)));
}
}
setupWalls();
}
void recalculateConstants () {
maxSpeed = 2.1 * globalScale;
friendRadius = 60 * globalScale;
crowdRadius = (friendRadius / 1.3);
avoidRadius = 90 * globalScale;
coheseRadius = friendRadius;
}
void setupWalls() {
avoids = new ArrayList<Avoid>();
for (int x = 0; x < width; x+= 20) {
avoids.add(new Avoid(x, 10));
avoids.add(new Avoid(x, height - 10));
}
}
void setupCircle() {
avoids = new ArrayList<Avoid>();
for (int x = 0; x < 50; x+= 1) {
float dir = (x / 50.0) * TWO_PI;
avoids.add(new Avoid(width * 0.5 + cos(dir) * height*.4, height * 0.5 + sin(dir)*height*.4));
}
}
void draw () {
noStroke();
colorMode(HSB);
fill(0, 100);
rect(0, 0, width, height);
if (tool == "erase") {
noFill();
stroke(0, 100, 260);
rect(mouseX - eraseRadius, mouseY - eraseRadius, eraseRadius * 2, eraseRadius *2);
if (mousePressed) {
erase();
}
} else if (tool == "avoids") {
noStroke();
fill(0, 200, 200);
ellipse(mouseX, mouseY, 15, 15);
}
for (int i = 0; i <boids.size(); i++) {
Boid current = boids.get(i);
current.go();
current.draw();
}
for (int i = 0; i <avoids.size(); i++) {
Avoid current = avoids.get(i);
current.go();
current.draw();
}
if (messageTimer > 0) {
messageTimer -= 1;
}
drawGUI();
}
void keyPressed () {
if (key == 'q') {
tool = "boids";
message("Add boids");
} else if (key == 'w') {
tool = "avoids";
message("Place obstacles");
} else if (key == 'e') {
tool = "erase";
message("Eraser");
} else if (key == '-') {
message("Decreased scale");
globalScale *= 0.8;
} else if (key == '=') {
message("Increased Scale");
globalScale /= 0.8;
} else if (key == '1') {
option_friend = option_friend ? false : true;
message("Turned friend allignment " + on(option_friend));
} else if (key == '2') {
option_crowd = option_crowd ? false : true;
message("Turned crowding avoidance " + on(option_crowd));
} else if (key == '3') {
option_avoid = option_avoid ? false : true;
message("Turned obstacle avoidance " + on(option_avoid));
}else if (key == '4') {
option_cohese = option_cohese ? false : true;
message("Turned cohesion " + on(option_cohese));
}else if (key == '5') {
option_noise = option_noise ? false : true;
message("Turned noise " + on(option_noise));
} else if (key == ',') {
setupWalls();
} else if (key == '.') {
setupCircle();
}
recalculateConstants();
}
void drawGUI() {
if(messageTimer > 0) {
fill((min(30, messageTimer) / 30.0) * 255.0);
text(messageText, 10, height - 20);
}
}
String s(int count) {
return (count != 1) ? "s" : "";
}
String on(boolean in) {
return in ? "on" : "off";
}
void mousePressed () {
switch (tool) {
case "boids":
boids.add(new Boid(mouseX, mouseY));
message(boids.size() + " Total Boid" + s(boids.size()));
break;
case "avoids":
avoids.add(new Avoid(mouseX, mouseY));
break;
}
}
void erase () {
for (int i = boids.size()-1; i > -1; i--) {
Boid b = boids.get(i);
if (abs(b.pos.x - mouseX) < eraseRadius && abs(b.pos.y - mouseY) < eraseRadius) {
boids.remove(i);
}
}
for (int i = avoids.size()-1; i > -1; i--) {
Avoid b = avoids.get(i);
if (abs(b.pos.x - mouseX) < eraseRadius && abs(b.pos.y - mouseY) < eraseRadius) {
avoids.remove(i);
}
}
}
void drawText (String s, float x, float y) {
fill(0);
text(s, x, y);
fill(200);
text(s, x-1, y-1);
}
void message (String in) {
messageText = in;
messageTimer = (int) frameRate * 3;
}