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

Daily Creative Coding

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

円から産まれるパーティクル

パーティクル インタラクション
/**
* particles from eggs
*
* @author aa_debdeb
* @date 2016/04/07
*/

float P_RADIOUS = 7;

float radious;
ArrayList<Particle> particles;

void setup(){
  size(500, 500);
  noStroke();
  radious = 0;
  particles = new ArrayList<Particle>();
}

void draw(){
  background(#FEF7D5);
  if(mousePressed){
    if(radious < 200){
      radious += 2;
    }
    fill(#E06A3B);
    ellipse(mouseX, mouseY, radious * 2, radious * 2);
  }
  
  ArrayList<Particle> nextParticles = new ArrayList<Particle>();
  for(Particle particle: particles){
    particle.update();
    particle.display();
    if(!particle.isDead()){
      nextParticles.add(particle);
    }
  }
  particles = nextParticles;
}

void mouseReleased(){
  
  if(radious > P_RADIOUS){
    int num = int(sq(radious) * 0.06);
    for(int i = 0; i < num; i++){
      float ang = random(TWO_PI);
      float r = random(radious - P_RADIOUS);
      PVector pos = new PVector(mouseX + r * cos(ang), mouseY + r * sin(ang));
      particles.add(new Particle(pos));
    }
  }
  radious = 0;
}

class Particle{
  PVector pos, vel;
  float life;
  
  Particle(PVector pos){
    this.pos = pos;
    vel = new PVector(0, 0);
    life = 1.0;
  }
  
  void update(){
    float accAng = random(TWO_PI);
    float accSize = random(0.1, 0.2);
    PVector acc = new PVector(accSize * cos(accAng), accSize * sin(accAng));
    vel.add(acc);
    pos.add(vel);
    life *= random(0.98, 0.995);
  }
  
  void display(){
    float r = P_RADIOUS * sqrt(life);
    float a = 255 * pow(life, 1.0 / 3);
    fill(#E06A3B, a);
    ellipse(pos.x, pos.y, r * 2, r * 2);
  }
  
  boolean isDead(){
    return life < 0.01;
  }
}