Created
April 19, 2016 15:27
-
-
Save nick3499/0f13eb29591b4c8071c58e2814f8e73f to your computer and use it in GitHub Desktop.
Array List. 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(); | |
| 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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment