nick34992
4/19/2016 - 3:27 PM

Array List. Using processing.js library.

Array List. Using processing.js library.

ArrayList population;

void setup() {
  size(1024, 576);
  smooth();
  frameRate(30);
  population = new ArrayList();
  for(int i=0; i<200; i++) {
    Ball myBall = new Ball(random(0,width),random(0,200));
    population.add(myBall);
  }
}

void draw() {
  background(0);
  for(int i=0; i<200; i++){
    Ball myBall = (Ball) population.get(i);
    myBall.ops();
  }
}
// http://codepen.io/Indiessance/pen/dGNpVB
class Ball { // global variable
  float x  =  0;
  float y  =  0;
  float xv = random(-2,2);
  float yv = random(-2,2);

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

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

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

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

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

  void velocity() { // velocity vector
    x += xv;
    y += yv;
  }
}