Unable to figure out how to make these objects go in the opposite direction

I want to make these circles go in the opposite direction (right to left, instead of left to right), but I can’t seem to figure out how.
I managed to get them moving right to left by changing the speed value to a negative number. However, I’m more stuck on how to change the circle’s spawn location to the right, and the de-spawn location to the left.
Circles are keep flickering in and out on the left-hand side, which is something I don’t want.

flake[] flake = new flake[30];

void setup(){
 size(800,800);
 for(int i = 0; i < flake.length; i++){
  flake[i] = new flake(); 
 }
}

void draw(){
  background(50);
  for(int i = 0; i < flake.length; i++){
   flake[i].show();
   flake[i].update();
  }
}

class flake{
  float y=random(height);
  float x=random(-width);
  
  float z = random(20, 100);
  float size=map(z, 1, 30, 3, 20);
  float speed=map(z, 1, 30, 0.1, 1);
  
  void show(){
    stroke(255);
    strokeWeight(size);
    point(x,y);
  }
  
  void update() {
    x+=speed;
    
    if(x>width) reset();
  }
  
  void reset(){
   y = random(height);
   x = random(-width,0);
   z = random(20,100);
   size=map(z,1,29,3,30);
   speed=map(z,1,29,0.1,1);
  }
  
}

1 Like

Here, below is your code modified so that it spawns particles both going to the left and to the right, based on random z from -100 to 100, and with some additional if statements to use one logic or the other based on if the speed is negative or not.
The a ? b : c thing is basically if statement that, if a is true, chooses b, and if it’s not chooses c instead, allowing for handy logic things.

class flake{
  
  float z = random(1)>0.5 ? random(-100,-20) : random(20, 100); //random(1)>0.5 gives true 50% of the times - so our value is always either smaller than -20 or bigger than 20 to prevent really small particles
  
  float y=random(height);
  float x=z<0 ? width+random(width) : random(-width);
  float size=abs(map(z, 1, 30, 3, 20)); //abs function takes any number and makes it always positive i.e. removes minus
  float speed=map(z, 1, 30, 0.1, 1);
  
  void show(){
    stroke(255);
    strokeWeight(size);
    point(x,y);
  }
  
  void update() {
    x+=speed;
    
    if(speed<0){
      if(x<-size/2) reset();
    } else {
      if(x>width+size/2) reset();
    }

  }
  
  void reset(){
   z = random(1)>0.5 ? random(-100,-20) : random(20, 100);
   y = random(height);
   x=z<0 ? width+random(width) : random(-width);
   size=abs(map(z,1,29,3,30));
   speed=map(z,1,29,0.1,1);
  }
  
}

Hopefully some of the things I used here aren’t too hard or confusing!

2 Likes

Very helpful, thank you.
How would I go about removing the circles going from the left to right?

Hmm, I don’t know in that regard. For that, I’d suggest having ArrayList<flake> instead of usual array - then, give flakes another variable boolean toRemove; - and in void update() of the particle, replace reset(); with toRemove=true;.
Then, in void draw(), have a for loop going through that ArrayList<flake>, checking toRemove() of each, and removing the thing. Make sure to account for the fact that for loop can glitch out a little by skipping forward if you remove stuff from ArrayList you iterate on inside that same for loop…

For more explanations, here’s a good video on topic:

2 Likes