Commits

Anonymous committed 8843be6

added main pde file

Comments (0)

Files changed (1)

engulferator04.pde

+import traer.physics.*;
+import oscP5.*;
+import netP5.*;
+
+OscP5 oscP5;
+NetAddress processLocation;
+
+Particle[] bouncer;
+Particle[] snd;
+ParticleSystem physics;
+int sndNum = 16;
+int bNum = 4;
+
+float[] bx = new float[bNum];
+float[] by = new float[bNum];
+int bs = 20;
+boolean[] bover = new boolean[bNum];
+boolean[] locked = new boolean[bNum];
+float[] bdifx = new float[bNum];
+float[] bdify = new float[bNum]; 
+
+void setup()
+{
+  size( 400, 400 );
+  frameRate( 24 );
+
+  oscP5 = new OscP5(this,12000);
+  processLocation = new NetAddress("127.0.0.1",7400);
+
+  for(int i=0; i<bNum; i++) {
+    bx[i] = random(width);
+    by[i] = random(height);
+    bover[i] = false; 
+    locked[i] = false; 
+    bdifx[i] = 0.0; 
+    bdify[i] = 0.0;
+  }
+  ellipseMode( CENTER );
+  smooth();
+
+  noStroke();
+
+  physics = new ParticleSystem( 5, 0.5 );
+  snd = new Particle[sndNum];
+  for (int i=0; i<sndNum; i++) {
+    snd[i] = physics.makeParticle( 1.0, random( 0, width+10 ), random( 0, height+50 ), 0 );
+  }
+  for(int i=0; i<sndNum; i++) {
+    for (int j=0; j<sndNum; j++) {
+      physics.makeAttraction( snd[i], snd[j], -100, 10 ); 
+    }
+  }
+
+  bouncer = new Particle[bNum];
+  for (int i=0; i<bNum; i++)
+    bouncer[i] = physics.makeParticle(1.0, bx[i], by[i], 0); 
+  for (int i=0; i<bNum; i++) {
+    bouncer[i].makeFixed(); 
+    for (int j=0; j<sndNum; j++) {
+      physics.makeAttraction( bouncer[i], snd[j], -7000, 20 );
+    }
+  }
+}
+
+void draw()
+{
+  background(0);
+
+  // Test if the cursor is over the box 
+  for(int i=0; i<bNum; i++) {
+    if (mouseX > bx[i]-bs && mouseX < bx[i]+bs && 
+      mouseY > by[i]-bs && mouseY < by[i]+bs) {
+      bover[i] = true;  
+      if(!locked[i]) { 
+        //            stroke(255); 
+        //            fill(153);
+      } 
+    } 
+    else {
+      //          stroke(153);
+      //          fill(153);
+      bover[i] = false;
+    }
+  }
+  background( 30 );  
+  // Draw the box
+  for(int i=0; i<bNum; i++) {
+    ellipse(bx[i], by[i], bs, bs);
+  }
+
+  for (int i=0; i<sndNum; i++) {
+    handleBoundaryCollisions( snd[i] );
+    sendOSC();
+  }
+
+  fill( 200);
+  for (int i=0; i<sndNum; i++){
+    ellipse( snd[i].position().x(), snd[i].position().y(), 15, 15 );   
+  }
+
+  fill( 100);
+  for (int i=0; i<bNum; i++){
+    ellipse( bouncer[i].position().x(), bouncer[i].position().y(), 25, 25 );
+  }
+
+  physics.tick(0.5);
+}
+
+
+
+void mousePressed() {
+  for(int i=0; i<bNum; i++) {
+    if(bover[i]) { 
+      locked[i] = true; 
+      //fill(255, 255, 255);
+    } 
+    else {
+      locked[i] = false;
+    }
+    bdifx[i] = mouseX-bx[i]; 
+    bdify[i] = mouseY-by[i]; 
+  }
+}
+
+void mouseDragged() {
+  for(int i=0; i<bNum; i++) {
+    if(locked[i]) {
+      bx[i] = mouseX-bdifx[i]; 
+      by[i] = mouseY-bdify[i]; 
+      bouncer[i].position().set(bx[i], by[i], 0);
+    }
+  }
+}
+
+void mouseReleased() {
+  for(int i=0; i<bNum; i++) {
+    locked[i] = false;
+  }
+}
+
+void handleBoundaryCollisions( Particle p )
+{
+  if ( p.position().x() < 40 || p.position().x() > width-40 )
+    p.setVelocity( -0.9*p.velocity().x(), p.velocity().y(), 0 );
+  if (p.position().y() > height )
+    p.position().set(random(width), 0, 0);
+}
+
+void sendOSC() {
+  OscBundle myBundle = new OscBundle();
+  OscMessage myMessage = new OscMessage("/test");
+  for (int i=0; i<sndNum; i++){
+    myMessage.add(snd[i].position().x());
+    myBundle.add(myMessage);
+    myMessage.clear();
+    myMessage.add(snd[i].position().y());
+    myBundle.add(myMessage);
+    oscP5.send(myBundle, processLocation);
+    myBundle.clear();
+  }    
+}
+
+