mirror of
https://github.com/processing/processing4.git
synced 2026-01-29 11:21:06 +01:00
41 lines
711 B
Plaintext
41 lines
711 B
Plaintext
// A simple Particle class
|
|
|
|
class Particle {
|
|
PVector location;
|
|
PVector velocity;
|
|
PVector acceleration;
|
|
float lifespan;
|
|
|
|
Particle(PVector l) {
|
|
acceleration = new PVector(0,0.05);
|
|
velocity = new PVector(random(-1,1),random(-2,0));
|
|
location = l.get();
|
|
lifespan = 255.0;
|
|
}
|
|
|
|
void run() {
|
|
update();
|
|
display();
|
|
}
|
|
|
|
// Method to update location
|
|
void update() {
|
|
velocity.add(acceleration);
|
|
location.add(velocity);
|
|
lifespan -= 2.0;
|
|
}
|
|
|
|
// Method to display
|
|
void display() {
|
|
stroke(255,lifespan);
|
|
fill(255,lifespan);
|
|
ellipse(location.x,location.y,8,8);
|
|
}
|
|
|
|
// Is the particle still useful?
|
|
boolean isDead() {
|
|
return (lifespan < 0.0);
|
|
}
|
|
|
|
}
|