-
Notifications
You must be signed in to change notification settings - Fork 5
/
spring.js
48 lines (37 loc) · 1.06 KB
/
spring.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
export default class Spring {
constructor(k, l, a, b, maxl, twoWay = true) {
this.k = k;
this.l = l;
this.a = a;
this.b = b;
this.twoWay = twoWay;
this.maxL = maxl || Infinity;
this.currentL = l;
this.isBroken = false;
}
update() {
let force = p5.Vector.sub(this.a.position, this.b.position);
this.currentL = force.mag();
if (this.currentL > this.maxL) {
this.isBroken = true;
}
let x = this.currentL - this.l;
if (!this.twoWay && x < 0) return;
force.normalize();
force.mult(this.k * x);
this.b.applyForce(force);
force.mult(-1);
this.a.applyForce(force);
}
show() {
strokeWeight(map(this.currentL, this.l, this.maxL, 6, 1));
// strokeWeight(4);
stroke(map(this.currentL, this.l, this.maxL, 70, 255), 70, 70);
line(
this.a.position.x,
this.a.position.y,
this.b.position.x,
this.b.position.y
);
}
}