/examples/advanced/simpleParticles/ParticleSystem.java

http://mt4j.googlecode.com/ · Java · 80 lines · 54 code · 19 blank · 7 comment · 5 complexity · 217a8197d7cfdde5c2051f272c75ce70 MD5 · raw file

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