changes to simulate examples

This commit is contained in:
shiffman
2011-12-15 17:27:25 +00:00
parent 2072bceb6e
commit 65f795f70d
12 changed files with 205 additions and 307 deletions

View File

@@ -9,32 +9,31 @@
* with Particles and CrazyParticles (a subclass of Particle)
* Note use of Inheritance and Polymorphism here.
*/
ArrayList psystems;
ArrayList<ParticleSystem> systems;
void setup() {
size(640, 360);
colorMode(RGB, 255, 255, 255, 100);
psystems = new ArrayList();
systems = new ArrayList<ParticleSystem>();
smooth();
}
void draw() {
background(0);
// Cycle through all particle systems, run them and delete old ones
for (int i = psystems.size()-1; i >= 0; i--) {
ParticleSystem psys = (ParticleSystem) psystems.get(i);
psys.run();
if (psys.dead()) {
psystems.remove(i);
}
for (ParticleSystem ps: systems) {
ps.run();
ps.addParticle();
}
if (systems.isEmpty()) {
fill(255);
textAlign(CENTER);
text("click mouse to add particle systems", width/2, height/2);
}
}
// When the mouse is pressed, add a new particle system
void mousePressed() {
psystems.add(new ParticleSystem(int(random(5,25)),new PVector(mouseX,mouseY)));
systems.add(new ParticleSystem(1, new PVector(mouseX, mouseY)));
}
@@ -46,4 +45,3 @@ void mousePressed() {

View File

@@ -1,57 +1,44 @@
// A simple Particle class
class Particle {
PVector loc;
PVector vel;
PVector acc;
float r;
float timer;
PVector location;
PVector velocity;
PVector acceleration;
float lifespan;
// One constructor
Particle(PVector a, PVector v, PVector l, float r_) {
acc = a.get();
vel = v.get();
loc = l.get();
r = r_;
timer = 100.0;
}
// Another constructor (the one we are using here)
Particle(PVector l) {
acc = new PVector(0,0.05,0);
vel = new PVector(random(-1,1),random(-2,0),0);
loc = l.get();
r = 10.0;
timer = 100.0;
acceleration = new PVector(0,0.05);
velocity = new PVector(random(-1,1),random(-2,0));
location = l.get();
lifespan = 255.0;
}
void run() {
update();
render();
display();
}
// Method to update location
void update() {
vel.add(acc);
loc.add(vel);
timer -= 1.0;
velocity.add(acceleration);
location.add(velocity);
lifespan -= 2.0;
}
// Method to display
void render() {
ellipseMode(CENTER);
stroke(255,timer);
fill(100,timer);
ellipse(loc.x,loc.y,r,r);
void display() {
stroke(255,lifespan);
fill(255,lifespan);
ellipse(location.x,location.y,8,8);
}
// Is the particle still useful?
boolean dead() {
if (timer <= 0.0) {
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}

View File

@@ -2,29 +2,25 @@
class ParticleSystem {
ArrayList particles; // An arraylist for all the particles
PVector origin; // An origin point for where particles are birthed
ArrayList<Particle> particles; // An arraylist for all the particles
PVector origin; // An origin point for where particles are birthed
ParticleSystem(int num, PVector v) {
particles = new ArrayList(); // Initialize the arraylist
particles = new ArrayList<Particle>(); // Initialize the arraylist
origin = v.get(); // Store the origin point
for (int i = 0; i < num; i++) {
// We have a 50% chance of adding each kind of particle
if (random(1) < 0.5) {
particles.add(new CrazyParticle(origin));
} else {
particles.add(new Particle(origin));
}
particles.add(new Particle(origin)); // Add "num" amount of particles to the arraylist
}
}
void run() {
// Cycle through the ArrayList backwards b/c we are deleting
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = (Particle) particles.get(i);
// Cycle through the ArrayList using Iterator we are deleting
Iterator<Particle> it = particles.iterator();
while (it.hasNext()) {
Particle p = it.next();
p.run();
if (p.dead()) {
particles.remove(i);
if (p.isDead()) {
it.remove();
}
}
}
@@ -41,11 +37,9 @@ class ParticleSystem {
boolean dead() {
if (particles.isEmpty()) {
return true;
}
else {
} else {
return false;
}
}
}