Stop Particles From Being Erased

I'm pretty new at this so please excuse my bad terminology. I have a project that requires every frame to be redrawn for the animation of particles and fluid then to fade them after creation but I want to add a background particle effect that every particle stays on the screen until the sketch is stopped. I have everything working and appearing on screen but the background branching particle effect is redrawn and thus only the newest particles appear and unfortunately overlay the animated particles.

Is there any way to draw the background branch particles behind the fluid and to keep them from being erased every frame without effecting the fluid fade?

Answers

  • Answer ✓

    Always redraw everything every frame. Do not depend on the what was drawn in the last frame at all. In fact, do a background(0); at the start of draw() so that you are sure you're not relying on previous frames.

    If this ruins certain effects that you like, you're actually doing the effects wrong. For example, this is nice, simple, and totally wrong:

    void setup(){
      size(220,220);
      background(0);
      noStroke();
    }
    
    void draw(){
      fill(0,196,0);
      ellipse(mouseX,mouseY,20,20);
      fill(0,0,0,5);
      rect(0,0,width,height);
    }
    

    This is complex, a hassle, but much more correct:

    ArrayList<PVector> al = new ArrayList();
    
    void setup(){
      size(220,220);
      noStroke();
    }
    
    void draw(){
      background(0);
      al.add( new PVector( mouseX, mouseY, 196) );
      while(al.size() > 255 ) al.remove(0);
      for(int i = 0;i<al.size();i++){
        al.get(i).z--;
        fill(0,al.get(i).z,0);
        ellipse(al.get(i).x,al.get(i).y,20,20);
      }
    }
    
  • Answer ✓

    TfGuy44 is a bit extremist. Code that doesn't clear background can be useful to get some effects, although it is very rarely practical. Most sketches just do the "redraw everything" thing.
    Now, if I understood correctly the problem, perhaps you can draw your (animated?) background on an off-line PGraphics, and thus display it at the start of draw() in a background() call.

  • Thank you PhiLho I had a feeling this was the solution I was looking for but I wasn't sure. Thanks so much for the response, really helped me solidify my understanding.

Sign In or Register to comment.