fixing up smoke particle system

This commit is contained in:
Daniel Shiffman
2013-03-06 16:46:31 -05:00
parent 5d09d85a78
commit c951d4a31b
3 changed files with 15 additions and 31 deletions
@@ -10,8 +10,8 @@ class Particle {
Particle(PVector l,PImage img_) {
acc = new PVector(0,0);
float vx = (float) generator.nextGaussian()*0.3;
float vy = (float) generator.nextGaussian()*0.3 - 1.0;
float vx = randomGaussian()*0.3;
float vy = randomGaussian()*0.3 - 1.0;
vel = new PVector(vx,vy);
loc = l.get();
lifespan = 100.0;
@@ -33,8 +33,8 @@ class Particle {
void update() {
vel.add(acc);
loc.add(vel);
acc.mult(0); // clear Acceleration
lifespan -= 2.5;
acc.mult(0); // clear Acceleration
}
// Method to display
@@ -42,10 +42,14 @@ class Particle {
imageMode(CENTER);
tint(255,lifespan);
image(img,loc.x,loc.y);
// Drawing a circle instead
// fill(255,lifespan);
// noStroke();
// ellipse(loc.x,loc.y,img.width,img.height);
}
// Is the particle still useful?
boolean dead() {
boolean isDead() {
if (lifespan <= 0.0) {
return true;
} else {
@@ -54,6 +58,3 @@ class Particle {
}
}
@@ -4,25 +4,24 @@
class ParticleSystem {
ArrayList<Particle> particles; // An arraylist for all the particles
PVector origin; // An origin point for where particles are birthed
PVector origin; // An origin point for where particles are birthed
PImage img;
ParticleSystem(int num, PVector v, PImage img_) {
particles = new ArrayList<Particle>(); // Initialize the arraylist
origin = v.get(); // Store the origin point
origin = v.get(); // Store the origin point
img = img_;
for (int i = 0; i < num; i++) {
particles.add(new Particle(origin, img)); // Add "num" amount of particles to the arraylist
particles.add(new Particle(origin, img)); // Add "num" amount of particles to the arraylist
}
}
void run() {
Iterator<Particle> it = particles.iterator();
while (it.hasNext()) {
Particle p = it.next();
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.dead()) {
it.remove();
if (p.isDead()) {
particles.remove(i);
}
}
}
@@ -40,19 +39,6 @@ class ParticleSystem {
particles.add(new Particle(origin,img));
}
void addParticle(Particle p) {
particles.add(p);
}
// A method to test if the particle system still has particles
boolean dead() {
if (particles.isEmpty()) {
return true;
} else {
return false;
}
}
}
@@ -11,14 +11,11 @@
/* @pjs preload="texture.png"; */
ParticleSystem ps;
Random generator;
void setup() {
size(640,360);
generator = new Random();
PImage img = loadImage("texture.png");
ps = new ParticleSystem(0,new PVector(width/2,height-60),img);
smooth();
}
void draw() {