xxxxxxxxxx
// square particles spawned in a circle around the window center
// particle system, sin, cos, arrayList, grayScale
// Mouse click to pause.
particleGenerator pg;
boolean pause;
void setup() {
size(750, 450);
frameRate(20);
background(50);
pg = new particleGenerator();
pause = false;
}
void draw() {
if (!pause)pg.run();
}
void mousePressed() {
pause = !pause;
}
// Shiffman particle system
class particleGenerator {
ArrayList<Particle> particles;
float x, y, ang;
particleGenerator() {
particles = new ArrayList<Particle>();
}
void run() {
ang += TWO_PI/37;
if (ang > TWO_PI) ang = ang % TWO_PI;
x = width/2 - cos(ang) * 150;
y = height/2 + sin(ang) * 150;
// limit particle generation frequency
if (frameCount % 20 == 0) {
particles.add(new Particle(x, y));
}
for (int i = particles.size ()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
}
class Particle {
float x, y, lifespan, sze;
Particle(float xin, float yin) {
x = xin;
y = yin;
lifespan = 255.0;
sze = 0;
rectMode(CENTER);
}
void run() {
sze += 2;
lifespan -= .85;
strokeWeight(5);
stroke(0, lifespan - 50);
noFill();
rect(x, y, sze, sze);
strokeWeight(1);
stroke(255, lifespan);
rect(x, y, sze, sze);
}
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}