xxxxxxxxxx
42
class Hair{
constructor(x, y, len){
this.pos = createVector();
this.color = color("rgba(138, 76, 15, 0.5)");
this.root = createVector(x, y);
//spring constants
this.angle = 10;
this.angleV = 0;
this.angleA = 0;
this.len = len;
this.gravity = 0.9;
}
show(){
push();
strokeWeight(4);
stroke(this.color);
line(this.root.x, this.root.y, this.pos.x, this.pos.y);
pop();
}
update(){
// pendulum logic
let an = map(angle, -60, 60, 110, -110)
let force = this.gravity * sin(this.angle+an);
this.angleA = (-1 * force) / (this.len/4);
this.angleV += this.angleA;
this.angle += this.angleV;
this.angleV *= 0.99; // slow it down over time
this.pos.x = this.len * sin(this.angle) + this.root.x;
this.pos.y = this.len * cos(this.angle) + this.root.y;
}
}