# STOP the explosion

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…

Thanks

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

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

See below.

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

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

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

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

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

``````// 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 ;

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

``````