# STOP the explosion

**URL:** https://discourse.processing.org/t/stop-the-explosion/5071
**Category:** Coding Questions
**Created:** [October 31, 2018, 9:40pm UTC](https://discourse.processing.org/t/stop-the-explosion/5071 "2018-10-31T21:40:12Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kornFusion](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@kornFusion](https://discourse.processing.org/u/kornFusion)
#### Post date: [October 31, 2018, 9:40pm UTC](https://discourse.processing.org/t/stop-the-explosion/5071/1 "2018-10-31T21:40:12Z")

</div>

Hey Guys,

I have the following code which I want to stop the particles before they leave the screen…  
So I dont know how to stop them near the edges of the screen…

Please advice…

Thanks

```auto
Particle [] pickles = new Particle [100];

void setup () {

  size (500, 500);
  smooth ();
  for (int i=0; i<pickles.length; i++) {
    pickles [i] = new Particle ();
  }
}

void draw () {
  background (0); //clear the background

  for (int i=0; i<pickles.length; i++) {
    pickles[i].update();
  }
}

class Particle {

  float x;
  float y;

  float velX ; // speed or velocity
  float velY;

  Particle () {
    //x and y position to be in middle of screen
    x = width/2;
    y = height/2;

    velX = random (-10, 10);
    velY = random (-10, 10);

} 

  void update () {

    x+=velX;
    y+=velY;
        
    fill (255);
    ellipse (x, y, 10, 10);
  }
}

```

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [November 1, 2018, 12:56am UTC](https://discourse.processing.org/t/stop-the-explosion/5071/2 "2018-11-01T00:56:16Z")

</div>

See below.

```auto
Particle [] pickles = new Particle [100];

void setup () {

  size (500, 500);
  smooth ();
  for (int i=0; i<pickles.length; i++) {
    pickles [i] = new Particle ();
  }
}

void draw () {
  background (0); //clear the background

  for (int i=0; i<pickles.length; i++) {
    pickles[i].update();
  }
}

class Particle {

  float x;
  float y;

  float velX ; // speed or velocity
  float velY;

  Particle () {
    //x and y position to be in middle of screen
    x = width/2;
    y = height/2;

    velX = random (-10, 10);
    velY = random (-10, 10);

} 

  void update () {

    x+=velX;
    y+=velY;
    // Stop them from going off the left side.
    if( x < 5 ){
      x = 5;
      velY = 0;
    }
    // Stop them from going off the right side.
    if( x > width - 5 ){
      x = width - 5;
      velY = 0;
    }
    // I will leave these two up to you!
    // TODO: Stop them from going out the top.
    // TODO: Stop them from going out the bottom.
    
    
        
    fill (255);
    ellipse (x, y, 10, 10);
  }
}

```

---

<div class="post-metadata">

### Author: ![kornFusion](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@kornFusion](https://discourse.processing.org/u/kornFusion)
#### Post date: [November 1, 2018, 1:14am UTC](https://discourse.processing.org/t/stop-the-explosion/5071/3 "2018-11-01T01:14:36Z")

</div>

It worked!! 😃  
Is it possible to stop them randomly? like Matrix

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [November 1, 2018, 6:54am UTC](https://discourse.processing.org/t/stop-the-explosion/5071/4 "2018-11-01T06:54:37Z")

</div>

how about a natural ageing?

```auto
// https://discourse.processing.org/t/stop-the-explosion/5071/2
Particle [] pickles = new Particle [10];

void setup () {
  size (500, 500);
  smooth ();
  for (int i=0; i<pickles.length; i++) {
    pickles [i] = new Particle ();
  }
}

void draw () {
// background (0); //clear the background
  for (int i=0; i<pickles.length; i++) {
    pickles[i].update();
  }
}

class Particle {
  float x;
  float y;
  float velX ; // speed or velocity
  float velY;

  Particle () {
    //x and y position to be in middle of screen
    x = width/2;
    y = height/2;
    velX = random (-10, 10);
    velY = random (-10, 10);
} 

  void update () {
    float ageing = 0.998;
    velX = velX * ageing;
    velY = velY * ageing;
    x+=velX;
    y+=velY;
    // change from STOP to BOUNCE
    if( x < 5 ) velX = -1*velX;
    if( x > width - 5 ) velX = -1*velX;
    if( y < 5 ) velY = -1*velY;
    if( y > height - 5 ) velY = -1*velY;
     
    fill (255);
    ellipse (x, y, 10, 10);
  }
}

```

add some color:

```auto
// https://discourse.processing.org/t/stop-the-explosion/5071/2
// from @TfGuy44, mod KLL

/*
 isn't it a colorful life? 
 we are all different individuals ( fill color )
 and go a different path ( x , y )
 bouncing like stupid objects in our little world. ( vel_ = -1*vel_ )
 but with the time we get older, slower ( vel_ = vel_ * ageing )
 and our path gets darker, ( stroke(_,100, HSBcolstroke) )
 but we keep our individuality ( fill (HSBcol, 100, 100) )
 until we die alone. ( fill(100) )
 */

Particle [] pickles = new Particle [10];

void setup () {
  size (500, 500);
  smooth ();
  colorMode(HSB, 100, 100, 100);
  for (int i=0; i<pickles.length; i++) pickles [i] = new Particle ();
}

void draw () {
  // background (0); //clear the background
  for (int i=0; i<pickles.length; i++) pickles[i].update();
}

class Particle {
  float x;
  float y;
  float velX ; // speed or velocity
  float velY;
  float HSBcol;
  float HSBcolstroke;

  Particle () { //x and y position to be in middle of screen
    x = width/2;
    y = height/2;
    velX = random (-10, 10);
    velY = random (-10, 10);
    HSBcol = random (0, 100);
    HSBcolstroke = HSBcol;
  } 

  void update () {
    final float ageing = 0.998;
    velX = velX * ageing;
    velY = velY * ageing;
    HSBcolstroke = HSBcolstroke * ageing;
    if ( abs(velX) < 0.0001 ) HSBcol = 0; // the end
    x+=velX;
    y+=velY;
    // change from STOP to BOUNCE
    if (( x < 5 ) || ( x > width - 5 )) velX = -1*velX;
    if (( y < 5 ) || ( y > height - 5 )) velY = -1*velY;
    stroke(HSBcolstroke, 100, HSBcolstroke); // turn to black stroke with age
    if ( HSBcol > 0 ) { fill (HSBcol, 100, 100); } else { fill(100); } // and white fill if die 
    ellipse (x, y, 10, 10);
  }
}

```

 ![snap-063](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9981959eac82e1c0c4bd63fea70081f6a2c454e5.jpeg)

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [November 1, 2018, 5:36pm UTC](https://discourse.processing.org/t/stop-the-explosion/5071/5 "2018-11-01T17:36:43Z")

</div>

If you’re going to post your question to multiple sites, please link between crossposts so we don’t end up repeating help you’ve already received. This question has also been posted here:

> <https://stackoverflow.com/questions/53092426/processing-stop-the-animation>
