nick34992
4/19/2016 - 3:29 PM

Array List 2. Using processing.js library.

Array List 2. Using processing.js library.

ArrayList population;

void setup() {
  size(1024, 576);
  smooth();
  frameRate(30);
  population = new ArrayList();
}

void draw() {
  background(0);
  Ball myBall = new Ball(512, 288);
  population.add(myBall);
  for(int i=0; i<population.size(); i++) {
    Ball mb = (Ball) population.get(i);
    mb.ops();
  }
}

// Ball Constructor Function
class Ball {
  float x  =  0;
  float y  =  0;
  float xv = random(-2,2); // random velocity
  float yv = random(-2,2);
  float oldBall; // aged ball

  Ball(float _x, float _y) {
    x = _x;
    y = _y;
    oldBall = 255;
  }

  void ops() {
    display (); // display ellipse
    velocity(); // velocity vector
    invertV (); // invert velocity
    force   (); // random force
  }

  void force() {
    yv += random(-0.6, 0.6); // random force
  }

  void invertV() { // invert y velocity vector
    if(y>height || y<0) {
      yv = yv * -1;
    }
  }

  void display() { // display ellipse
    fill(random(0, 255), random(0, 255), random(0, 255), oldBall);
    ellipse(x, y, 40, 40);
  }

  void velocity() { // velocity vector
    x += xv;
    y += yv;
    oldBall -= 1;
  }
  
  boolean old(){ // remove aged balls
    if (oldBall < 0) {
      return true;
    } else {
      return false;
    } 
  }
}