Now we want 100 of them.
We could start wriitng this out…
WaveBall wb0;
WaveBall wb1;
WaveBall wb2;
WaveBall wb3;
WaveBall wb4;
But this is STUPID! There must be a better way, right? Well there is! You can put these objects in an ARRAY.
class WaveBall {
float rad;
float xOrigin, yOrigin;
float yPos;
boolean toLeft;
WaveBall() {
rad = 50;
yOrigin = height/2;
toLeft = false;
xOrigin = random(width);
}
void draw() {
noStroke();
yPos =cos(frameCount * 0.15) * rad;
ellipse (xOrigin, yOrigin + yPos, 20, 20);
if (toLeft==false) {
xOrigin = xOrigin + 5;
if (xOrigin > 500) {
toLeft = true;
}
} else {
xOrigin = xOrigin - 5;
if (xOrigin < 0) {
toLeft = false;
}
}
}
}
WaveBall[] wbs;
void setup() {
size(500, 500);
wbs = new WaveBall[25];
for( int i = 0; i < wbs.length; i++) wbs[i] = new WaveBall();
}
void draw() {
background(0);
for( int i = 0; i < wbs.length; i++) wbs[i].draw();
}
Great! Now there are many of them. but they all oscillate the same. We should add an oscillation offset next:
class WaveBall {
float rad;
float xOrigin, yOrigin;
float yPos;
boolean toLeft;
float osc_offset = random(TWO_PI);
WaveBall() {
rad = 50;
yOrigin = height/2;
toLeft = false;
xOrigin = random(width);
}
void draw() {
noStroke();
yPos =cos(frameCount * 0.15 + osc_offset) * rad;
ellipse (xOrigin, yOrigin + yPos, 20, 20);
if (toLeft==false) {
xOrigin = xOrigin + 5;
if (xOrigin > 500) {
toLeft = true;
}
} else {
xOrigin = xOrigin - 5;
if (xOrigin < 0) {
toLeft = false;
}
}
}
}
WaveBall[] wbs;
void setup() {
size(500, 500);
wbs = new WaveBall[25];
for( int i = 0; i < wbs.length; i++) wbs[i] = new WaveBall();
}
void draw() {
background(0);
for( int i = 0; i < wbs.length; i++) wbs[i].draw();
}
Notice that with the addition of TWO LINES OF CODE, I have added this oscilation offset for EVERY WaveBall object! The power!
- You want to goof about with colors? Okay. First, let’s give each WaveBall it’s own color:
class WaveBall {
float rad;
float xOrigin, yOrigin;
float yPos;
boolean toLeft;
float osc_offset = random(TWO_PI);
color c;
WaveBall() {
rad = 50;
yOrigin = height/2;
toLeft = false;
xOrigin = random(width);
c = color( random(255), random(255), random(255));
}
void draw() {
noStroke();
yPos =cos(frameCount * 0.15 + osc_offset) * rad;
fill(c);
ellipse (xOrigin, yOrigin + yPos, 20, 20);
if (toLeft==false) {
xOrigin = xOrigin + 5;
if (xOrigin > 500) {
toLeft = true;
}
} else {
xOrigin = xOrigin - 5;
if (xOrigin < 0) {
toLeft = false;
}
}
}
}
WaveBall[] wbs;
void setup() {
size(500, 500);
wbs = new WaveBall[25];
for ( int i = 0; i < wbs.length; i++) wbs[i] = new WaveBall();
}
void draw() {
background(0);
for ( int i = 0; i < wbs.length; i++) wbs[i].draw();
}
That works.
Animation starting on mousepressed? Add a boolean that tracks if the mouse has been pressed yet, and use it to not draw the balls:
class WaveBall {
float rad;
float xOrigin, yOrigin;
float yPos;
boolean toLeft;
float osc_offset = random(TWO_PI);
color c;
WaveBall() {
rad = 50;
yOrigin = height/2;
toLeft = false;
xOrigin = random(width);
c = color( random(255), random(255), random(255));
}
void draw() {
if( !was_pressed ) return;
noStroke();
yPos =cos(frameCount * 0.15 + osc_offset) * rad;
fill(c);
ellipse (xOrigin, yOrigin + yPos, 20, 20);
if (toLeft==false) {
xOrigin = xOrigin + 5;
if (xOrigin > 500) {
toLeft = true;
}
} else {
xOrigin = xOrigin - 5;
if (xOrigin < 0) {
toLeft = false;
}
}
}
}
WaveBall[] wbs;
boolean was_pressed;
void setup() {
size(500, 500);
wbs = new WaveBall[25];
for ( int i = 0; i < wbs.length; i++) wbs[i] = new WaveBall();
was_pressed = false;
}
void draw() {
background(0);
for ( int i = 0; i < wbs.length; i++) wbs[i].draw();
}
void mousePressed(){
was_pressed = true;
}