forked from ashugeo/Silhouettes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pluie.pde
311 lines (268 loc) · 9.14 KB
/
Pluie.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
class Pluie extends SubSketch
{
PluieFlock flock;
int flockSize = 1;
String[] alphabet = {"A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","C","C","C","C","C","C","C","C","C","C","C","C","C","C","D","D","D","D","D","D","D","D","D","D","D","D","D","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","F","F","F","G","G","G","G","G","H","H","H","H","I","I","I","I","I","I","I","I","I","I","I","I","I","I","I","I","I","I","I","J","K","L","L","L","L","L","L","L","L","L","L","L","L","L","L","L","M","M","M","M","M","M","M","M","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","P","P","P","P","P","P","P","Q","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","S","T","T","T","T","T","T","T","T","T","T","T","T","T","T","T","T","T","U","U","U","U","U","U","U","U","U","U","U","U","U","U","V","V","V","V","W","X","Y","Z"};
PVector missionPoint;
float s = 15;
float a = 1.0;
float c = 0.1;
float m = 0.1;
float f = 10.0;
public Pluie(PApplet parent, int _width, int _height)
{
super(parent,_width,_height);
name = "(a) Pluie";
flock = new PluieFlock();
// Add an initial set of boids into the system
missionPoint = new PVector(width/2,height/3);
}
public void draw() {
background(255);
flock.run();
if (s>2.0) s-=0.1;
}
// Add a new boid into the System
public void mouseReleased() {
s = 15.0;
}
public void mousePressed()
{
m=3.0;
}
void mouseDragged()
{
flockSize++;
println(flockSize);
flock.addPluieBoid(new PluieBoid(this,flock, random(0,pluie.width),0));
}
}
class PluieFlock {
ArrayList<PluieBoid> boids; // An ArrayList for all the boids
PluieFlock() {
boids = new ArrayList<PluieBoid>(); // Initialize the ArrayList
}
void run() {
for (PluieBoid b : boids) {
b.run(boids); // Passing the entire list of boids to each boid individually
}
}
void addPluieBoid(PluieBoid b) {
boids.add(b);
}
void removePluieBoid(PluieBoid b){
boids.remove(b);
}
}
// The PluieBoid class
class PluieBoid {
Pluie pluie;
PluieFlock flock;
PVector position;
PVector velocity;
PVector acceleration;
float r;
float maxforce; // Maximum steering force
float maxspeed; // Maximum speed
String letter;
int textSize = 12;
PluieBoid(Pluie p, PluieFlock f, float x, float y) {
pluie = p;
flock = f;
acceleration = new PVector(0, 0);
// This is a new PVector method not yet implemented in JS
// velocity = PVector.random2D();
// Leaving the code temporarily this way so that this example runs in JS
float angle = random(TWO_PI);
velocity = new PVector(cos(angle), sin(angle));
position = new PVector(x, y);
r = 2.0;
maxspeed = 11; //2
maxforce = 0.01; //0.03
letter = pluie.alphabet[int(random(0,pluie.alphabet.length))];
}
void run(ArrayList<PluieBoid> boids) {
flock(boids);
update();
borders();
render();
}
void applyForce(PVector force) {
// We could add mass here if we want A = F / M
acceleration.add(force);
}
// We accumulate a new acceleration each time based on three rules
void flock(ArrayList<PluieBoid> boids) {
PVector sep = separate(boids); // Separation
PVector ali = align(boids); // Alignment
PVector coh = cohesion(boids); // Cohesion
PVector mis = mission(pluie.missionPoint);
PVector fal = fall();
// Arbitrarily weight these forces
sep.mult(pluie.s);
ali.mult(pluie.a);
coh.mult(pluie.c);
mis.mult(pluie.m);
fal.mult(pluie.f);
// Add the force vectors to acceleration
applyForce(sep);
applyForce(ali);
applyForce(coh);
applyForce(mis);
applyForce(fal);
}
// Method to update position
void update() {
// Update velocity
velocity.add(acceleration);
// Limit speed
velocity.limit(maxspeed);
position.add(velocity);
// Reset accelertion to 0 each cycle
acceleration.mult(0);
}
// A method that calculates and applies a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
PVector seek(PVector target) {
PVector desired = PVector.sub(target, position); // A vector pointing from the position to the target
// Scale to maximum speed
desired.normalize();
desired.mult(maxspeed);
// Above two lines of code below could be condensed with new PVector setMag() method
// Not using this method until Processing.js catches up
// desired.setMag(maxspeed);
// Steering = Desired minus Velocity
PVector steer = PVector.sub(desired, velocity);
steer.limit(maxforce); // Limit to maximum steering force
return steer;
}
void render() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + radians(90);
// heading2D() above is now heading() but leaving old syntax until Processing.js catches up
pluie.fill(0);
pluie.stroke(0);
pluie.pushMatrix();
pluie.translate(position.x, position.y);
pluie.rotate(theta);
/*pluie.beginShape(TRIANGLES);
pluie.vertex(0, -r*2);
pluie.vertex(-r, r*2);
pluie.vertex(r, r*2);
pluie.endShape();*/
pluie.textSize = int(map(position.y,pluie.height,0,0,30));
pluie.textSize = constrain(pluie.textSize,1,30);
pluie.text(letter,0,0);
pluie.popMatrix();
}
// Wraparound
void borders() {
if (position.x < -r) position.x = r;
if (position.x > pluie.width+r) position.x = pluie.width-r;
if (position.y > pluie.height+r)
{
position.y = -r;
position.x = random(0,pluie.width);
}
if (position.dist(pluie.missionPoint) < 100)
{
velocity.y = -0.4*velocity.y;
if (position.x < pluie.width/2) velocity.x -=0.3;
else velocity.x += 0.3;
}
}
// Separation
// Method checks for nearby boids and steers away
PVector separate (ArrayList<PluieBoid> boids) {
float desiredseparation = 25.0f;
PVector steer = new PVector(0, 0, 0);
int count = 0;
// For every boid in the system, check if it's too close
for (PluieBoid other : boids) {
float d = PVector.dist(position, other.position);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if ((d > 0) && (d < desiredseparation)) {
// Calculate vector pointing away from neighbor
PVector diff = PVector.sub(position, other.position);
diff.normalize();
diff.div(d); // Weight by distance
steer.add(diff);
count++; // Keep track of how many
}
}
// Average -- divide by how many
if (count > 0) {
steer.div((float)count);
}
// As long as the vector is greater than 0
if (steer.mag() > 0) {
// First two lines of code below could be condensed with new PVector setMag() method
// Not using this method until Processing.js catches up
// steer.setMag(maxspeed);
// Implement Reynolds: Steering = Desired - Velocity
steer.normalize();
steer.mult(maxspeed);
steer.sub(velocity);
steer.limit(maxforce);
}
return steer;
}
// Alignment
// For every nearby boid in the system, calculate the average velocity
PVector align (ArrayList<PluieBoid> boids) {
float neighbordist = 50;
PVector sum = new PVector(0, 0);
int count = 0;
for (PluieBoid other : boids) {
float d = PVector.dist(position, other.position);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.velocity);
count++;
}
}
if (count > 0) {
sum.div((float)count);
// First two lines of code below could be condensed with new PVector setMag() method
// Not using this method until Processing.js catches up
// sum.setMag(maxspeed);
// Implement Reynolds: Steering = Desired - Velocity
sum.normalize();
sum.mult(maxspeed);
PVector steer = PVector.sub(sum, velocity);
steer.limit(maxforce);
return steer;
}
else {
return new PVector(0, 0);
}
}
// Cohesion
// For the average position (i.e. center) of all nearby boids, calculate steering vector towards that position
PVector cohesion (ArrayList<PluieBoid> boids) {
float neighbordist = 50;
PVector sum = new PVector(0, 0); // Start with empty vector to accumulate all positions
int count = 0;
for (PluieBoid other : boids) {
float d = PVector.dist(position, other.position);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.position); // Add position
count++;
}
}
if (count > 0) {
sum.div(count);
return seek(sum); // Steer towards the position
}
else {
return new PVector(0, 0);
}
}
//Reach a point
PVector mission (PVector attractivePoint) {
return seek(attractivePoint);
}
//Fall down
PVector fall (){
return seek(new PVector(position.x,pluie.height));
}
}