ParticleManager.update()

Hi all,

I was rendering clones of an particleManager with the bounds on. And i noticed that when the ParticleManager’s repeat type is clamp, and when it reached the final, it didn’t set Active as false, which could save up on quite alot of computational power.



This is useful to see if all the particles are dead and you want to remove the particles from the scene to save up on those prescious vertices. The following code does it nicely:



Declare the int:


   private int deadParticles;



In the constructor:


public ParticleManager(int noParticles, Camera cam) {
      camera = cam;
      this.noParticles = noParticles;
      deadParticles = 0;<-- new here



In the update(float time) method:


int i = 0;
            while (i < noParticles) {
               if (particles[i].updateAndCheck(timePassed)
                     && (!controlFlow || particlesToCreate > 0)) {
                  if (particles[i].status == Particle.DEAD
                        && getRepeatType() == RT_CLAMP) {
                     deadParticles++;//<-- new here
                  } else {



Also in update:


   particlesGeometry.setVertices(geometryCoordinates);
            particlesGeometry.setColors(appearanceColors);
         }
         if (particlesGeometry.getModelBound() != null) {
            particlesGeometry.getModelBound()
                  .computeFromPoints(particlesGeometry.getVertices());
         }
         
         // this if statement is new
         if (deadParticles == noParticles && getRepeatType() == Controller.RT_CLAMP) {
            setActive(false);
         }
         
      }
   }



This is very useful for:


   private void checkPManagerArray() {
      for (int i = 0; i < pManagerArray.size(); i++) {
         ParticleManager pM = (ParticleManager) pManagerArray.get(i);
         if (pM.isActive() == false) {
            rootSceneNode.detachChild(pM.getParticles());
            pManagerArray.remove(i);
         }
      }
   }



Enjoy! DP

actually, it can be handled with a single boolean:


        int i = 0;
        boolean dead = true; // <-- new
        while (i < noParticles) {
          if (particles[i].updateAndCheck(timePassed) &&
              (!controlFlow || particlesToCreate > 0)) {
            if (particles[i].status == Particle.DEAD &&
                getRepeatType() == RT_CLAMP) {
              ;
            } else {
              dead = false;  // <-- new
              if (controlFlow) {
                released++;
                particlesToCreate--;
              }


and then:

              particles[i].updateVerts();
            }
          }
          else dead = false;    // <-- new
          i++;
        }

        particlesGeometry.setVertices(geometryCoordinates);
        particlesGeometry.setColors(appearanceColors);
        if (dead) setActive(false);   // <-- new



Tested it with TestParticleSystem and seems to work just peachy.

Knew you would come up with something cool. Hehehe



And yes, i dont have much to do in my free time :stuck_out_tongue:



DP

committed per mojo’s ok on IM