/
residUUm.pde
652 lines (575 loc) · 19.8 KB
/
residUUm.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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
import punktiert.math.Vec;
import punktiert.physics.*;
// import OSC utils
import oscP5.*;
import netP5.*;
import java.util.regex.*;
// declare OSC objects
OscP5 oscP5;
NetAddress myRemoteLocation;
//world object
VPhysics physics;
// attractor
BAttraction attr;
Vec pos;
//number of particles in the scene
int amount = 0;
int sides;
float rad;
int satVal;
int brightVal;
float radiusNoise = random(10);
boolean oneLine = true;
boolean twoLines = false;
float lineChange = 0.0;
int frameGague = 24;
float [] multScaler = {0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4, 5};
IntList particleIdMapping;
float particleSize;
FloatList particleShape;
float particleLocationX;
float particleLocationY;
float particleLocationZ;
float particleRotation;
FloatList particleColorH;
float particleColorS;
float particleColorB;
float particleColorA;
FloatList particleLifespan;
FloatList particleGroup;
FloatList fadeOut;
int groupCounter = 0;
int maxGroups = 6;
IntList shapeId;
int particleBehavior;
// IntList sideId;
// int circSide;
float angle;
float halfAngle;
float radius1, radius2;
float newFadeOut=10;
float globalFadeOut=0;
float outerAngle;
float innerAngle;
int maxNumParticles=45;
int numMessages=0;
boolean collisions=true;
public void setup() {
size(displayWidth, displayHeight, OPENGL);
//size(800, 600, OPENGL);
colorMode(HSB, 360, 100, 100, 100);
smooth(8);
rectMode(CENTER);
oscP5 = new OscP5(this, 11998);
// set remote location
myRemoteLocation = new NetAddress("127.0.0.1", 12004);
// myRemoteLocation = new NetAddress("192.168.2.1", 12004);
OscMessage newMessage = new OscMessage("/global");
newMessage.add("/muteAll");
oscP5.send(newMessage, myRemoteLocation);
OscMessage backMessage = new OscMessage("/global");
backMessage.add("/back");
backMessage.add(0);
oscP5.send(backMessage, myRemoteLocation);
particleGroup = new FloatList();
particleLifespan = new FloatList();
fadeOut = new FloatList();
particleIdMapping = new IntList();
shapeId = new IntList();
// sideId = new IntList();
for(int n = 0; n < maxNumParticles; n++) {
particleLifespan.append(-1);
particleIdMapping.append(-1);
shapeId.append(-1);
fadeOut.append(-1);
}
physics = new VPhysics(width, height);
physics.setfriction(.1f);//map(mouseY, 0, width, 0.0f, 1.0f));
// new AttractionForce: (Vec pos, radius, strength)
attr = new BAttraction(new Vec(width * .5f, height * .5f), 100, 20f);
physics.addBehavior(attr);
for (int i = 0; i < amount; i ++) {
//val for arbitrary radius
rad = random(5, 200);
//vector for position
pos = new Vec (random(rad, width - rad), random(rad, height - rad), 0);
//create particle (Vec pos, mass, radius)
VParticle particle = new VParticle(pos, 1, rad);
//add Collision Behavior
particle.addBehavior(new BCollision());
// add Local Attractor on each Particle (radius, strength)
particle.addBehavior(new BAttractionLocal(rad * 5, 2));
//add Cohesion Behavior to each Particle (radius, maxSpeed, maxForce)
particle.addBehavior(new BCohesion(100, 1.5f, .5f));
//add particle to world
physics.addParticle(particle);
}
}
//enable full screen presentation mode
boolean sketchFullScreen()
{
return true;
}
int callsToDraw=0;
int HERE=0;
public void draw() {
frameRate(frameGague);
/*if(satVal < 60) background(360, 100, 0);
else if(brightVal < 50) background(360, 0, 100);
else */background(map(mouseX, 0, width, 0, 360), map(mouseX, 0, width, 0, 100), map(mouseY, 0, height, 0, 100));
cursor(CROSS);
physics.update();
physics.setfriction(map(mouseY, 0, width, 0.0f, 1.0f));
//println(physics.getfriction());
drawShape();
// set pos to mousePosition
noFill();
attr.setAttractor(new Vec(mouseX, mouseY));
ellipse(attr.getAttractor().x, attr.getAttractor().y, attr.getRadius(), attr.getRadius());
if((callsToDraw&1)==0){
OscMessage newMessage = new OscMessage("/route");
newMessage.add("/counter");
newMessage.add(numMessages++);
boolean done=false;
for(int n = 0; n < particleIdMapping.size(); n ++) {
int particleIndex=getParticleFromId(n);
if(particleIndex<0) //true only if not all of the voices are allocated
continue;
done=true;
float lifespan=getLifespan(n);
if(lifespan<0){
if(HERE == 0){
// println("You shouldn't be here");
// println("id: "+n+"; particle: "+particleIndex);
}
HERE=1;
continue;// println("sending message for id: "+n+"; particle: "+particleIndex+"; lifespan: "+lifespan);
}
newMessage.add("/id");
newMessage.add(n);
if(lifespan >= 0) {
particleLifespan.sub(n, (globalFadeOut + fadeOut.get(n)));
if(particleLifespan.get(n) < 0.0) {
unassignId(n);
}
}
newMessage.add("/lifespan");
newMessage.add(getLifespan(n));
newMessage.add("/locationx");
newMessage.add(physics.particles.get(particleIndex).x);
newMessage.add("/locationy");
newMessage.add(physics.particles.get(particleIndex).y);
// newMessage.add("/locationz");
// newMessage.add(map(particleLifespan.get(i), 360, 0, 5, 100));
}
// println("collisions:"+collisions);
for(int i = 0; i < physics.particles.size(); i++){
if(getIdFromParticle(i)<0)
continue;
for(int n = i + 1; n < physics.particles.size(); n++) {
if(getIdFromParticle(n)<0)
continue;
if(
collisions == true
&&
dist(physics.particles.get(i).x, physics.particles.get(i).y, physics.particles.get(n).x, physics.particles.get(n).y) <
(physics.particles.get(i).getRadius() + physics.particles.get(n).getRadius())
&&
dist(mouseX, mouseY, physics.particles.get(n).x, physics.particles.get(n).y) < 200
)
{
done=true;
//println(physics.particles.get(i) + ", " + physics.particles.get(i + 1));
newMessage.add("/id");
newMessage.add(i);
newMessage.add("/collision");
newMessage.add(1);
newMessage.add("/id");
newMessage.add(n);
newMessage.add("/collision");
newMessage.add(1);
//point(physics.particles.get(i).x, physics.particles.get(i).y);
//ellipse(physics.particles.get(i).x, physics.particles.get(i).y, rippleSize, rippleSize);
float rippleX1;
float rippleY1;
float rippleX2;
float rippleY2;
float radAng;
float thisRadius;
float lastX1 = random(width);
float lastY1 = random(height);
float lastX2 = random(width);
float lastY2 = random(height);
float multScalerVal = 1.0;
beginShape();
for(int j = 0; j <= 360; j ++) {
radAng = radians(j);
radiusNoise += 0.005;
thisRadius = physics.particles.get(i).getRadius() * multScalerVal + (noise(radiusNoise) * random(physics.particles.get(i).getRadius()));
if(oneLine) multScalerVal = multScaler[5];
else if(twoLines) multScalerVal = multScaler[(int)random(0, 15)];
rippleX1 = physics.particles.get(i).x + (thisRadius * cos(radAng));
rippleY1 = physics.particles.get(i).y + (thisRadius * sin(radAng));
rippleX2 = physics.particles.get(n).x + (thisRadius * cos(radAng));
rippleY2 = physics.particles.get(n).y + (thisRadius * sin(radAng));
if(oneLine) {
strokeWeight(map(mouseX, 0, width, j, 50));
if(satVal < 60) stroke(360, 0, 100, random(100));
else if(brightVal < 50) stroke(360, 100, 0, random(100));
else stroke(map(getIdFromParticle(i), 1, maxNumParticles, 0, 360), 100, 100, random(1, 15));
line(rippleX1, rippleY1, lastX1, lastY1);
}
else if(twoLines) {
strokeWeight(random(lineChange));
if(satVal < 60) stroke(360, 0, 100, random(100));
else if(brightVal < 50) stroke(360, 100, 0, random(100));
else stroke(map(getIdFromParticle(i), 1, maxNumParticles, 0, 360), 100, 100, random(100));
line(rippleX1, rippleY1, lastX1, lastY1);
if(satVal < 60) stroke(360, 0, 100, random(100));
else if(brightVal < 50) stroke(360, 100, 0, random(100));
else stroke(map(getIdFromParticle(n), 1, maxNumParticles, 360, 0), 100, 100, random(100));
line(rippleX2, rippleY2, lastX2, lastY2);
}
lastX1 = rippleX1;
lastY1 = rippleY1;
lastX2 = rippleX2;
lastY2 = rippleY2;
}
endShape();
}
}
}
if(done)
oscP5.send(newMessage, myRemoteLocation);
}
callsToDraw ++;
if(mousePressed) {
satVal = (int(random(55, 100)));
brightVal = (int(random(45, 100)));
if(satVal < 60) {
OscMessage backMessage = new OscMessage("/global");
backMessage.add("/back");
backMessage.add(2);
oscP5.send(backMessage, myRemoteLocation);
}
else if(brightVal < 50) {
OscMessage backMessage = new OscMessage("/global");
backMessage.add("/back");
backMessage.add(1);
oscP5.send(backMessage, myRemoteLocation);
}
else {
OscMessage backMessage = new OscMessage("/global");
backMessage.add("/back");
backMessage.add(0);
oscP5.send(backMessage, myRemoteLocation);
}
particleBehavior = (int)random(0, 4);
int sizeBefore=physics.particles.size();
if(particleBehavior == 0) physics.addParticle(new VParticle(new Vec(mouseX, mouseY, 0), 1, random(5, 120)).addBehavior(new BCollision()));
else if(particleBehavior == 1) physics.addParticle(new VParticle(new Vec(mouseX, mouseY, 0), 1, random(5, 20)).addBehavior(new BAttractionLocal(rad * 5, 2)));
else if(particleBehavior == 2) physics.addParticle(new VParticle(new Vec(mouseX, mouseY, 0), 1, random(50, 100)).addBehavior(new BCohesion(100, 1.5f, .5f)));
else if(particleBehavior == 3) physics.addParticle(new VParticle(new Vec(mouseX, mouseY, 0), 1, random(50, 150)).addBehavior(attr));
int sizeAfter=physics.particles.size();
if(sizeBefore==sizeAfter){
// println("Size before: "+sizeBefore);
// println("Size after: "+sizeAfter);
// println("behavior: "+particleBehavior);
// printArray();
} else {
int particle=physics.particles.size()-1;
int id=assignIdToParticle(particle);
// println("Just assigned this id: "+id+" to particle "+particle);
if(id<0) {//there are no more ids available, fuckit
println("there are no more ids available, fuckit");
// printArray();
} else {
shapeId.set(id, (int)random(0, 5));
// sideId.set(id, (int)random(20, 360));
particleLifespan.set(id, random(200, 360)); //and initialize lifespan
fadeOut.set(id, newFadeOut);
}
}
}
}
int previousParticlesCount = 0;
void mouseReleased() {
// if (true)
// return;
//particleGroup.append(groupCounter);
OscMessage newMessage = new OscMessage("/route");
newMessage.add("/counter");
newMessage.add(numMessages++);
for(int i = previousParticlesCount; i < physics.particles.size(); i ++) {
int id=getIdFromParticle(i);
if(id < 0)
continue;
// println("id: "+id);
newMessage.add("/id");
newMessage.add(id);
newMessage.add("/size");
newMessage.add(physics.particles.get(i).getRadius());
newMessage.add("/color");
newMessage.add(map(i, 1, physics.particles.size(), 0, 360));
// newMessage.add(map(id, 1, physics.particles.getHue(i), 0, 360));
newMessage.add("/shape");
newMessage.add(shapeId.get(id));
// newMessage.add("/locationy");
// newMessage.add(physics.particles.get(i).y);
newMessage.add("/locationz");
newMessage.add(physics.particles.get(i).z);
newMessage.add("/group");
newMessage.add(groupCounter);
}
oscP5.send(newMessage, myRemoteLocation);
previousParticlesCount = physics.particles.size();
// println(physics.particles.size() + " " + previousParticlesCount);
groupCounter ++;
if(groupCounter>=maxGroups)
groupCounter=0;
}
void drawShape() {
particleRotation = radians((int)random(0, 360)); //radians(frameCount);
for(int i = 0; i < physics.particles.size(); i ++) {
int id=getIdFromParticle(i);
if(id < 0)
continue;
int shape=shapeId.get(id);
// if(shape == 4) circSide = sideId.get(id);
//if(shape != 4) particleRotation = radians((int)random(0, 360)); //radians(frameCount);
switch((shape)) {
case 0: //rectangle
shape = 0;
sides = 4;
break;
case 1: //triangle
shape = 1;
sides = 3;
break;
case 2: //star
shape = 2;
break;
case 3: //line
shape = 3;
break;
case 4: //ellipse
shape = 4;
sides = 360; //circSide;
break;
}
outerAngle = TWO_PI / sides;
innerAngle = TWO_PI / sides;
// println("I drew this id"+id);
strokeWeight(1);
stroke(map(id, 1, maxNumParticles, 360, 0), 100, 100, map(particleLifespan.get(id), 360, 0, 100, 0));
fill(map(id, 1, maxNumParticles, 0, 360), map(particleLifespan.get(id), 360, 0, 100, 60), map(particleLifespan.get(id), 360, 0, 100, 50), map(particleLifespan.get(id), 360, 0, 100, 0));
//fill(map(id, 1, maxNumParticles, 0, 360), satVal, brightVal, map(particleLifespan.get(id), 360, 0, 100, 0));
if((shape < 2) || ((shape != 3) && (shape > 2))) {
pushMatrix();
radiusNoise += 0.005;
beginShape();
for (int j = 0; j < sides; j ++) {
float x = physics.particles.get(i).x + cos(j * outerAngle - particleRotation) * physics.particles.get(i).getRadius();
float y = physics.particles.get(i).y + sin(j * outerAngle - particleRotation) * physics.particles.get(i).getRadius();
float z = map(particleLifespan.get(id), 360, 0, 100, 0); //physics.particles.get(i).z; //map(particleLifespan.get(id), 360, 0, 0, 10);
vertex(x, y, z);
}
// beginContour();
// for (int j = 0; j < sides; j ++) {
// radiusNoise += 0.005;
// float x = physics.particles.get(i).x + noise(radiusNoise) * cos(j * innerAngle + particleRotation) * physics.particles.get(i).getRadius()/2; // - random(0, (physics.particles.get(i).getRadius() - 2)));
// float y = physics.particles.get(i).y + noise(radiusNoise) * sin(j * innerAngle + particleRotation) * physics.particles.get(i).getRadius()/2; // - random(0, (physics.particles.get(i).getRadius() - 2)));
// float z = map(particleLifespan.get(id), 360, 0, 100, 0); //physics.particles.get(i).z; //map(particleLifespan.get(id), 360, 0, 0, 10);
// vertex(x, y, z);
// }
// endContour();
endShape(CLOSE);
popMatrix();
}
else if(shape == 2) {
angle = TWO_PI / (map(mouseX, 0, width, 1, 10));
halfAngle = angle/2.0;
pushMatrix();
beginShape();
for (float a = 0; a < TWO_PI; a += angle) {
float x = physics.particles.get(i).x + cos(a - particleRotation) * physics.particles.get(i).getRadius();
float y = physics.particles.get(i).y + sin(a - particleRotation) * physics.particles.get(i).getRadius();
float z = map(particleLifespan.get(id), 360, 0, 100, 0); //physics.particles.get(i).z;
vertex(x, y, z);
x = physics.particles.get(i).x + cos(a + halfAngle - particleRotation) * physics.particles.get(i).getRadius() / 2.0;
y = physics.particles.get(i).y + sin(a + halfAngle - particleRotation) * physics.particles.get(i).getRadius() / 2.0;
z = map(particleLifespan.get(id), 360, 0, 100, 0); //physics.particles.get(i).z;
vertex(x, y, z);
}
endShape(CLOSE);
popMatrix();
}
else if(shape == 3) {
pushMatrix();
strokeWeight((int)random(1, physics.particles.get(i).getRadius()));
line(physics.particles.get(i).x, 0, physics.particles.get(i).x, height);
line(0, physics.particles.get(i).y, width, physics.particles.get(i).y);
for(int j = 0; j < physics.particles.size() - 1; j ++) {
//strokeWeight(1);
int idj=getIdFromParticle(j);
if(idj < 0)
continue;
strokeWeight(2);
rotate(particleRotation);
stroke(map(j, 1, physics.particles.size(), 360, 0), 100, 100, map(particleLifespan.get(idj), 360, 0, 100, 0));
line(physics.particles.get(j).x, physics.particles.get(j).y, physics.particles.get(j).z,
physics.particles.get(j + 1).x, physics.particles.get(j + 1).y, physics.particles.get(j + 1).z);
}
popMatrix();
}
}
}
int assignIdToParticle(int particle){
int id=getAvailableId();
if(id>=0){
particleIdMapping.set(id,particle);
}
return id;
}
void unassignId(int id){
particleIdMapping.set(id, -1);
}
int getIdFromParticle(int particle){
for(int n = 0; n < particleIdMapping.size(); n++){
if(particle==particleIdMapping.get(n))
return n;
}
return -1;
}
int getParticleFromId(int id){
for(int i = 0; i < physics.particles.size(); i++){
if(id==getIdFromParticle(i))
return i;
}
return -1;
}
int getAvailableId(){
for(int n = 0; n < particleIdMapping.size(); n++){
if(particleIdMapping.get(n)==-1)
return n;
}
return -1;
}
float getLifespan(int id){
return particleLifespan.get(id);
}
void setLifespan(int id, float value){
particleLifespan.set(id, value);
}
void printArray(){
for(int n=0; n<maxNumParticles; n++){
println("id: "+n+" "+particleLifespan.get(n)+" "+particleIdMapping.get(n));
}
}
int getNumActiveParticles(){
int count=0;
for(int n = 0; n < particleIdMapping.size(); n++){
if(particleIdMapping.get(n)!=-1)
count++;
}
return count;
}
void keyPressed(){
fill(360, 0, 100, 20);
noStroke();
if(key == RETURN || key == ENTER)
printArray();
if(key == 'q'){
newFadeOut=50;
globalFadeOut=0;
collisions=false;
println("fade "+newFadeOut);
//ellipse(1350, height - 100, 10, 10);
return;
}
if(key == 'w'){
//ellipse(1350, height - 120, 10, 10);
for(int n=0; n<particleIdMapping.size(); n++){
fadeOut.set(n, newFadeOut);
// println("fadeout"+fadeOut.get(n));
}
return;
}
collisions=true;
if(key == 'a'){
newFadeOut=2.5;
globalFadeOut=0;
println("fade "+newFadeOut);
//ellipse(1350, height - 60, 10, 10);
return;
}
if(key == 's'){
newFadeOut=0;
globalFadeOut=0;
println("fade "+newFadeOut);
//ellipse(1350, height - 20, 10, 10);
return;
}
if(key == 'x'){
globalFadeOut+=2;
//ellipse(1350, height - 40, 10, 10);
return;
}
if(key == 'z'){
//ellipse(1350, height - 20, 10, 10);
globalFadeOut-=2;
if(globalFadeOut<0)
globalFadeOut=0;
return;
}
if(key == 'l') {
oneLine = !oneLine;
twoLines = !twoLines;
}
char [] s = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
for(float i = 0; i < s.length; i ++) {
if(key == s[(int)i]) lineChange = i;
switch((int)lineChange) {
case 0:
lineChange = 0.5;
break;
case 1:
lineChange = 1;
break;
case 2:
lineChange = 2;
break;
case 3:
lineChange = 3;
break;
case 4:
lineChange = 4;
break;
case 5:
lineChange = 5;
break;
case 6:
lineChange = 6;
break;
case 7:
lineChange = 7;
break;
case 8:
lineChange = 8;
break;
case 9:
lineChange = 9;
break;
}
}
if ((key == 'g') || (key == 'h')) {
if (key == 'g') {
frameGague --;
if(frameGague <= 0) frameGague = 24;
} else if (key == 'h') {
frameGague ++;
if(frameGague >= 25) frameGague = 1;
}
println("frameGague: " + frameGague);
}
}