-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParticleSystem.pde
76 lines (62 loc) · 1.96 KB
/
ParticleSystem.pde
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles
class ParticleSystem {
ArrayList particles; // An arraylist for all the particles
PVector origin; // An origin point for where particles are birthed
PVector direction; // A default direction for the particles.
PVector defaultDirection;
PImage img;
ParticleSystem(int num, PVector v, PImage img_) {
defaultDirection = new PVector(-0.316, -0.124);
direction = defaultDirection;
particles = new ArrayList(); // Initialize the arraylist
origin = v.get(); // Store the origin point
img = img_;
for (int i = 0; i < num; i++) {
particles.add(new Particle(origin, img)); // Add "num" amount of particles to the arraylist
}
}
void run() {
// Cycle through the ArrayList backwards b/c we are deleting
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = (Particle) particles.get(i);
p.run();
if (p.dead()) {
particles.remove(i);
}
}
}
// Method to add a force vector to all particles currently in the system
void add_force(PVector dir) {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = (Particle) particles.get(i);
p.add_force(dir);
}
}
// method to add a firing direction to an emitter.
void addDir(PVector dir) {
this.direction = dir;
}
// method to "fire" the emitter.
void fire() {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = (Particle) particles.get(i);
p.add_force(direction);
}
}
// method to add particles
void addParticle() {
particles.add(new Particle(origin,img));
}
void addParticle(Particle p) {
particles.add(p);
}
// A method to test if the particle system still has particles
boolean dead() {
if (particles.isEmpty()) {
return true;
} else {
return false;
}
}
}