spilled particles
@author
float MAX_VEL = 15;
float MAX_SIZE = 10.0;
float GRAVITY = 1.0;
ArrayList<Particle> particles;
void setup(){
size(640, 640);
particles = new ArrayList<Particle>();
}
void draw(){
fill(0, 100);
rect(0, 0, width, height);
noStroke();
fill(255);
ArrayList<Particle> nextParticles = new ArrayList<Particle>();
for(Particle p: particles){
p.update();
p.display();
if(p.size > 0.1){
nextParticles.add(p);
}
}
particles = nextParticles;
if(mousePressed){
for(int i = 0; i < random(50); i++){
particles.add(new Particle());
}
} else {
for(int i = 0; i < random(3); i++){
particles.add(new Particle());
}
}
}
class Particle {
PVector loc, vel;
float size;
float decay;
Particle(){
loc = new PVector(mouseX, mouseY);
float velSize = 0.0;
for(int i = 0; i < 5; i++){
velSize += random(MAX_VEL);
}
velSize /= 5.0;
float velAng = random(TWO_PI);
vel = new PVector(velSize * cos(velAng), velSize * sin(velAng));
size = 0.0;
for(int i = 0; i < 5; i++){
size += random(MAX_SIZE);
}
size /= 5.0;
decay = random(0.9, 0.97);
}
void display(){
ellipse(loc.x, loc.y, size, size);
}
void update(){
vel.y += GRAVITY;
loc.add(vel);
if(loc.y >= height - size / 2){
vel.y *= -0.5;
loc.y += vel.y;
}
size *= decay;
}
}