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

Daily Creative Coding

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

パーティクルの流れ

/**
* Noisy Flow for Particles
*
* @author aa_debdeb
* @date 2016/03/04
*/

ArrayList<Particle> particles;

void setup(){
  size(640, 640);
  particles = new ArrayList<Particle>();
  for(int i = 0; i < 1000; i++){
    particles.add(new Particle());
  }
  
  background(255);
  stroke(0, 20);
  strokeWeight(1);
}

void draw(){
  for(Particle particle: particles){
    float radian = map(noise(mouseX + particle.pos.x * 0.02, mouseY + particle.pos.y * 0.02), 0, 1, 0, TWO_PI * 5) + random(PI / 6.0);
    particle.update(radian);
    particle.display();  
  }
}

void keyPressed(){
  background(255);
}

class Particle{
  
  PVector pos, pPos;
  float step;
  
  Particle(){
    pos = new PVector(random(width), random(height));
    step = map(random(1), 0, 1, 1, 1.5);
  }
  
  void update(float radian){
    pPos = pos;
    pos = PVector.add(pos, new PVector(step * cos(radian), step * sin(radian)));  
    if(pos.x < 0 || pos.x >= width || pos.y < 0 || pos.y >= height){
      pos = new PVector(random(width), random(height));
      pPos = new PVector(pos.x, pos.y);
    }
  }
  
  void display(){
    line(pPos.x, pPos.y, pos.x, pos.y);
  }
  
  
  
}