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

Daily Creative Coding

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

パーティクルの視点

/**
* view from a particle
*
* @author aa_debdeb
* @date 2016/05/16
*/

Particle cP;
ArrayList<Particle> particles;
float radious = 5;

void setup(){
  size(500, 500, P3D);
  cP = new Particle();
  particles = new ArrayList<Particle>();
  for(int i = 0; i < 100; i++){
    particles.add(new Particle());
  }
  noStroke();
  rectMode(CENTER);
}

void draw(){
  background(250, 235, 215);
  lights();
  camera(cP.pos.x, cP.pos.y, 0, cP.pos.x + cP.vel.x, cP.pos.y + cP.vel.y, 0, 0, 0, -1); 
  fill(240, 250, 100);
  pushMatrix();
  translate(width / 2, 0, 0);
  rotateX(PI / 2);
  rect(0, 0, width, 60);
  popMatrix();
  pushMatrix();
  translate(width / 2, height, 0);
  rotateX(PI / 2);
  rect(0, 0, width, 60);
  popMatrix();  
  pushMatrix();
  translate(0, height / 2, 0);
  rotateY(PI / 2);
  rect(0, 0, 60, height);
  popMatrix();
  pushMatrix();
  translate(width, height / 2, 0);
  rotateY(PI / 2);
  rect(0, 0, 60, height);
  popMatrix();  
  
  fill(250, 110, 100);
  cP.update();
  for(Particle particle: particles){
    particle.display();
    particle.update();
  }
}

class Particle{
  PVector pos, vel;
  Particle(){
    pos = new PVector(random(width), random(height));
    float velAng = random(TWO_PI);
    vel = new PVector(3 * cos(velAng), 3 * sin(velAng));
  }
  
  void update(){
    pos.add(vel);
    if(pos.x < radious){
      pos.x = radious;
      vel.x *= -1;
    }
    if(pos.x >= width - radious){
      pos.x = width - radious;
      vel.x *= -1;
    }
    if(pos.y < radious){
      pos.y = radious;
      vel.y *= -1;
    }
    if(pos.y >= height - radious){
      pos.y = height - radious;
      vel.y *= -1;
    }
  }
  
  void display(){
    pushMatrix();
    translate(pos.x, pos.y, 0);
    sphere(radious * 2);
    popMatrix();
  }
}