/* 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 = 20; //head radius--HARD-CODED TEMP float rT = rH*0.8; //tail radius--HARD-CODED TEMP float aH = 150; //head opacity--HARD-CODED TEMP float aT = 255; //tail opacity--HARD-CODED TEMP float mass = 1; //color--HARD-CODED TEMP float r,g,b; //Not sure yet whether these should be independent for head and tail: PVector velocity; PVector acceleration; //Particle() { //CTOR Particle(float pX_, float pY_) { //CTOR //Particle(float r_, float g_, float b_) { //CTOR //pH = new PVector(0,0); pH = new PVector(pX_,pY_); pT = new PVector(0,0); //RELATE TO pH, or better: velocity //or shutterspeed? ie, if shutterspeed>frameRate, show blur //r=r_; //g=g_; //b=b_; velocity = new PVector(0,0); acceleration = new PVector(0,0); } void applyForce(PVector force) { PVector f = PVector.div(force,mass); acceleration.add(f); } void update() { velocity.add(acceleration); pH.add(velocity); acceleration.mult(0); } void display(color c) { //HEAD //getColor(); //fill(255,aH); //color--HARD-CODED TEMP fill(c,aH); //fill(r,g,b,aH); noStroke(); //ellipse(pH.x,pH.y,rH*2,rH*2); //Draw the "head" portion of the particle rectMode(CENTER); rect(pH.x,pH.y,rH*2,rH*2); //TAIL //fill(255,0,0,aT); //color--HARD-CODED TEMP pushMatrix(); // Translate to head location to render particle translate(pH.x,pH.y); // 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 = 7; //--HARD-CODED TEMP & CHANGE VARIABLE NAME float body = velocity.mag()*scalar; //println(velocity.mag()); //ellipse(-body,0,rT*2,rT*2); //Draw the "tail" portion of the particle rect(-body,0,rT*2,rT*2); 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 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; } } }