diff --git a/java/examples/Topics/Simulate/MultipleParticleSystems/CrazyParticle.pde b/java/examples/Topics/Simulate/MultipleParticleSystems/CrazyParticle.pde index 02b2dfa97..9c77dc6e3 100644 --- a/java/examples/Topics/Simulate/MultipleParticleSystems/CrazyParticle.pde +++ b/java/examples/Topics/Simulate/MultipleParticleSystems/CrazyParticle.pde @@ -12,7 +12,6 @@ class CrazyParticle extends Particle { super(l); // One more line of code to deal with the new variable, theta theta = 0.0; - } // Notice we don't have the method run() here; it is inherited from Particle @@ -21,27 +20,21 @@ class CrazyParticle extends Particle { void update() { super.update(); // Increment rotation based on horizontal velocity - float theta_vel = (vel.x * vel.mag()) / 10.0f; + float theta_vel = (velocity.x * velocity.mag()) / 10.0f; theta += theta_vel; } - // Override timer - void timer() { - timer -= 0.5; - } - - // Method to display - void render() { + // This display() method overrides the parent class display() method + void display() { // Render the ellipse just like in a regular particle - super.render(); - + super.display(); // Then add a rotating line pushMatrix(); - translate(loc.x,loc.y); + translate(location.x,location.y); rotate(theta); - stroke(255,timer); + stroke(255,lifespan); line(0,0,25,0); popMatrix(); } -} +} diff --git a/java/examples/Topics/Simulate/MultipleParticleSystems/MultipleParticleSystems.pde b/java/examples/Topics/Simulate/MultipleParticleSystems/MultipleParticleSystems.pde index 9ab20cdef..5acfc96e6 100644 --- a/java/examples/Topics/Simulate/MultipleParticleSystems/MultipleParticleSystems.pde +++ b/java/examples/Topics/Simulate/MultipleParticleSystems/MultipleParticleSystems.pde @@ -1,13 +1,13 @@ /** * Multiple Particle Systems - * by Daniel Shiffman. - * + * by Daniel Shiffman. + * * Click the mouse to generate a burst of particles - * at mouse location. - * + * at mouse location. + * * Each burst is one instance of a particle system * with Particles and CrazyParticles (a subclass of Particle) - * Note use of Inheritance and Polymorphism here. + * Note use of Inheritance and Polymorphism here. */ ArrayList systems; @@ -24,7 +24,6 @@ void draw() { ps.run(); ps.addParticle(); } - if (systems.isEmpty()) { fill(255); textAlign(CENTER); @@ -35,13 +34,3 @@ void draw() { void mousePressed() { systems.add(new ParticleSystem(1, new PVector(mouseX, mouseY))); } - - - - - - - - - - diff --git a/java/examples/Topics/Simulate/MultipleParticleSystems/Particle.pde b/java/examples/Topics/Simulate/MultipleParticleSystems/Particle.pde index b519208ca..078ef23c5 100644 --- a/java/examples/Topics/Simulate/MultipleParticleSystems/Particle.pde +++ b/java/examples/Topics/Simulate/MultipleParticleSystems/Particle.pde @@ -31,14 +31,10 @@ class Particle { fill(255,lifespan); ellipse(location.x,location.y,8,8); } - + // Is the particle still useful? boolean isDead() { - if (lifespan < 0.0) { - return true; - } else { - return false; - } + return (lifespan < 0.0); } -} +} diff --git a/java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde b/java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde index 1088f5c85..d387e8444 100644 --- a/java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde +++ b/java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde @@ -1,4 +1,6 @@ -// An ArrayList is used to manage the list of Particles +// An ArrayList is used to manage the list of Particles +// An Iterator is used to remove "dead" particles while iterating over the list +import java.util.Iterator; class ParticleSystem { @@ -14,7 +16,7 @@ class ParticleSystem { } void run() { - // Cycle through the ArrayList using Iterator we are deleting + // Cycle through the ArrayList using Iterator, because we are deleting while iterating Iterator it = particles.iterator(); while (it.hasNext()) { Particle p = it.next(); @@ -26,7 +28,14 @@ class ParticleSystem { } void addParticle() { - particles.add(new Particle(origin)); + Particle p; + // Add either a Particle or CrazyParticle to the system + if (int(random(0, 2)) == 0) { + p = new Particle(origin); + } else { + p = new CrazyParticle(origin); + } + particles.add(p); } void addParticle(Particle p) { @@ -35,11 +44,7 @@ class ParticleSystem { // A method to test if the particle system still has particles boolean dead() { - if (particles.isEmpty()) { - return true; - } else { - return false; - } + return particles.isEmpty(); } -} +} \ No newline at end of file