xxxxxxxxxx
final int STARTING_PARTICLES = 7;
final int PARTICLE_SIZE = 48;
ArrayList<Particle> particles = new ArrayList<Particle>(); // All particles in sketch
Particle[] explodingParticles = new Particle[STARTING_PARTICLES]; // Particles that will explode
Boolean explode = false;
int pos = 0;
void setup()
{
size(600, 600, P2D);
for (int x = 0; x < STARTING_PARTICLES; x++) {
Particle part = new Particle();
particles.add(part);
explodingParticles[x] = part;
}
noStroke();
}
void draw()
{
// Draw rectangle background (for motion blur)
fill(255, 20);
rect(0, 0, width, height);
fill(0);
// Draw particles
for (Particle part : particles) {
part.move();
part.display();
}
// Explode particles
if (explode & pos != STARTING_PARTICLES) {
explodingParticles[pos].explode();
pos++;
explode = false;
}
}
class Particle
{
final static float BOUNCE = -1;
final static float MAX_SPEED = 2.0;
int size = PARTICLE_SIZE;//4;
PVector speed = new PVector(random(-MAX_SPEED, MAX_SPEED), random(-MAX_SPEED, MAX_SPEED));
PVector pos;
ArrayList<Particle> neighbours;
ColorGenerator colour = new ColorGenerator();
Particle()
{
// Determine random start location of particle
pos = new PVector (random(width), random(height));
}
public void move()
{
pos.add(speed);
// Boundary check
if (pos.x < 0)
{
pos.x = 0;
speed.x *= BOUNCE;
}
else if (pos.x > width)
{
pos.x = width;
speed.x *= BOUNCE;
}
if (pos.y < 0)
{
pos.y = 0;
speed.y *= BOUNCE;
}
else if (pos.y > height)
{
pos.y = height;
speed.y *= BOUNCE;
}
}
public void explode()
{
for (int x = size; x >= 4; x = x - 4) {
Particle part = new Particle();
part.pos = new PVector(pos.x, pos.y); // Same position as original particle
part.speed = new PVector(random(-MAX_SPEED, MAX_SPEED), random(-MAX_SPEED, MAX_SPEED)); // Random particle vector
part.size = 4;
this.size -= 4;
particles.add(part);
}
}
public void display()
{
colour.update();
noFill();
fill(colour.R, colour.G, colour.B);
ellipse(pos.x, pos.y, size, size);
}
}
class ColorGenerator
{
final static float MIN_SPEED = 0.7;
final static float MAX_SPEED = 1.5;
float R, G, B;
float Rspeed, Gspeed, Bspeed;
ColorGenerator()
{
init();
}
public void init()
{
// Random starting colour
R = random(255);
G = random(255);
B = random(255);
// Random transition
Rspeed = (random(1) > 0.5 ? 1 : -1) * random(MIN_SPEED, MAX_SPEED);
Gspeed = (random(1) > 0.5 ? 1 : -1) * random(MIN_SPEED, MAX_SPEED);
Bspeed = (random(1) > 0.5 ? 1 : -1) * random(MIN_SPEED, MAX_SPEED);
}
public void update()
{
// Random transition (keep within RGB colour range)
Rspeed = ((R += Rspeed) > 255 || (R < 0)) ? -Rspeed : Rspeed;
Gspeed = ((G += Gspeed) > 255 || (G < 0)) ? -Gspeed : Gspeed;
Bspeed = ((B += Bspeed) > 255 || (B < 0)) ? -Bspeed : Bspeed;
}
}
void keyPressed()
{
if (key == ENTER) {
explode = true;
} //else if (key = SPACE
}