// Click the mouse to generate a burst of particles at mouse location // Each burst is one instance of a particle system // with Particles and CrazyParticles (a subclass of Particle) // Note use of Inheritance and Polymorphism here import noc.*; //library to communicate with maxMSP import oscP5.*; import netP5.*; OscP5 oscP5; NetAddress whereimsending; String messageselector; ArrayList psystems; PFont f; float theta; int x, y; void setup() { size(1024,720); colorMode(RGB,255,255,255,100); psystems = new ArrayList(); smooth(); f = createFont ("Courier", 16, true); oscP5 = new OscP5(this,12000); } void draw() { background(255); // Cycle through all particle systems, run them and delete old ones for (int i = psystems.size()-1; i >= 0; i--) { ParticleSystem psys = (ParticleSystem) psystems.get(i); psys.run(); } } /* // When the mouse is pressed, add a new particle system void mousePressed() { psystems.add(new ParticleSystem(int(random(25,35)),new Vector3D(mouseX,mouseY))); } */ ////// // A subclass of Particle class CrazyParticle extends Particle { CrazyParticle(Vector3D l) { // "super" means do everything from the constructor in Particle super(l); } // This update() method overrides the parent class update() method void update() { super.update(); } // Method to display void render() { super.render(); } } /// // A simple Particle class class Particle { Vector3D loc; Vector3D vel; Vector3D acc; // float r; float timer; ////////////////// adding the string here String letter; int sz; float t=0; float theta; boolean roll; // constructor Particle(Vector3D l) { acc = new Vector3D(random(-1,1),random(-1,1),0); vel = new Vector3D(0,0); loc = l.copy(); timer = 100.0; int r; ///ascii numbers float trigger = random(0,100); if (trigger < 85) { r = int (random(97,122)); } else { r = int (random( 65,90)); } // this is how processing processes ASCII numbers into letters [chars] char c = (char) r; // and this is the way we ask the letters to be displayed {individually} letter = "" + c; // set the size sz = 25; theta = 0.0; roll = false; } void run() { update(); timer(); render(); } // Method to update location void update() { t = t + random(0.5,1.5); if ((t > 20) && (t < 40)) { // exploding vel = new Vector3D(0,0); acc = new Vector3D(0,0); } else if (t > 50) { //falling //set acceleration so they fall acc.add( new Vector3D(0, .05, 0) ); } if (roll){ float theta_vel = random(5,10); theta += theta_vel; } if( loc.y < height-10 ){ // go, unless on the bottom vel.add(acc); loc.add(vel); } else { //println( "t: " + t ); if( t> 100){ if(roll == false) roll = true; if( loc.x > height/2){ loc.x+=10; } else{ loc.x-=10; } } } } void timer() { timer -= .15; } // Method to display void render() { noStroke(); fill(0, 200);//,timer); textFont(f); textSize(sz); textAlign(CENTER); pushMatrix(); translate(loc.x,loc.y); // rotate(random(2*PI)); rotate( theta );//println("theta in render: " + theta ); text(letter,0,0); popMatrix(); } // Is the particle still useful? boolean dead() { if (timer <= 1) { return true; } else { return false; } } } /////// // A class to describe a group of Particles // An ArrayList is used to manage the list of Particles class ParticleSystem { ArrayList particles; // An arraylist for all the particles Vector3D origin; // An origin point for where particles are birthed ParticleSystem(int num, Vector3D v) { particles = new ArrayList(); // Initialize the arraylist origin = v.copy(); // Store the origin point // Add an initial group of particles to the ArrayList for (int i = 0; i < num; i++) { if (random(1) > 0.2) { particles.add(new Particle(origin)); } else { // There is a 20% chance we will add a "crazy particle" into the system particles.add(new CrazyParticle(origin)); } } } void run() { // Cycle through the ArrayList backwards b/c we are deleting for (int i = particles.size()-1; i >= 0; i--) { Particle p = (Particle) particles.get(i); p.run(); if (p.dead()) { particles.remove(i); } } } void addParticle() { particles.add(new Particle(origin)); } void addParticle(Particle p) { particles.add(p); } } // incoming osc message are forwarded to the oscEvent method. void oscEvent(OscMessage thereceivedmessage) { // print the address pattern and the typetag of the received OscMessage messageselector = thereceivedmessage.addrPattern(); // println("!" + messageselector + "hhhh" + "baba" + thereceivedmessage ); if(messageselector.equals("d")) { // println("drawing..."); x = thereceivedmessage.get(0).intValue(); y = thereceivedmessage.get(1).intValue(); //println("x = "+x); //println("y = "+y); println("x:" + x + " y:" + y); // print(x); // print(y); psystems.add(new ParticleSystem(int(random(5,25)),new Vector3D(x,y))); } }