/* Our particle system is a simple collection of a variable number of Particle objects. The particle system provides a few utility methods to easily run simulations on all of the particles */ class ParticleSystem { // The list of particles ArrayList particles; // The source of the particle system Vector3D origin; // The image we're using to draw our system PImage img; ParticleSystem(int num, Vector3D origin_, PImage img_) { // Set the particles array particles = new ArrayList(); // Save the center of the simulation origin = origin_.copy(); // save the rendering image img = img_; // Create all of the starting particles for (int i = 0; i < num; i++) { particles.add(new Particle(origin, img)); } } void simulate() { /* This simulate method is a bit different than what we're used too. Because particles in our system can die out, we need to remove particles that are 'dead'. To make this efficient we want to iterate over the list of particles once to run the simualtion, removing those that are already 'dead'. To do this, we count backwards through our array, so that when we remove a particle (element) from the array, we don't change the order or position of elements we haven't yet processed. */ for (int i = particles.size() - 1; i >= 0; i--) { // Get our particle; we need to cast the particle into its type Particle p = (Particle)particles.get(i); // Run the simulation p.simulate(); // Remove any dead particles if (p.isDead()) { particles.remove(i); } } } void wind(Vector3D wind_) { for (int i = 0; i < particles.size(); i++) { Particle p = (Particle)particles.get(i); p.addForce(wind_); } } void addParticle() { particles.add(new Particle(origin, img)); } boolean isDead() { if (particles.isEmpty()) { return true; } else { return false; } } }