Hello all! This is a continuation of my project from here!
It is a bouncing ball that starts in random location with a random gravity acting on it. If you press with a mouse the velocity increases. There’s a chance the background could be black or white. It creates dots as it bounces along. All of that is working and intentional ^-^
However, what I wanted to do is to connect the dots with a line between the previous point and the current points. The code below is my failed attempt to do that (focus on the enhanced for loop near the end)
PVector location;
PVector velocity;
PVector gravity;
ArrayList<PVector> list = new ArrayList();
int col1;
int col2;
int col3;
int ran;
float locRanX;
float locRanY;
float previousX;
float previousY;
void setup() {
size(640,360);
locRanX = (float)(Math.random()*2);
locRanY = (float)(Math.random()*2);
location = new PVector(random(width),random(height));
velocity = new PVector(locRanX, locRanY);
gravity = new PVector(0,(float)(Math.random()*0.7)+0.2);
col1 = (int)(Math.random()*254)+1;
col2 = (int)(Math.random()*254)+1;
col3 = (int)(Math.random()*254)+1;
ran = (int)(Math.random()*2);
previousX = locRanX;
previousY = locRanY;
}
void draw() {
if (ran==1){
background(255);
}
else {
background(0);
}
list.add(new PVector(location.x,location.y));
location.add(velocity);
velocity.add(gravity);
if ((location.x < 0)) {
velocity.x = velocity.x * -1;
col1 = (int)(Math.random()*254)+1;
col2 = (int)(Math.random()*254)+1;
col3 = (int)(Math.random()*254)+1;
}
if((location.x> width)){
velocity.x = velocity.x * -1;
col1 = (int)(Math.random()*254)+1;
col2 = (int)(Math.random()*254)+1;
col3 = (int)(Math.random()*254)+1;
gravity = new PVector(0,(float)(Math.random()*0.6)+0.2);}
if (location.y > height) {
velocity.y = velocity.y * -0.88;
location.y = height;
}
if (location.y < 0) {
velocity.y = velocity.y * -0.6;
location.y = 0;
}
if (mousePressed){
velocity.y = velocity.y * 1.05;
}
if (ran!=1){
stroke(255);
fill(127);
}
else{
stroke (0);
fill(60);
}
strokeWeight(2);
ellipse(location.x,location.y, 48, 48);
for(PVector currentPV : list) {
stroke(col1, col2, col3);
point(currentPV.x,currentPV.y);
line (currentPV.x, currentPV.y, previousX, previousY);
previousX = currentPV.x;
previousY = currentPV.x;
}
}
I think I’m using the line function wrong. If you remove the line function, you can see the dots created. May someone please guide me to see my error(s)?