Alternating colour of particles - sparkle effect

edited May 2015 in How To...

Hi there, I'm pretty new to Programming as I'm a first year of an interactive media course in York.

But I currently have a working particle system, using a linked list to produce fireworks with is all working! But i'd like to add an effect to the particles that makes them flash as they fall to the ground. I was wondering if anybody has an idea to fix this. Somehow making the particle alternate between a colour and black? Making a sparkle affect... Hoping it will be a simple fix.

Any suggestions would be much appreciated! Many thanks in advance :)

Answers

  • Thanks, but I've already had a look at that example and it doesn't quite achieve what I want.. Thanks though! any other suggestions....?

  • Yeah, if your particles are objects, draw them only some of the time. I added this effect to my Octree of cubes with only three additional lines:

    boolean[] bx = {
      false, true, false, true, false, true, false, true
    };
    boolean[] by = {
      false, false, true, true, false, false, true, true
    };
    boolean[] bz = {
      false, false, false, false, true, true, true, true
    };
    
    float rx, ry;
    
    
    class Octree {
      boolean hasChilds;
      Octree[] childs;
      color c;
      PVector p;
      PVector o = new PVector(0, 0, 0);
      PVector v = new PVector(random(-1,1),random(-1,1),random(-1,1));
      float s;
      int my_level;
      int flashrate = int(random(5000));
      Octree(float is, PVector ip, boolean haveChilds, int level) {
        s = is;
        p = ip;
        hasChilds = haveChilds;
        my_level = level;
        if ( hasChilds ) {
          childs = new Octree[8];
          float half_s = s/2;
          for (int i=0; i<8; i++) {        
            childs[i] = new Octree(
            half_s, 
            new PVector(
            p.x+(bx[i]?half_s:0), 
            p.y+(by[i]?half_s:0), 
            p.z+(bz[i]?half_s:0)
              ), 
            my_level < 3 && random(1) < .6, 
            my_level+1
              );
          }
        } else {
          float r = random(255);
          c = color(r, r, random(r, 255)); //, random(255), random(255));
        }
      }
      void draw() {
        if ( hasChilds ) {
          for (int i=0; i<8; i++) {
            childs[i].draw();
          }
        } else {
          fill(c);
          noStroke();
          //      stroke(c);
          //      noFill();
          pushMatrix();
          translate(p.x+o.x, p.y+o.y, p.z+o.z);
          translate(s/2, s/2, s/2);
          if(millis()%(2*flashrate)<flashrate){
            box(s*map(abs(-10000+millis()%20000), 0, 10000, .3, 1));
          }
          popMatrix();
          if (!lock) {
            o.x=.9*o.x;
            o.y=.9*o.y;
            o.z=.9*o.z;
          } else {
            o.x+=v.x;//random(-10,10);
            o.y+=v.y;//random(-10,10);
            o.z+=v.z;//random(-10,10);
          }
        }
      }
    }
    
    Octree ot = new Octree( 400, new PVector(0, 0, 0), true, 0);
    float mx, my;
    boolean lock = false;
    
    void setup() {
      size(1200, 800, P3D);
    }
    
    void draw() {
      background(64);
      translate(width/2, height/2, 0);
      pushMatrix();
      scale(600);
      lights();
      popMatrix();
      if (!lock) {
        mx = mouseX; 
        my=mouseY;
      }
      ry+=.005;
      rx+=.006;
      ry%=TWO_PI;
      rx%=TWO_PI;
      rotateY(ry);//map(mx, 0, width, -PI, PI));    
      rotateX(rx);//map(my, 0, height, -PI, PI));
      translate(-200, -200, -200);
      ot.draw();
    }  
    
    void mousePressed() {
      lock = !lock;
      mx = mouseX;
      my = mouseY;
    }
    
  • edited May 2015

    Oh, the lines are those that use the variable "flashrate", and the } that ends the if statement that's the second use of it.

    Of course, you might still want to draw your particle in black, in which case you'd change the drawing color based on some conditional statement involving your Particle's flashing rate.

    Post your working code and your attempt at this change for more help.

  • Thank you so much for this code! very impressive! I'll try this tomorrow with my code and let you know how it goes. cheers

Sign In or Register to comment.