This repository has been archived by the owner on Jun 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticle.pde
64 lines (57 loc) · 1.53 KB
/
Particle.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
class Particle {
PVector p, v, a; //position, velocity, acceleration
float d; //diameter
color c; //colour
int life; //lifespan
boolean dead; //dead or naw
Particle(float tempX, float tempY) {
p = new PVector(tempX, tempY);
v = new PVector(random(-3, 3), 0);
a = new PVector(0, 0.1);
d = 40;
c = color(255, random(0, 155), random(0, 155));
life = 120; //lifespan of 2 seconds approx.
dead = false;
}
void display() {
noStroke();
fill(c, 50);
rect(p.x, p.y, d, d);
}
void move() { //moves according to gravity
v = v.add(a);
p = p.add(v);
}
void timer() { //timer to remove object after certain amount of time (reduce lag)
life--;
if (life < 0) {
dead = true;
}
}
void collision() { //bounces off of the walls & ground
if (p.x <= 0 || p.x + d >= width) {
v.x *= -1;
} else if (p.y >= height || p.y + d <= 0) {
v.y *= -1;
p.y = (height - d);
}
}
}
void particleBackground() { //function to draw the pretty menu background
background(0);
for (int i = 0; i < 2; i++) { //add two particles per draw
particles.add(new Particle(random(width), random(height/2)));
}
for (Particle p : particles) { //for every particle...
p.display();
p.move();
p.collision();
p.timer();
}
for (int i = 0; i < particles.size(); i++) { //for loop to get dead particles and remove them from the array
Particle p = particles.get(i);
if (p.dead) { //if dead = true
particles.remove(i); //remove from arraylist
}
}
}