//Base particle code for CSC 123 lab 10 //TODO: add a new particle constructor that will take in a starting color for a particle //TODO: add code so that the fire work explodes where the mouse clicks //TODO: add more then one firework //TODO: add code in order to have the shading/transparency and or shape of the particle change over time // or depending on velocity //define a particle class Particle { PVector loc; PVector vel; PVector accel; float r; float life; PVector pcolor; //constructor Particle(PVector start) { accel = new PVector(0, 0.05, 0); //gravity vel = new PVector(random(-2, 2), random(-6, 0), 0); pcolor = new PVector(random(255), random(255), random(255)); loc = start.get(); r = 8.0; life = 100; } //TODO define another constructor that allows a particle to start with a given color //what to do each frame void run() { updateP(); renderP(); } //a function to update the particle each frame void updateP() { vel.add(accel); loc.add(vel); life -= 1.0; } //how to draw a particle void renderP() { pushMatrix(); ellipseMode(CENTER); stroke(pcolor.x, pcolor.y, pcolor.z); fill(pcolor.x, pcolor.y, pcolor.z); translate(loc.x, loc.y); ellipse(0, 0, r, r); popMatrix(); } //a function to test if a particle is alive boolean alive() { if (life <= 0.0) { return false; } else { return true; } } } //end of particle object definition //now define a group of particles as a particleSys class PSys{ ArrayList particles; //all the particles PVector source; //where all the particles emit from PVector shade; //their main color //constructor PSys(int num, PVector init_loc) { particles = new ArrayList(); source = init_loc.get(); shade = new PVector(random(255), random(255), random(255)); for (int i=0; i < num; i++) { particles.add(new Particle(source)); } } //what to do each frame void run() { //go through backwards for deletes for (int i=particles.size()-1; i >=0; i--) { Particle p = (Particle) particles.get(i); //update each particle per frame p.run(); if ( !p.alive()) { particles.remove(i); } } } //is particle system still populated? boolean dead() { if (particles.isEmpty() ) { return true; } else { return false; } } } //declare a particle system PSys fireW1; int frame; void setup() { size(500, 500); colorMode(RGB, 255, 255, 255, 100); //start a new particle system fireW1 = new PSys(100, new PVector(random(width-width/2), random(height-40), 0)); smooth(); frame = 0; frameRate(40); } void draw() { background(0); //run the particle system fireW1.run(); }