Each particle system with a different color?

Hello creative coders ! I am learning processing, and am working a code with multiple particle systems.

I used kind of the same structure as dans shifman's nature of code example, system of systems, in which a new particle system appears when you click the mouse (see code here)

I want to do something kind of simple. Everytime i click the mouse, a particle system of a different color appears.

My color is defined in the Particle class with a function using noise :

  void col(){
    colorMode(HSB, 360, 100, 100);
    c = map(noise(ct), 0, 1, 140, 360);
    ct-= 0.05;

  }

Should I work it here or in the ParticleSystem class? In words, how can I do this?

Answers

  • Allow the Particle class to store its own color in a variable. And this variable should be initialised inside the constructor. The constructor should therefore have an additional color argument.

    BTW, the code you have given is quite stupid - line 3 does nothing at all.

  • edited March 2017

    Sorry, I corrected it. But ignore it. In shifman's example.. I would declare a new variable in the particlesystem class like : particles.add(new Particle(origin, sysCol));

    class Particle {
      PVector position;
      PVector velocity;
      PVector acceleration;
      float lifespan;
    
      Particle(PVector l, float col) {
        acceleration = new PVector(0,0.05);
        velocity = new PVector(random(-1,1),random(-2,0));
        position = l.get();
        lifespan = 255.0;
      }
    

    To display it :

    void display() {
      fill(col);
      ellipse(position.x,position.y,12,12);
    }
    

    But if I put col = random(255); to have a random greyscale value here, every particle would have a different color. I guess It needs to be in the ParticleSystem class.

    What I don't get is how modify/use a variable from a class to another. Sorry im just getting started

  • edited March 2017 Answer ✓

    You should have the color for the particle store like this:-

    class Particle {
      PVector position;
      PVector velocity;
      PVector acceleration;
      float lifespan;
      float col;
    
      Particle(PVector l, float col) {
        acceleration = new PVector(0,0.05);
        velocity = new PVector(random(-1,1),random(-2,0));
        position = l.get();
        lifespan = 255.0;
        this.col = col;
      }
    }
    
  • Yes, @xxMrPHDxx you're probably right. But the OP's comments are a bit unclear, so the exact implementation may be different.

  • Yay thank you both ! Had no idea aboutthis.

  • Then research about it, it's important.

  • https://processing.org/reference/this.html

    Using this is often unnecessary / redundant, but it can add clarity.

  • But sometimes it's better/nescessary to use this because there's just no other way for a variable to pass itself into a function, if you understand me.

Sign In or Register to comment.