Daily Creative Coding

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

ちらつくパーティクル

/**
* Flies in Center
*
* @author aa_debdeb
* @date 2016/02/05
*/

ArrayList<Particle> particles;

void setup(){
  size(500, 500);
  frameRate(30);
  
  particles = new ArrayList<Particle>();
  for(int i = 0; i < 1000; i++){
    particles.add(new Particle());
  } 
}

void draw(){
  background(0);
  noStroke();
  fill(255, 100);
  translate(width / 2, height / 2);
  for(Particle particle: particles){
    particle.display();
    particle.update();
  }
  
  
}

class Particle{

  PVector position, velocity;
  
  Particle(){
    float posAng = map(random(1), 0, 1, 0, TWO_PI);
    float posMag = map(random(1), 0, 1, 0, 200);
    position = new PVector(posMag * cos(posAng), posMag * sin(posAng));
    float velAng = map(random(1), 0, 1, 0, TWO_PI);
    float velMag = map(random(1), 0, 1, 0, 2);
    velocity = new PVector(velMag * cos(velAng), velMag * sin(velAng));
  }
  
  void display(){
    ellipse(position.x, position.y, 4, 4);
  }
  
  void update(){
    float angFromCenter = atan2(position.y, position.x);
    PVector acceleration = new PVector(0.05 * cos(angFromCenter + PI), 0.05 * sin(angFromCenter + PI));
    if(random(1) < 0.1){
      float accAng = map(random(1), 0, 1, 0, TWO_PI);
      float accMag = map(random(1), 0, 1, 0, 5);
      PVector acc = new PVector(accMag * cos(accAng), accMag * sin(accAng));
      acceleration.add(acc);
    }
    velocity.add(acceleration);
    velocity.limit(2);
    position.add(velocity);
  }  
}