119 lines
3.4 KiB
JavaScript
119 lines
3.4 KiB
JavaScript
|
const g = 9.8 / (144 * 144);
|
||
|
let res = Math.ceil(20/20);
|
||
|
class Pendulum {
|
||
|
constructor(xoffset, yoffset, scale) {
|
||
|
this.x1 = 0;
|
||
|
this.y1 = 0;
|
||
|
this.x2 = 0;
|
||
|
this.y2 = 0;
|
||
|
this.x3 = 0;
|
||
|
this.y3 = 0;
|
||
|
this.m1 = 2;
|
||
|
this.m2 = 2;
|
||
|
this.a1 = Math.PI;
|
||
|
this.a2 = Math.PI;
|
||
|
this.a3 = Math.PI;
|
||
|
this.r1 = random(0.5,3.5);
|
||
|
this.r2 = random(0.5,3.5);
|
||
|
this.r3 = 2.5;
|
||
|
this.a1_v = Math.PI/64 * random(-1,1);
|
||
|
this.a2_v = Math.PI/64 * random(-1,1);
|
||
|
this.a3_v = Math.PI/64 * random(-1,1);
|
||
|
this.xoffset = xoffset;
|
||
|
this.yoffset = yoffset;
|
||
|
this.scale = scale;
|
||
|
this.poslog = [];
|
||
|
this.fc = 0;
|
||
|
this.setPos();
|
||
|
}
|
||
|
|
||
|
refresh(){
|
||
|
this.a1_v = Math.PI/64 * random(-1,1);
|
||
|
this.a2_v = Math.PI/64 * random(-1,1);
|
||
|
this.r1 = random(0.5,3.5);
|
||
|
this.r2 = random(0.5,3.5);
|
||
|
this.poslog = [];
|
||
|
}
|
||
|
|
||
|
update() {
|
||
|
this.a1 += this.a1_v;
|
||
|
this.a2 += this.a2_v;
|
||
|
// this.a3 += this.a3_v;
|
||
|
this.setPos();
|
||
|
if (this.fc % res === 0) {
|
||
|
this.logPos();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
show() {
|
||
|
this.fc++;
|
||
|
|
||
|
push();
|
||
|
translate(this.xoffset, this.yoffset);
|
||
|
|
||
|
strokeWeight(10 * this.scale);
|
||
|
point(0, 0);
|
||
|
|
||
|
strokeWeight(20 * this.scale);
|
||
|
point(this.x1, this.y1);
|
||
|
point(this.x2, this.y2);
|
||
|
point(this.x3, this.y3);
|
||
|
|
||
|
|
||
|
strokeWeight(4 * this.scale);
|
||
|
line(0, 0, this.x1, this.y1);
|
||
|
line(this.x1, this.y1, this.x2, this.y2);
|
||
|
//line(this.x2, this.y2, this.x3, this.y3);
|
||
|
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;
|
||
|
// this.x3 = this.x2 + sin(this.a3) * this.r3 * 100 * this.scale;
|
||
|
// this.y3 = this.y2 + cos(this.a3) * this.r3 * 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*100)
|
||
|
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(3 * 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();
|
||
|
}
|
||
|
}
|