const g = 9.8 / (144 * 144); const h = 2*Math.PI/12; let speed = 0; let xoffset = 0; let yoffset = 0; let scale = 0; let res = 20/10; //let c = color(255, 204, 0); // Define color 'c' //fill(c); class Pendulum { constructor(xoffs, yoffs, scl, spd) { speed = spd; xoffset = xoffs; yoffset = yoffs; scale = scl; this.x1 = 0; this.y1 = 0; this.a1 = Math.PI;//Math.PI / 2 + random(-Math.PI, Math.PI) / 16 + Math.PI/4; this.a2 = Math.PI;//Math.PI + random(-Math.PI, Math.PI) / 16; this.r1 = 2; this.xoffset = xoffset; this.yoffset = yoffset; this.scale = scale; this.speed = speed; this.setPos(); } update() { //this.a1_v += this.a1_a; //this.a2_v += this.a2_a; this.a1 -= (h*this.speed)/144; //this.a2 -= h/144; //this.updateA(); this.setPos(); } show() { if (this.speed == 0.5) stroke(20); else stroke(100); push(); translate(this.xoffset, this.yoffset); strokeWeight(6 * this.scale); point(0, 0); strokeWeight(5 * this.scale); point(this.x1, this.y1); strokeWeight(6 * this.scale); line(0, 0, this.x1, this.y1); //line(this.x1, this.y1, this.x2, this.y2); pop(); } setPos() { this.x1 = sin(this.a1) * this.r1 * 100 * this.scale; this.y1 = cos(this.a1) * this.r1 * 100 * this.scale; } refresh() { this.x1 = 0; this.y1 = 0; this.a1 = Math.PI;//Math.PI / 2 + random(-Math.PI, Math.PI) / 16 + Math.PI/4; this.a2 = Math.PI;//Math.PI + random(-Math.PI, Math.PI) / 16; this.r1 = 2; this.xoffset = xoffset; this.yoffset = yoffset; this.scale = scale; this.speed = speed; this.setPos(); } updateA() { // let d = 2 * this.m1 + this.m2 - this.m2 * cos(2 * this.a1 - 2 * this.a2); // let num1 = -g * (2 * this.m1 + this.m2) * sin(this.a1); // let num2 = -this.m2 * g * sin(this.a1 - 2 * this.a2); // let num3 = -2 * sin(this.a1 - this.a2) * this.m2; // let num4 = this.a2_v * this.a2_v * this.r2 + this.a1_v * this.a1_v * this.r1 * cos(this.a1 - this.a2); // this.a1_a = (num1 + num2 + num3 * num4) / (this.r1 * d); // num1 = 2 * sin(this.a1 - this.a2); // num2 = this.a1_v * this.a1_v * this.r1 * (this.m1 + this.m2); // num3 = g * (this.m1 + this.m2) * cos(this.a1); // num4 = this.a2_v * this.a2_v * this.r2 * this.m2 * cos(this.a1 - this.a2); // this.a2_a = (num1 * (num2 + num3 + num4)) / (this.r2 * d); this.a1 += Math.PI/12; } }