-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparticle_system.js
More file actions
44 lines (37 loc) · 979 Bytes
/
particle_system.js
File metadata and controls
44 lines (37 loc) · 979 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
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
class ParticleSystem {
constructor(position) {
this.origin = position.copy();
this.particles = [];
}
addParticle(x, y) {
if (x !== undefined && y !== undefined) {
this.particles.push(new Particle(x, y));
} else {
this.particles.push(new Particle(this.origin.x, this.origin.y));
}
}
run() {
// Run every particle
// ES6 for..of loop
for (let particle of this.particles) {
particle.run();
}
// Filter removes any elements of the array that do not pass the test
this.particles = this.particles.filter(particle => !particle.isDead());
}
// A function to apply a force to all Particles
applyForce(f) {
for (let particle of this.particles) {
particle.applyForce(f);
}
}
applyRepeller(r) {
for (let particle of this.particles) {
let force = r.repel(particle);
particle.applyForce(force);
}
}
}