-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle.js
53 lines (47 loc) · 1.07 KB
/
particle.js
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
class Particle {
constructor() {
this.pos = createVector(random(width), random(height));
this.prevPos = this.pos.copy();
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.maxspeed = 3;
}
update() {
this.prevPos = this.pos.copy();
this.vel.add(this.acc);
this.vel.limit(this.maxspeed);
this.pos.add(this.vel);
this.acc.mult(0);
// wrap around
if (this.pos.x > width + 3) {
this.pos.x = 0;
this.prevPos = this.pos.copy();
}
if (this.pos.x < 0 - 3) {
this.pos.x = width;
this.prevPos = this.pos.copy();
}
if (this.pos.y > height + 3) {
this.pos.y = 0;
this.prevPos = this.pos.copy();
}
if (this.pos.y < 0 - 3) {
this.pos.y = height;
this.prevPos = this.pos.copy();
}
}
applyForce(force) {
force.limit(0.6);
this.acc.add(force);
}
show() {
stroke(255, 1);
strokeWeight(1);
line(this.prevPos.x, this.prevPos.y, this.pos.x, this.pos.y);
}
followField(noise, xoff, yoff, toff) {
const angle = noise(xoff, yoff, toff) * TWO_PI * 2;
const v = p5.Vector.fromAngle(angle);
this.applyForce(v);
}
}