-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleSystem.pde
More file actions
48 lines (41 loc) · 1.05 KB
/
ParticleSystem.pde
File metadata and controls
48 lines (41 loc) · 1.05 KB
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
import java.util.*;
class ParticleSystem {
ArrayList<Particle> particles;
boolean running = false;
ParticleSystem() {
particles = new ArrayList<Particle>();
}
void start(float centerX, float centerY, int lifetime) {
running = true;
for (int i=0; i < 50; i++) {
float angle = random(0, TWO_PI);
PVector dir= new PVector(sin(angle), cos(angle));
particles.add(
new Particle(
new PVector(centerX, centerY), //position
dir.mult(random(1, 3)), //velocity
new PVector(dir.x*-1/(float)lifetime, dir.y*-1/(float)lifetime), //acceleration
lifetime)
);
}
}
void stop() {
running = false;
}
void run() {
if (running) {
Iterator<Particle> particleIterator = particles.iterator();
while (particleIterator.hasNext()) {
Particle p = particleIterator.next();
p.update();
p.display();
if (p.isDead()) {
particleIterator.remove();
}
}
if (particles.size() == 0) {
stop();
}
}
}
}