I'm trying to create a simple animation where multiple balls are sequentially thrown in a parabolic arc.
The code I wrote consists of class BallMotion where I assign values to the 2D array 'float [][] f' through two nested for loops (as seen below). The first for loop keeps track of the number of balls, and the second for loop assigns values for the y, so the 2D array contains 'float [numberofballs][yvalue]'. Thus, the y values are generated and assigned for each ball.
//method set value
public void values() {
for (int i=0;i<numBalls;i++) { //index number
int t=(int)random(1, 5); // variables for parabolic equation
int r=(int)random(1, 500); //variable for parabolic equation
for (int j=0;j<width;j++) { //assign xy value for each [i] number
In 'public void draw' I set up two for loops again, in order to draw the ellipses, one after another - as seen below. I put the command background(255) (line 9) within the second for loop - so the screen is redrawn after every ball is drawn - for animation purposes.
public void draw() {
//derive the y values for all balls
bm.values();
//first loop for each ball
for (int i=0;i<numBalls;i++) {
//second loop for y value of each ball
for (int j=0;j<width;j++) {
//background(255);
ellipse(j, bm.f[i][j], 15, 15); // bm.f[i][j] = y value
}
}
}
Okay, so the problem is that this animation effect is not occurring. For some reason placing the background(255) within the second for loop causes the program to not work. On the other hand, if there's no background - you can see that the program runs as designed - just without the animation effect. I've been playing around with this for a couple of hours and haven't been able to crack this.
If anyone can help, it'd be super appreciated.
Full code below:
BallMotion bm;
int numBalls=5; // number of balls created
public void setup() {
size(500, 500);
bm=new BallMotion();
}
public void draw() {
//derive the y values for all balls
bm.values();
//first loop for each ball
for (int i=0;i<numBalls;i++) {
//second loop for y value of each ball
for (int j=0;j<width;j++) {
//background(255);
ellipse(j, bm.f[i][j], 15, 15); // bm.f[i][j] = y value
}
}
}
class BallMotion {
float [][] f; // array for y value
//ctor
public BallMotion() {
f=new float [numBalls][width];
}
//method set value
public void values() {
for (int i=0;i<numBalls;i++) { //index number
int t=(int)random(1, 5); // variables for parabolic equation
int r=(int)random(1, 500); //variable for parabolic equation
for (int j=0;j<width;j++) { //assign xy value for each [i] number