xxxxxxxxxx
50
class Spring{
constructor(k, restLength, a, b){
this.k = k;
this.restLength = restLength;
this.a = a;
this.b = b;
}
update(){
let force = p5.Vector.sub(this.b.position, this.a.position);
let x = force.mag() - this.restLength;
force.normalize();
force.mult(this.k*x);
this.a.applyForce(force);
force.mult(-1);
this.b.applyForce(force);
}
show(){
strokeWeight(64);
stroke(0);
line(this.a.position.x, this.a.position.y, this.b.position.x, this.b.position.y);
strokeWeight(60);
stroke(255);
line(this.a.position.x, this.a.position.y, this.b.position.x, this.b.position.y);
strokeWeight(4);
stroke(0);
// line(this.a.position.x, this.a.position.y, this.b.position.x, this.b.position.y);
// perpendiculine(this.a.position.x, this.a.position.y, this.b.position.x, this.b.position.y);
}
}
function perpendiculine(x1, y1, x2, y2){
// let ang = atan2(x1,y1,x2- x1, y2 - y1);
// ang += PI/2;
// let x3 =
let a = (y2 - y1) / (x2 - x1);
// let y = a * x
let b = y - a*x;
y3 = a *(x2-x1) + y1;
line(x1, y1, x1 + x3, y1 + y3);
}