Daily Creative Coding

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

爆発するパーティクル

/**
* Explosion Particle
*
*
* @author aa_debdeb
* @date 2015/10/04
*/


float MAX_RADIOUS = 20;
float MAX_SPEED = 5;
float MAX_RECURSION = 3;

float EXPLOSION_RATE = 0.1;
float NEW_PARTICLE_RATE = 0.1;

ArrayList<Particle> particles;

void setup(){
  size(500, 500);
  smooth();
  frameRate(24);
  noStroke();
  
  particles = new ArrayList<Particle>();
  particles.add(new Particle());
  
}

void draw(){
  
  colorMode(RGB, 256);
  fill(0);
  rect(0, 0, width, height);

  for(Particle particle : particles){
    particle.draw();
  }
  
  for(Particle particle : particles){
    particle.update();
  }
  
  ArrayList<Particle> addParticles = new ArrayList<Particle>();
  ArrayList<Particle> deleteParticles = new ArrayList<Particle>();
  for(Particle particle: particles){
    if(random(1) < EXPLOSION_RATE){
      if(particle.recursion == MAX_RECURSION){
        deleteParticles.add(particle);
      } else {
        deleteParticles.add(particle);
        ArrayList<Particle> childrens = particle.makeChildrens();
        for(Particle children : childrens){
          addParticles.add(children);        
        }
      }
    }
  }  
  for(Particle particle : deleteParticles){
    particles.remove(particle);
  } 
  for(Particle particle: addParticles){
    particles.add(particle);
  }
  
  if(random(1) < NEW_PARTICLE_RATE){
    particles.add(new Particle());
  }
  
}

class Particle{
  PVector position;
  PVector velocity;
  float radious;
  float transparency;
  float recursion;
  float colorH;
  float colorS;
  float colorB;
  
  Particle(){
    position = new PVector(random(width), random(height));
    velocity = new PVector(random(MAX_SPEED * 2) - MAX_SPEED, random(MAX_SPEED * 2) - MAX_SPEED);
    radious = MAX_RADIOUS;
    transparency = 255;
    recursion = 0;
    colorH = random(360);
  }
  
  Particle(Particle parent){
      position = new PVector(parent.position.x, parent.position.y);
      velocity = new PVector(random(MAX_SPEED * 2) - MAX_SPEED, random(MAX_SPEED * 2) - MAX_SPEED);
      radious = parent.radious / sqrt(2);
      transparency = parent.transparency / sqrt(2);
      recursion = parent.recursion + 1;
      colorH = parent.colorH;
  }
  
  ArrayList<Particle> makeChildrens(){
    ArrayList<Particle> childrens = new ArrayList<Particle>();
    for(int i = 0; i < 4; i++){
      childrens.add(new Particle(this));
    }
    return childrens;
  }
  
  void draw(){
    colorMode(HSB, 360, 100, 100);
    fill(colorH, 100, 100, transparency);
    arc(position.x, position.y, radious * 2, radious * 2, 0, PI * 2);
  }
  
  void update(){
    position.add(velocity);
  }
}
http://aa-debdeb.tumblr.com/post/130466591998/http30min-processinghatenablogcomentry20151