const g = 9.8 / (144 * 144); let res = 20/10; class Pendulum { constructor(xoffset, yoffset, scale) { this.x1 = 0; this.y1 = 0; this.x2 = 0; this.y2 = 0; this.m1 = 2; this.m2 = 2; this.a1 = Math.PI / 2 + random(-Math.PI, Math.PI) / 16 + Math.PI/4; this.a2 = Math.PI + random(-Math.PI, Math.PI) / 16; this.r1 = 2; this.r2 = 2; this.a1_v = 0; this.a2_v = 0; this.a1_a = 0; this.a2_a = 0; this.xoffset = xoffset; this.yoffset = yoffset; this.scale = scale; this.poslog = []; this.fc = 0; this.setPos(); } update() { this.a1_v += this.a1_a; this.a2_v += this.a2_a; this.a1 += this.a1_v; this.a2 += this.a2_v; this.updateA(); this.setPos(); if ((this.fc % res === 0) && (this.fc > 144*3)) { this.logPos(); } } show() { this.fc++; push(); translate(this.xoffset, this.yoffset); strokeWeight(20 * this.scale); point(0, 0); strokeWeight(40 * this.scale); point(this.x1, this.y1); point(this.x2, this.y2); strokeWeight(8 * this.scale); line(0, 0, this.x1, this.y1); line(this.x1, this.y1, this.x2, this.y2); pop(); this.showTrail(); } setPos() { this.x1 = sin(this.a1) * this.r1 * 100 * this.scale; this.y1 = cos(this.a1) * this.r1 * 100 * this.scale; this.x2 = this.x1 + sin(this.a2) * this.r2 * 100 * this.scale; this.y2 = this.y1 + cos(this.a2) * this.r2 * 100 * this.scale; } 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); } logPos() { if(this.poslog.length<144/res*10) this.poslog.push(createVector(this.x2, this.y2)); else{ this.poslog.shift(); this.poslog.push(createVector(this.x2, this.y2)); } } showTrail() { push(); translate(this.xoffset, this.yoffset); strokeWeight(2 * this.scale); stroke(33); noFill(); beginShape(); for (let i = 0; i < this.poslog.length; i++) { vertex(this.poslog[i].x, this.poslog[i].y); } endShape(); pop(); } }