Daily Creative Coding

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

スペースデブリ

/**
* space debris
*
* @author aa_debdeb
* @date 2016/12/01
*/

ArrayList<Particle> particles;

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

void draw(){
  background(0);
  noStroke();
  fill(230);
  translate(width / 2, height / 2);
  lights();
  rotateX(map(mouseY, 0, height, -PI / 3, PI / 3));
  rotateZ(map(mouseX, 0, height, -PI / 16, PI / 16));
  for(Particle p: particles){
    p.display();
    p.update();
  }
}

class Particle {
 
  float xRadius, zRadius;
  float angle;
  float angleSpeed;
  float tiltAngle;
  float rotXSpeed, rotYSpeed;
  float size;
  
  Particle(){
    xRadius = 200 + randomGaussian() * 40;
    zRadius = 100 + randomGaussian() * 20;
    angle = random(TWO_PI);
    angleSpeed = random(PI / 500, PI / 250);
    tiltAngle = randomGaussian() * PI / 32;
    rotXSpeed = random(-PI / 32, PI / 32);
    rotYSpeed = random(-PI / 32, PI / 32);
    size = map(pow(random(1), 3), 0, 1, 1, 5);
  }
  
  void display(){
    pushMatrix();
    rotateZ(tiltAngle);
    translate(xRadius * cos(angle), 0, zRadius * sin(angle));
    rotateX(rotXSpeed * frameCount);
    rotateY(rotYSpeed * frameCount);
    box(size);
    popMatrix();
  }
  
  void update(){
    angle -= angleSpeed;
    if(angle < 0){
      angle += TWO_PI;
    }
  }
}
f:id:aa_debdeb:20161123184359j:plain