Sakura dans Particles

The particle system code examples all had an ethereal quality to them. I wanted to do something with the code that would enhance that. I used a photograph I took of the cherry blossoms in the Brooklyn Botanical Gardens for the background, and used a petal from one of the flowers as the image for the particle class. Unfortunately, using all of these png files makes the sketch render kind of slowly.

This is the code for the main sketch:

[code lang="java"]//Kim Ash
//Sakura - petals fall from cherry blossoms using particle mechanism
 
PImage cherrytree;
ParticleSystem high;
ParticleSystem middle;
ParticleSystem low;
 
void setup()
{
  size(800, 533);
  cherrytree = loadImage("cherrytree2.jpg");
  background(cherrytree);
  high = new ParticleSystem(new PVector(700,40));
  middle = new ParticleSystem(new PVector(300,80));
  low = new ParticleSystem(new PVector(width/2 + 100, 150));
   
}
 
void draw()
{
  background(cherrytree);
   
  PVector gravity = new PVector(-0.025,0.05);
   
  high.run();
  high.addParticle();
  high.applyForce(gravity);
   
  middle.run();
  middle.addParticle();
  middle.applyForce(gravity);
   
  low.run();
  low.addParticle();
  low.applyForce(gravity);
}
 
void mousePressed()
{
  int x = 1;
  PVector wind = new PVector(x * 2, 0);
   
  if (mouseX < width/2)
 {
    x = x;
    high.applyForce(wind);
    middle.applyForce(wind);
    low.applyForce(wind);
 }
 
  else
 {
    x = -x;
    high.applyForce(wind);
    middle.applyForce(wind);
    low.applyForce(wind);
 }
}[/code]

Particle class code:

[code lang="java"]

PImage petal;
 
class Particle {
  PVector location;
  PVector velocity;
  PVector acceleration;
  float lifespan;
   
  float mass = 1; // Let's do something better here!
 
  Particle(PVector l) {
    acceleration = new PVector(0,2);
    velocity = new PVector(random(-2, 2),random(-2,0));
    location = l.get();
    lifespan = 255.0;
  }
 
  void run() {
    update();
    display();
  }
 
  void applyForce(PVector force) {
    PVector f = force.get();
    f.div(mass);  
    acceleration.add(f);
  }
 
  // Method to update location
  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    acceleration.mult(0);
    lifespan -= 1.0;
  }
 
  //particles made up of petal image - slows rendering a little
  void display() {
    petal = loadImage("petal.png");
    tint(255, lifespan);
    image(petal, location.x, location.y, 10, 11);
  }
 
  // Is the particle still useful?
  boolean isDead() {
    if (lifespan < 0.0) {
      return true;
    } else {
      return false;
    }
  }
}[/code]

Particle system code:

[code lang="java"]class ParticleSystem {
  ArrayList particles;
  PVector origin;
 
  ParticleSystem(PVector location) {
    origin = location.get();
    particles = new ArrayList();
  }
 
  void addParticle() {
    particles.add(new Particle(origin));
  }
 
  // A function to apply a force to all Particles
  void applyForce(PVector f) {
    for (Particle p: particles) {
      p.applyForce(f);
    }
  }
 
  void run() {
    Iterator it = particles.iterator();
    while (it.hasNext()) {
      Particle p = it.next();
      p.run();
      if (p.isDead()) {
        it.remove();
      }
    }
  }
}[/code]