読者です 読者をやめる 読者になる 読者になる

Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

漂う発光体

/**
* floating illuminants
*
* @author aa_debdeb
* @date 2016/09/04
*/

ArrayList<Particle> particles;

void setup(){
  size(640, 640);
  noStroke();
  particles = new ArrayList<Particle>();
}
  
void draw(){
  background(0);
  ArrayList<Particle> nextParticles = new ArrayList<Particle>();
  for(Particle p: particles){
    p.display();
    p.update();
    if(p.isIn()){
      nextParticles.add(p);
    }
  }
  particles = nextParticles;
  if(mousePressed){
    int num = int(random(5));
    for(int i = 0; i < num; i++){
      particles.add(new Particle(mouseX, mouseY));
    }
  }
}

class Particle{
  
  PVector loc, vel;
  float state;
  float diameter;
  color c;
 
  Particle(float x, float y){
    loc = new PVector(x, y);
    vel = new PVector(0, 0);
    state = random(TWO_PI);
    diameter = random(1, 3);
    c = color(random(133, 213), 255, random(17, 77));
  }
  
  void display(){
    float alpha = map(sin(state), -1, 1, 0, 255);
    fill(red(c), green(c), blue(c), alpha);
    ellipse(loc.x, loc.y, diameter, diameter);  
  }
  
  void update(){
    float accAng = random(TWO_PI);
    float accSize = random(0.1);
    PVector acc = new PVector(accSize * cos(accAng), accSize * sin(accAng));
    vel.add(acc);
    vel.limit(1);
    loc.add(vel);
    state += random(PI / 64);
    if(state >= TWO_PI){
      state -= TWO_PI;
    }
  }
  
  boolean isIn(){
    return 0 <= loc.x && loc.x <= width && 0 <= loc.y && loc.y <= height;
  }
  
}
f:id:aa_debdeb:20160831144604j:plain