-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.js
41 lines (36 loc) · 939 Bytes
/
classes.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
class Orbit {
constructor(x, y, speed, history) {
this.pos = createVector(x, y);
this.vel = p5.Vector.random2D();
this.acc = 0;
this.speed = speed;
this.history = [];
this.lifespan = history || 160;
this.vel.mult(8);
}
update(x, y) {
let follow = createVector(x, y);
this.acc = p5.Vector.sub(follow, this.pos);
this.acc.setMag(0.8);
this.vel.add(this.acc);
this.vel.limit(this.speed);
this.pos.add(this.vel);
let v = createVector(this.pos.x, this.pos.y);
this.history.push(v);
if (this.history.length > this.lifespan) this.history.shift();
}
show() {
beginShape();
for (let i = 1; i < this.history.length; i++) {
let opacity = i < 35 ? i : 35;
stroke(100, 100, 100, opacity);
line(
this.history[i].x,
this.history[i].y,
this.history[i - 1].x,
this.history[i - 1].y
);
}
endShape();
}
}