-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparticle.js
More file actions
51 lines (42 loc) · 954 Bytes
/
particle.js
File metadata and controls
51 lines (42 loc) · 954 Bytes
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
51
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Simple Particle System
// A simple Particle class
class Particle {
constructor(x, y) {
this.position = createVector(x, y);
this.velocity = createVector(random(-1, 1), random(-1, 0));
this.acceleration = createVector(0, 0);
this.lifespan = 55.0;
}
run() {
this.update();
this.display();
}
applyForce(f) {
this.acceleration.add(f);
}
// Method to update position
update() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.lifespan -= 2;
this.velocity.limit(5);
}
// Method to display
display() {
stroke(255, this.lifespan);
strokeWeight(2);
fill(255, this.lifespan);
ellipse(this.position.x, this.position.y, 12, 12);
}
// Is the particle still useful?
isDead() {
if (this.lifespan < 0.0) {
return true;
} else {
return false;
}
}
}