Ball3D[] balls;
static final int numBalls = 100;
float fl = 250;
float vpX = 0;
float vpY = 0;
float gravity = 0.2;
float floor = 200;
float bounce = -0.6;

void setup() {
  size(960, 540);
  frameRate(30);
  initialize();
}

void initialize() {
  smooth();
  noStroke();
  vpX = width/2;
  vpY = height/2;
  balls = new Ball3D[numBalls];
  for(int i=0; i floor) {
    ball.ypos = floor;
    ball.vy *= bounce;
  }
  
  if(ball.zpos > -fl) {
    float scale = fl / (fl + ball.zpos);
    ball.scaleX = ball.scaleY = scale;
    ball.x = vpX + ball.xpos * scale;
    ball.y = vpY + ball.ypos * scale;
    ball.visible = true;
  }else{
    ball.visible = false;
  }
}

void sortZ() {
  for(int i=0; ii; j--) {
      if(balls[j].zpos > balls[j-1].zpos) {
        Ball3D t = balls[j];
        balls[j] = balls[j-1];
        balls[j-1] = t;
      }
    }
  }
  for(int i=0; i

Processing.js Animation Examples. Based on Keith Peters's book. Amazon