Created
April 19, 2016 15:29
-
-
Save nick3499/61cc53df3c93981e691264abed519031 to your computer and use it in GitHub Desktop.
Array List 2. Using processing.js library.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment