class SimplePendulum extends p5.Vector { constructor(x, y) { super(x, y); this.角度 = 0; this.角速度 = 0; this.摆长 = 100; this.重锤 = createVector(this.x, this.y + this.摆长); } update(damping = 0) { if (mouseIsPressed) { this.重锤 = createVector(mouseX, mouseY); this.摆长 = this.dist(this.重锤); this.角速度 = 0; const 向下矢量 = createVector(0, 1); this.角度 = p5.Vector.sub(this.重锤, this).angleBetween(向下矢量); } else { // 未考虑时间精确性及重力加速度 this.角速度 -= sin(this.角度) / this.摆长; // -sin(Θ)g/L this.角速度 = lerp(this.角速度, 0, damping); // 模拟阻尼 this.角度 += this.角速度; this.重锤.x = this.x + this.摆长 * sin(this.角度); this.重锤.y = this.y + this.摆长 * cos(this.角度); } } draw() { push(); fill(128); stroke(255); strokeWeight(1); line(this.x, this.y, this.重锤.x, this.重锤.y); circle(this.重锤.x, this.重锤.y, 20); circle(this.x, this.y, 4); pop(); } }