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

**URL:** <https://discourse.processing.org/t/unable-to-figure-out-how-to-make-these-objects-go-in-the-opposite-direction/7155>\
**Category:** Coding Questions\
**Created:** [January 4, 2019, 10:30am UTC](https://discourse.processing.org/t/unable-to-figure-out-how-to-make-these-objects-go-in-the-opposite-direction/7155 "2019-01-04T10:30:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![francium401](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@francium401](https://discourse.processing.org/u/francium401)\
**Post date:** [January 4, 2019, 10:30am UTC](https://discourse.processing.org/t/unable-to-figure-out-how-to-make-these-objects-go-in-the-opposite-direction/7155/1 "2019-01-04T10:30:20Z")

</div>

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.

```auto
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();
  }
}

```

```auto
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);
  }
  
}

```

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 4, 2019, 10:47am UTC](https://discourse.processing.org/t/unable-to-figure-out-how-to-make-these-objects-go-in-the-opposite-direction/7155/2 "2019-01-04T10:47:32Z")

</div>

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.

```auto
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!

---

<div class="post-metadata">

**Author:** ![francium401](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@francium401](https://discourse.processing.org/u/francium401)\
**Post date:** [January 4, 2019, 10:29pm UTC](https://discourse.processing.org/t/unable-to-figure-out-how-to-make-these-objects-go-in-the-opposite-direction/7155/3 "2019-01-04T22:29:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 5, 2019, 6:44am UTC](https://discourse.processing.org/t/unable-to-figure-out-how-to-make-these-objects-go-in-the-opposite-direction/7155/4 "2019-01-05T06:44:04Z")

</div>

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:

[![](https://img.youtube.com/vi/IsdZKG9wyBc/hqdefault.jpg "4.3: Deleting objects from ArrayList - The Nature of Code") ](https://www.youtube.com/watch?v=IsdZKG9wyBc)
