/** * A particle. Everything in this universe is a particle. * Nick Selvaggio * 12.02.09 **/ class Particle { PVector loc; PVector vel; PVector acc; PVector tF; //total force on this particle. float mass; color myColor; float t; //time. it keeps going. float lifespan; //its our destiny. //----------------------------------------------------- Particle(PVector location) { loc = location; acc = new PVector(); vel = new PVector(random(-1, 1), random(-1, 1)); mass = 4; lifespan = random(350); tF = new PVector(); myColor = someColor(); } Particle(PVector location, float masa) { loc = location; acc = new PVector(); vel = new PVector(random(-1, 1), random(-1, 1)); mass = masa; lifespan = random(350); t = 0; //the beginning of time for this particle. tF = new PVector(); myColor = someColor(); } //----------------------------------------------------- //applies an array of forces to this particle. void applyForces(ArrayList f) { tF.mult(0); //clear the total force out. it changes. for(int i=0; i< f.size(); i++) { tF.add((PVector)f.get(i)); } newtonize(); } //------------------------------------------------------- void applyForce(PVector f) { //tF.mult(0); tF.add(f); newtonize(); } void newtonize() { //a = f/m //checkWalls(); //break it down... Newton style. F=ma tF.div(mass); acc.add(tF); vel.add(acc); loc.add(vel); acc.mult(0); t++; } void render() { //output a sphere. smooth(); fill(myColor); noStroke(); ellipse(loc.x, loc.y, mass, mass); //output a point. //stroke(myColor); //point(loc.x, loc.y); } void die() { //life is over... it was fun. //drop the particle off the screen using a gravitational force. applyForce(new PVector(0, 9.8)); } void checkWalls() { //lets check our boundaries. if(loc.x > width || loc.x < 0) { vel.x = -vel.x; } if(loc.y > height || loc.y < 0) { vel.y = -vel.y; } } void lineTo(Particle p) { smooth(); stroke(255); line(loc.x, loc.y, p.loc.x, p.loc.y); } }