import java.util.Random; import processing.core.PApplet; import processing.core.PImage; // hacked from shiffman (thanks :) // A simple Particle class, renders the particle as an image public class Particle { Vector3D loc; Vector3D vel; Vector3D acc; PImage img[]; // this particle's texture PImage currentImage; Random generator; // rng PApplet p; // the parent rendering environment // other info float scale = 1; // the particle's texture scale float life = 0; // the particle's life float timer = 100; // the particles life timer // Another constructor (the one we are using here) public Particle(PApplet p, Vector3D l, PImage[] img_) { // need a Vector for initial location generator = new Random(); this.p = p; acc = new Vector3D(0.0F, 0.0F, 0.0F); float x = 0;//(float) generator.nextGaussian() * 0.3f; float y = 0;// (float) generator.nextGaussian() * 0.3f; - 1.0f; vel = new Vector3D(x, y, 0); loc = l.copy(); img = img_; currentImage = img[(int)p.random(0,3)]; // life range life = p.random(50,100); timer = life; // scale range scale = p.random(.6f,1.5f); } void run() { update(); render(); } // Method to apply a force vector to the Particle object // Note we are ignoring "mass" here void add_force(Vector3D f) { acc.add(f); } // Method to update location void update() { vel.add(acc); loc.add(vel); timer -= 1; acc.setXYZ(0, 0, 0); } // Method to display void render() { float lifePercent = PApplet.constrain(timer/(life),0,1); // move to 0,1 p.pushMatrix(); p.translate( loc.x - currentImage.width / 2, loc.y - currentImage.height / 2); p.scale(PApplet.lerp(scale,.1F, lifePercent)); float transitionPoint0 = .6f; // the flame / smoke tranistion point float transitionPoint1 = .4f; int startFireColor = p.color(255,255,0,100); int endFireColor = p.color(255,0,0,0); int startSmokeColor = p.color(0,0); int middleSmokeColor = p.color(50,20); int endSmokeColor = startSmokeColor; if(lifePercent >= transitionPoint0) { // i made fire!! // yellow // red invisible p.tint(p.lerpColor(startFireColor,endFireColor, PApplet.norm(lifePercent, 1,transitionPoint0 ))); } else if (lifePercent >= transitionPoint1) { // currentImage = img[3]; // give me smoky textures // turn into smoke! // p.tint(p.lerpColor(startSmokeColor,middleSmokeColor, PApplet.norm(lifePercent, transitionPoint0,transitionPoint1))); // smoke } else { // smoke goes away p.tint(p.lerpColor(middleSmokeColor,endSmokeColor, PApplet.norm(lifePercent, transitionPoint1,0))); // smoke } p.imageMode(PApplet.CORNER); p.image(currentImage,currentImage.width/2,0); p.popMatrix(); } // Is the particle still alive? boolean dead() { if (timer <= 0.0) { return true; } else { return false; } } }