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

Daily Creative Coding

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

2つの輪を泳ぐパーティクル

パーティクル
/**
*  Particles in Two Rings
*
* @author aa_debdeb
* @date 2016/02/12
*/

ArrayList<Particle> particles;
PVector center1, center2;
float radious;

void setup(){
  size(600, 400);
  frameRate(30);
  noStroke();
  background(0);
  
  center1 = new PVector(200, height / 2);
  center2 = new PVector(400, height / 2);
  radious = 100;
  
  particles = new ArrayList<Particle>();
  for(int i = 0; i < 100; i++){
    particles.add(new Particle());
  }
}

void draw(){
  fill(0, 50);
  rect(0, 0, width, height);
  fill(255, 100);
  for(Particle p: particles){
    p.display();
    p.update();
  }
}

class Particle{
  float angle;
  float angleSpeedNoise;
  float radiousNoise;
  
  Particle(){
    angle = random(TWO_PI * 2);
    angleSpeedNoise = random(100);
    radiousNoise = random(100);
  }
  
  void display(){
    if(angle < TWO_PI){
      float r = radious + map(noise(radiousNoise + frameCount * 0.01), 0, 1, -50, 50);
      ellipse(center1.x + r * cos(angle), center1.y + r * sin(angle), 5, 5);
    } else {
      float r = radious + map(noise(radiousNoise + frameCount * 0.01), 0, 1, 50, -50);
      float a = PI - (angle - TWO_PI);
      ellipse(center2.x + r * cos(a),  center2.y + r * sin(a), 5, 5);
    }
  }
  
  void update(){
    angle += map(noise(angleSpeedNoise + frameCount * 0.01), 0, 1, PI / 128, PI / 32);
    if(angle >= 2 * TWO_PI){
      angle -= 2 * TWO_PI;
    }
  }
}