Ball Class. Using processing.js library.
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;
}
}