xxxxxxxxxx
61
class Spring {
constructor(w, x, m, b, k, l) {
this.wallPos = w;
this.pos = x;
this.vel = 0;
this.acc = 0;
this.x = 0;
this.m = m;
this.b = b;
this.k = k;
this.l = l;
this.pMouse = 0;
}
springForce() {
this.x = this.pos - this.l;
this.acc = -this.k / this.m * this.x - this.b / this.m * this.vel;
}
mouseInput() {
if (mouseIsPressed && mouseY > 0 && mouseY < height) {
this.vel = 0;
this.pos += mouseX - this.pMouse;
}
this.pMouse = mouseX;
}
update() {
this.springForce();
this.vel += this.acc;
this.mouseInput();
this.pos += this.vel;
this.acc = 0;
}
show() {
push();
strokeWeight(3);
fill(0, 0, 255);
noStroke();
rectMode(CENTER);
rect(this.pos, height / 2, 35, 70);
if (this.x < 0) {
stroke(255, 0, 0);
} else {
stroke(0 ,255, 0);
}
let d = (this.pos - this.wallPos) / 16;
line(this.wallPos, height / 2, this.wallPos + d, height / 2);
line(this.wallPos + d, height / 2, this.wallPos + 2 * d, height / 2 - 20);
line(this.wallPos + 2 * d, height / 2 - 20, this.wallPos + 4 * d, height / 2 + 20);
line(this.wallPos + 4 * d, height / 2 + 20, this.wallPos + 6 * d, height / 2 - 20);
line(this.wallPos + 6 * d, height / 2 - 20, this.wallPos + 8 * d, height / 2 + 20);
line(this.wallPos + 8 * d, height / 2 + 20, this.wallPos + 10 * d, height / 2 - 20);
line(this.wallPos + 10 * d, height / 2 - 20, this.wallPos + 12 * d, height / 2 + 20);
line(this.wallPos + 12 * d, height / 2 + 20, this.wallPos + 14 * d, height / 2 - 20);
line(this.wallPos + 14 * d, height / 2 - 20, this.wallPos + 15 * d, height / 2);
line(this.wallPos + 15 * d, height / 2, this.wallPos + 16 * d, height / 2);
pop();
}
}