Particle generators
Here’s an example of a simple particle generator in Processing. The next step would be to make a ParticleGenerator class with this functionality, to make it easier to use in your program.
class Particle
{
PVector position;
PVector velocity;
PVector acceleration;
float r;
int c;
boolean alive;
Particle(PVector position, PVector velocity)
{
this.position = position.copy();
this.velocity = velocity.copy();
this.acceleration = new PVector(0, .3);
this.r = random(1, 5);
this.c = color(50, random(200, 256), random(200, 256));
this.alive = true;
}
void display()
{
fill(c);
ellipse(position.x, position.y, r*2, r*2);
position.add(velocity);
velocity.add(acceleration);
if (position.x < -r || position.x > width+r ||
position.y < -r || position.y > height+r)
alive = false;
}
}
ArrayList<Particle> particles;
void setup()
{
size(400, 400);
particles = new ArrayList<Particle>();
}
void createParticles()
{
for (int i=0; i<3; i++)
{
PVector position = new PVector(mouseX, mouseY);
PVector velocity = new PVector(random(-5, 5), random(-5, 5));
particles.add(new Particle(position, velocity));
}
}
void draw()
{
background(0);
for (Particle p : particles)
p.display();
createParticles();
fill(255);
textSize(20);
text(particles.size(), 50, 50);
// remove dead particles
for (int i=particles.size()-1; i>=0; i--)
if (particles.get(i).alive == false)
particles.remove(i);
}