/* A Particle Class based on: "Particle animation and rendering using data parallel computation", Karl Sims Morgen Fleisig 21 February 2011 */ class Particle { /* head tail position radius color opacity */ PVector pH; //head location PVector pT; //tail location float rH = 7; //head radius--HARD-CODED TEMP--REALLY DIAMETER! float rT = rH*0.8; //tail radius--HARD-CODED TEMP--REALLY DIAMETER! float aH = 150; //head opacity--HARD-CODED TEMP float aT = aH*0.6; //tail opacity--HARD-CODED TEMP float mass = 1; //color--HARD-CODED TEMP //VARY THESE VALUES WITH OTHER FACTORS? //Not sure yet whether these should be independent for head and tail: PVector velocity; PVector acceleration; Particle() { //CTOR pH = new PVector(70,70); pT = new PVector(pH.x,pH.y); //RELATE TO pH, or better: velocity //or shutterspeed? ie, if shutterspeed>frameRate, show blur velocity = new PVector(0,0); acceleration = new PVector(0,0); } void applyForce(PVector force) { mass = mass+((1/abs(pH.x-width/2))+(1/abs(pH.y-height/2))); //increase mass toward center of screen PVector f = PVector.div(force,mass); acceleration.add(f); } void update() { velocity.add(acceleration); pH.add(velocity); acceleration.mult(0); } void display() { //HEAD fill(255,aH); //color--HARD-CODED TEMP noStroke(); ellipse(pH.x,pH.y,rH,rH); //TAIL //fill(255,0,0,aT); //RED--HARD-CODED TEMP fill(255,150,0,aT); //YELLOW--HARD-CODED TEMP //fill(255,aT); //WHITE--HARD-CODED TEMP [MATCHES HEAD] //VARY COLOR WITH OTHER FACTORS? pushMatrix(); // Translate to location to render particle translate(pH.x,pH.y); //stroke(0); // Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate rotate(velocity.heading2D()); // Calculate length of vector & scale it to be bigger or smaller if necessary float scalar = 3;//--HARD-CODED TEMP & CHANGE VARIABLE NAME float body = velocity.mag()*scalar; float bodyConstraint = constrain(body,0,rH+1); ellipse(-bodyConstraint,0,rT,rT); //Draw the "tail" portion of the particle popMatrix(); checkEdges(); //KEYPRESS BOOL to see velocity vector if (keyPressed == true) { drawVector(velocity,pH,10); } } void checkEdges() { if (pH.x > width + rH) { //width + particle radius (IMPLEMENTED AS DIAMETER) pH.x = 0 - rH; } if (pH.x < 0 - rH) { pH.x = width + rH; } if (pH.y > height + rH) { pH.y = 0 - rH; } if (pH.y < 0 - rH) { pH.y = height + rH; } } // From Dan Shiffman's "Simple Motion with PVector" // Renders a vector object 'v' as an arrow and a location 'loc' void drawVector(PVector v, PVector loc, float scayl) { pushMatrix(); float arrowsize = 4; // Translate to location to render vector translate(loc.x,loc.y); stroke(255); // Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate rotate(v.heading2D()); // Calculate length of vector & scale it to be bigger or smaller if necessary float len = v.mag()*scayl; // Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction) line(0,0,len,0); line(len,0,len-arrowsize,+arrowsize/2); line(len,0,len-arrowsize,-arrowsize/2); popMatrix(); } //Eliminate particles at center of screen: boolean isDead() { if ((abs(pH.x - width/2) < 5) && (abs(pH.y - height/2) < 5)) { return true; } else { return false; } } }