xxxxxxxxxx
ArrayList<Particle> particles = new ArrayList<Particle>();
int numBolts = 1;
int timestep = 30;
void setup() {
size(1112, 834);
background(0);
for (int i = 0; i < numBolts; i++) {
particles.add(new Particle((i+1)*width/(numBolts+1), 0, random(-3,3), 5, true));
}
}
void draw() {
//noLoop();
//background(0);
//while (particles.size() > 0) {
for (int t = 0; t < timestep; t++) {
for (int i = particles.size() - 1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
p.split();
p.runChildren();
if (!p.isAlive()) {
particles.remove(p);
}
}
}
//}
}
class Particle {
PVector p;
PVector ps;
PVector v;
boolean alive;
boolean isParent;
int opacity;
ArrayList<Particle> children = new ArrayList<Particle>();
Particle(x, y, vx, vy, isParent) {
p = new PVector(x, y);
ps = new PVector(p.x, p.y);
v = new PVector(vx, vy);
alive = true;
this.isParent = isParent;
opacity = 255;
}
Particle(x, y, vx, vy, isParent, opacity) {
p = new PVector(x, y);
ps = new PVector(p.x, p.y);
v = new PVector(vx, vy);
alive = true;
this.isParent = isParent;
this.opacity = opacity;
}
void run() {
stroke(255,opacity);
if (isParent) {
strokeWeight(3);
} else {
strokeWeight(1);
opacity -= 3;
}
ps.set(p.x,p.y);
p.add(v);
p.add(random(-5,5),random(-1,1));
line(p.x,p.y,ps.x,ps.y);
strokeWeight(10 + (isParent*2));
stroke(255,floor(opacity/10));
line(p.x,p.y,ps.x,ps.y);
}
void runChildren() {
for (int i = children.size() - 1; i >= 0; i--) {
Particle p = children.get(i);
p.run();
p.split();
p.runChildren();
}
}
void split() {
if (isParent) {
if (random() < 0.04) {
children.add(new Particle(p.x, p.y, random(-3,3), random(1,3), false, opacity));
}
} else {
if (random() < 0.02) {
children.add(new Particle(p.x, p.y, random(-3,3), random(1,3), false, opacity));
}
}
}
boolean isAlive() {
if (p.y > height) {
alive = false;
}
if (!isParent && opacity <= 0) {
alive = false;
}
return alive;
}
}
void mouseClicked() {
particles.add(new Particle(random(width), 0, random(-3,3), 5, true));
}
void keyPressed(){save('pix.jpg');}