Created
April 19, 2016 15:32
-
-
Save nick3499/1c3ff4f3dcc61fae2ff9f6f0b211ba0b to your computer and use it in GitHub Desktop.
Ball Class. 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
| Ball[] population = new Ball[400]; | |
| void setup() { | |
| size(1024, 576); | |
| smooth(); | |
| frameRate(30); | |
| for(int i=0; i<400; i++) { | |
| population[i] = new Ball(random(0, width), random(0, 100)); | |
| } | |
| } | |
| void draw() { | |
| background(0); | |
| for(int i=0; i<400; i++){ | |
| population[i].ops(); | |
| } | |
| } | |
| // Constructor | |
| class Ball { // global variable | |
| float x = 0; | |
| float y = 0; | |
| float xv = 16; | |
| float yv = 9; | |
| Ball(float _x, float _y) { | |
| x = _x; | |
| y = _y; | |
| } | |
| void ops() { | |
| display (); // display ellipse | |
| velocity(); // velocity vector | |
| invertV (); // invert velocity | |
| force (); // simulate gravity | |
| } | |
| void force() { | |
| yv += random(-0.6, 0.6); // separate objects | |
| } | |
| 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)); | |
| ellipse(x, y, 20, 20); | |
| } | |
| 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