Daily Creative Coding

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

三次元のボールが向かってくる

/**
* Coming Ball
*
* @author aa_debdeb
* @date 2015/11/29
*/

ArrayList<Ball> balls;

void setup(){
  size(500, 500, P3D);
  smooth();
  frameRate(30);
  sphereDetail(16);
  background(0);
  
  balls = new ArrayList<Ball>();
}

void draw(){
  noStroke();
  fill(0, 30);
  rect(-1, -1, width + 1, height + 1);
  lights();
  if(random(1) < 0.2){
    balls.add(new Ball());
  }
  
  ArrayList<Ball> disapearedBalls = new ArrayList<Ball>();
  for(Ball ball : balls){
    ball.display();
    if(ball.isDisapeared()){
      disapearedBalls.add(ball);
    }
  }
  for(Ball ball : disapearedBalls){
    balls.remove(ball);
  }
}

class Ball{
  PVector position;
  float speed;
  float acceleration;
  
  Ball(){
    position = new PVector(random(100) + 200, random(100) + 200, -1000);
    speed = 10;
  }
  
  void display(){
    position.z += speed;
    pushMatrix();
    translate(position.x, position.y, position.z);
    noStroke();
    fill(255, 0, 0);
    sphere(10);
    popMatrix();
  }
  
  boolean isDisapeared(){
    return position.z > (height/2.0) / tan(PI*30.0 / 180.0) + 100;
  }
}