-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle.js
More file actions
66 lines (61 loc) · 2 KB
/
Copy pathParticle.js
File metadata and controls
66 lines (61 loc) · 2 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function Particle(x, y, hu, firework) {
// Declare variables
this.location = createVector(x, y);
this.firework = firework;
this.lifespan = 255;
this.hu = hu;
// If firework is true it will move up at random locations
if(this.firework) {
this.velocity = createVector(0, random(-18, -7));
} else {
// If it's false it will go in any random direction
// at a random distance
this.velocity = p5.Vector.random2D();
this.velocity.mult(random(2, 10));
}
this.acceleration = createVector(0, 0);
//-------------------------------------------------------
this.show = function() {
//If it's not a firework
if(!this.firework) {
colorMode(HSB);
strokeWeight(1);
stroke(hu, 255, 255, this.lifespan);
} else {
// If it's a firework
colorMode(HSB);
strokeWeight(1);
stroke(hu, 255, 255);
}
point(this.location.x, this.location.y);
}
//-------------------------------------------------------
this.update = function() {
// If this is not a firework slow down the expoded particles
// and minimize the lifespan
if (!this.firework) {
this.velocity.mult(0.9);
this.lifespan -= 4;
}
// Add the velocity to acceleration and
// the location to velocity and reset the acceeleration
this.velocity.add(this.acceleration);
this.location.add(this.velocity);
this.acceleration.mult(0);
}
//-------------------------------------------------------
// If lifespan is less then 0 then the particle is done
this.done = function() {
if (this.lifespan < 0) {
return true;
} else {
return false;
}
}
//-------------------------------------------------------
// Force accumulation
// Add a force to the acceleration
this.applyForce = function(force) {
this.acceleration.add(force);
}
}