forked from nature-of-code/noc-examples-p5.js-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticle.js
50 lines (43 loc) · 934 Bytes
/
particle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Simple Particle System
class Particle {
// Another constructor (the one we are using here)
constructor(x, y, img) {
// Boring example with constant acceleration
this.acc = createVector(0, 0);
this.vel = p5.Vector.random2D();
this.pos = createVector(x, y);
this.lifespan = 255;
this.img = img;
}
run() {
this.update();
this.render();
}
applyForce(f) {
this.acc.add(f);
}
// Method to update position
update() {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
this.lifespan -= 5.0;
}
// Method to display
render() {
imageMode(CENTER);
tint(this.lifespan);
image(this.img, this.pos.x, this.pos.y, 32, 32);
}
// Is the particle still useful?
isDead() {
if (this.lifespan <= 0.0) {
return true;
} else {
return false;
}
}
}