Questions about gradually lighting up

Hello! Here is my project of the fireflies that will run away when the mouse is on the left side of the screen, I’m wondering whether there is a way to light each partical (firefly) gradually instead of just suddenly light them all up. Could any one help me with that part? Thanks so much!

// glowworm version, mouse control
//***3 type, not on the screen, dim and normal movement;on the left part, run away;on the right part,lighter
//partical num
int num = 300;
//define
Particle[] particles = new Particle[num];

void setup() {
  size(1024, 728, P3D);
  noStroke();
  mouseX = 2000;
  mouseY=9000;
  //create partical
  for (int i=0; i<num; i++) {
    PVector loc = new PVector(random(width*1.2), random(height));
    float rad = random(-TWO_PI,TWO_PI);
    PVector speed = new PVector(0, 0);
    PVector acc = new PVector(cos(rad), sin(rad));
    color col= color(0,0,0,100);
    particles[i]= new Particle(loc, speed, acc, col);
  }
}

void draw() {
  //background
  fill(0, 60);
  noStroke();
  if(mouseX>width*0.03&&mouseX<width*0.50&&mouseY<height*0.97&&mouseY>height*0.03){
    rect(0, 0, width, height);
  fill(255,215,30, 90+noise(millis())*70);  }
  else if(mouseX>=width*0.50&&mouseX<width*0.97&&mouseY<height*0.97&&mouseY>height*0.03)
  {rect(0, 0, width, height);
  fill(255,215,30, 90+noise(millis())*70); }
  else{rect(0, 0, width, height);
  fill(255,215,30, 70);
  }
  //run all partical
  for (int i=0; i<particles.length; i++) {
    particles[i].run();
  }
}

//class
class Particle {
  //vectors
  PVector loc, speed, acc;
  color col ;
  float rad;
  float maxVel = 1;
  float w = 500.0;
  float h = 500.0;
  float f = 1000.0;
  //constructor
  Particle(PVector _loc, PVector _speed, PVector _acc, color _col) {
    loc   = _loc;
    speed = _speed;
    acc   = _acc;
    col = _col;
  }

  Particle() {
    loc = new PVector(random(width), random(height));
    rad = random(TWO_PI);
    speed = new PVector(0, 0);
    acc = new PVector(cos(rad)/40, sin(rad)/40);
    col= color(0,0,0,100);
  }

  //motion+render+edge
  void run() {
    move();
    checkEdges();
    render();
  }
  //motion
  void move() {
    if(mouseX>width*0.03&&mouseX<width*0.50&&mouseY<height*0.97&&mouseY>height*0.03){
    PVector mouse = new PVector(random(40,800),random(20,700));
    PVector noise_fac = new PVector(random(-100,100),random(-150,150));
    PVector loc_new = PVector.sub(loc,noise_fac);
    PVector dir = PVector.sub(loc_new, mouse);//see if get closer or away
     float m = dir.mag();
    dir.normalize();
    dir.mult(0.9);
    
  if(m<random(200,1280)) {
    float maxVel = random(2,5);
     acc=dir;
      speed.add(acc);
    if (speed.magSq()>maxVel) {
      speed.normalize();
      speed.mult(maxVel);
    }
    loc.add(speed);
    }
    else{
      float deg = 360.0*noise(
      loc.x/w, 
      loc.y/h, 
      millis()/10000.0);

    rad=radians(deg);

    acc.set(cos(rad)/40, sin(rad)/40);

    speed.add(acc);
    if (speed.magSq()>maxVel) {
      speed.normalize();
      speed.mult(maxVel);
    }
    loc.add(speed);
   }}
    else if(mouseX>=width*0.50&&mouseX<width*0.97&&mouseY<height*0.97&&mouseY>height*0.03){
    PVector mouse = new PVector(9000,3000);
    PVector noise_fac = new PVector(random(-100,100),random(-150,150));
    PVector loc_new = PVector.sub(loc,noise_fac);
    PVector dir = PVector.sub(loc_new, mouse);//see if get closer or away
     float m = dir.mag();
    dir.normalize();
    dir.mult(0.9);
    
  if(m<random(200,1280)) {
    float maxVel = random(2,5);
     acc=dir;
      speed.add(acc);
    if (speed.magSq()>maxVel) {
      speed.normalize();
      speed.mult(maxVel);
    }
    loc.add(speed);
    }
    else{
      float deg = 360.0*noise(
      loc.x/w, 
      loc.y/h, 
      millis()/10000.0);

    rad=radians(deg);

    acc.set(cos(rad)/40, sin(rad)/40);

    speed.add(acc);
    if (speed.magSq()>maxVel) {
      speed.normalize();
      speed.mult(maxVel);
    }
    loc.add(speed);
   }}
   else{
     
    PVector mouse = new PVector(9000,3000);
    PVector noise_fac = new PVector(random(-100,100),random(-150,150));
    PVector loc_new = PVector.sub(loc,noise_fac);
    PVector dir = PVector.sub(loc_new, mouse);//see if get closer or away
     float m = dir.mag();
    dir.normalize();
    dir.mult(0.9);
    
  if(m<random(200,1280)) {
    float maxVel = 2;
     acc=dir;
      speed.add(acc);
    if (speed.magSq()>maxVel) {
      speed.normalize();
      speed.mult(maxVel);
    }
    loc.add(speed);
    }
    else{
      float deg = 360.0*noise(
      loc.x/w, 
      loc.y/h, 
      millis()/10000.0);

    rad=radians(deg);

    acc.set(cos(rad)/40, sin(rad)/40);

    speed.add(acc);
    if (speed.magSq()>maxVel) {
      speed.normalize();
      speed.mult(maxVel);
    }
    loc.add(speed);
   }
    }}
    
  
  //edge
  void checkEdges() {
    if (loc.x<0 || loc.x>width || loc.y<0 || loc.y>height) {    
      loc.x = random(width,width*1.2);
      loc.y = random(height);
    }
  }
  //render
  void render() {
     float ran=random(2,8);
    ellipse(loc.x, loc.y, ran,ran);
 
     col = color(0,0,0,100);
  }
}

First of all, this is a wall of text. Even with some snippets of code marked it’s something most people won’t even read. If you need help you should try to narrow down your problem and just present the most relevant bits.

If you have no idea where to look at all then might I suggest looking at simpler examples first?

To clarify: From the lines you marked I have no idea what makes a firefly light up. If you want to change the behavior of lighting up you should figure out where that’s controlled first.

where the part is responsible for lighting?

is this what you mean mate? im not very sure
use HSB Color mode instead.
you can easily change the brightness

color c = color(88, 40, 80);
float h, s, b;

// press g to light up
// press f to light off
void setup() {
  background(0);
  noStroke();
  size(800, 800);
  colorMode(HSB);
  b = brightness(c);
  h = hue(c);
  s = saturation(c);
}

boolean toogle = false;
void draw() {
  background(0);
  if (toogle) {
    b = lerp(b, 255, 0.06);
  } else {
    b = lerp(b, brightness(c), 0.06);
  }
  fill(h, s, b);
  ellipse(width/2, height/2, 500, 500);
}

void keyPressed() {
  if (key == 'f') {
    toogle = false;
  }
  if (key == 'g') {
    toogle = true;
  }
}
1 Like