xxxxxxxxxx
53
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Simple Particle System
// A simple Particle class
var Particle = function(position, img) {
this.acceleration = createVector(0, 0.05);
this.velocity = createVector(random(-1, 1), random(-1, 0));
this.position = position.copy();
this.lifespan = 255.0;
this.image = img;
this.run = function() {
this.update();
this.display();
};
// Method to update position
this.update = function(){
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.lifespan -= 2;
};
// Method to apply a force vector to the Particle object
// Note we are ignoring "mass" here
this.applyForce = function(f) {
this.acceleration.add(f);
}
// Method to display
this.display = function() {
//imageMode(CENTER);
//tint(255,lifespan);
//image(img,loc.x,loc.y);
// Drawing a circle instead
fill(255,this.lifespan);
noStroke();
ellipse(this.position.x,this.position.y,this.image.width,this.image.height);
};
// Is the particle still useful?
this.isDead = function(){
if (this.lifespan < 0.0) {
return true;
} else {
return false;
}
};
};