The Eme­r­g­e­n­c­y War­n­i­n­g System is Bro­k­e­n­!

by Samuel Withers
  • Show Code
/** @peep sketch */
 
int NUM_PARTICLES = 100;
ParticleSystem particle_system;
 
void setup() {
  size(400, 400);
  PVector centre = new PVector(width/2, height/2, 0);
  particle_system = new ParticleSystem(centre, NUM_PARTICLES);
  strokeWeight(0.5);
  fill(255);
}
 
 
void update() {
  particle_system.update();
}
 
void draw() {
  update();
  loadPixels();
  for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
      // A random brightness!
      float bright = random(255);
      pixels[x+y*width] = color(bright);
    }
  }
  updatePixels();
 
  imageMode(CENTER);
  for (int i = 0; i < particle_system.particles.length; i++) {
    PVector l = particle_system.particles[i].location;
    ellipse(l.x, l.y, 20, 20);
  }
}
 
class Particle {
  PVector location;
  PVector velocity;
  PVector acceleration;
  float time_to_live;
 
  Particle(PVector _location) {
    location = _location.get(); // make a copy of given location using get()
    velocity = new PVector(random(-2.5, 2.5), random(-5, 0));
    acceleration = new PVector(0, 0.1);
    time_to_live = random(100, 200);
  }
 
  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    time_to_live -= 1;
  }
 
  boolean dead() {
    return (time_to_live <= 0);
  }
}
 
class ParticleSystem {
  PVector location;
  Particle[] particles;
 
  ParticleSystem(PVector _location, int _num_particles) {
    location = _location.get();
    particles = new Particle[_num_particles];
    for (int i = 0; i < particles.length; i++) {
      particles[i] = new Particle(location);
    }
  }
 
  void update() {
    for (int i = 0; i < particles.length; i++) {
      particles[i].update();
      if (particles[i].dead()) {
        particles[i] = new Particle(location);
      }
    }
  }
}

Comments

Nobody has said anything yet.