/examples/advanced/simpleParticles/ParticleSystem.java
Java | 80 lines | 54 code | 19 blank | 7 comment | 5 complexity | 217a8197d7cfdde5c2051f272c75ce70 MD5 | raw file
1package advanced.simpleParticles; 2 3import java.util.ArrayList; 4 5import javax.media.opengl.GL; 6 7import org.mt4j.util.math.Tools3D; 8 9 10import processing.core.PApplet; 11import processing.core.PGraphics; 12import processing.core.PVector; 13 14// A class to describe a group of Particles 15// An ArrayList is used to manage the list of Particles 16 17public class ParticleSystem { 18 19 private ArrayList<Particle> particles; // An arraylist for all the particles 20 private PVector origin; // An origin point for where particles are born 21 private PApplet app; 22 23 public ParticleSystem(PApplet app, int num, PVector v) { 24 this.app = app; 25 particles = new ArrayList<Particle>(); // Initialize the arraylist 26 origin = v.get(); // Store the origin point 27 for (int i = 0; i < num; i++) { 28 particles.add(new Particle(app, origin)); // Add "num" amount of particles to the arraylist 29 } 30 } 31 32 public void run(PGraphics g) { 33 GL gl = Tools3D.getGL(g); 34 gl.glDisable(GL.GL_DEPTH_TEST); 35// gl.glDepthMask(false);//depth testing - makes depth buffer read-only 36// gl.glBlendFunc(GL.GL_SRC_ALPHA,GL.GL_ONE);//define blending as alpha blending 37 gl.glBlendFunc(GL.GL_ONE,GL.GL_ONE);//define blending as alpha blending 38 39 // Cycle through the ArrayList backwards b/c we are deleting 40 for (int i = particles.size()-1; i >= 0; i--) { 41 Particle p = (Particle) particles.get(i); 42 p.run(g); 43 if (p.isDead()) { 44 particles.remove(i); 45 } 46 } 47 48 gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); 49// gl.glDepthMask(true);//depth testing - makes depth buffer read-only 50 gl.glEnable(GL.GL_DEPTH_TEST); 51 } 52 53 public void addParticle() { 54 particles.add(new Particle(app, origin)); 55 } 56 57 public void addParticle(float x, float y) { 58 particles.add(new Particle(app, new PVector(x,y))); 59 } 60 61 public void addParticle(Particle p) { 62 particles.add(p); 63 } 64 65 public void addParticle(Particle p, float x, float y){ 66 p.loc.set(x,y,0); 67 particles.add(p); 68 } 69 70 // A method to test if the particle system still has particles 71 public boolean isDead() { 72 if (particles.isEmpty()) { 73 return true; 74 } else { 75 return false; 76 } 77 } 78 79} 80