bnb({
w: 1000,
h: 800,
record: true,
numFrames: 500,
fps: 60,
globals: {
points : [],
pendulum1: null,
pendulum2: null
},
draw: (sketch, time, globals) => {
sketch.background(225)
sketch.stroke(0);
sketch.strokeWeight(4);
globals.pendulum1.show()
globals.pendulum1.update()
globals.pendulum2.show()
globals.pendulum2.update()
for(var i = 0; i <= globals.points.length-1; i++){
sketch.point= globals.points[i]
sketch.fill(sketch.point.color);
sketch.stroke(sketch.point.color)
sketch.ellipse(sketch.point.x, sketch.point.y, 2, 0)
}
},
setup: (sketch, globals) => {
globals.pendulum1 = new pendulum(Math.PI*0.6, Math.PI,'black');
globals.pendulum2 = new pendulum(Math.PI*-0.5,Math.PI,'red');
function pendulum(o1, o2, color){
var center = {
x : 500,
y : 200
}
var r1 = 150
var r2 = 200
var m1 = 20
var m2 = 10
var a1 = o1
var a1_v = 0
var a2 = o2
var a2_v = 0
this.show = function(){
var x1 = {
x : center.x+Math.sin(a1)*r1,
y : center.y+Math.cos(a1)*r1
}
var x2 = {
x : x1.x+Math.sin(a2)*r1,
y: x1.y+Math.cos(a2)*r1
}
sketch.fill(color)
sketch.stroke(color)
sketch.line(center.x, center.y, x1.x, x1.y)
sketch.ellipse(x1.x, x1.y, m1, m1)
sketch.line(x1.x, x1.y, x2.x, x2.y)
sketch.ellipse(x2.x, x2.y, m2, m2)
if(globals.points.length>300){
globals.points = globals.points.reverse().slice(globals.points.length.length-400+1, 395).reverse()
}
if(sketch.frameCount % 1 == 0){
globals.points.push({x: x2.x, y:x2.y, color:color})
}
}
this.update = function(){
var g = 1
let num1 = -g * (2 * m1 + m2) * sketch.sin(a1);
let num2 = -m2 * g * sketch.sin(a1 - 2 * a2);
let num3 = -2 * sketch.sin(a1 - a2) * m2;
let num4 = a2_v * a2_v * r2 + a1_v * a1_v * r1 * sketch.cos(a1 - a2);
let den = r1 * (2 * m1 + m2 - m2 * sketch.cos(2 * a1 - 2 * a2));
let acceleration1 = (num1 + num2 + num3 * num4) / den;
num1 = 2 * sketch.sin(a1 - a2);
num2 = a1_v * a1_v * r1 * (m1 + m2);
num3 = g * (m1 + m2) * sketch.cos(a1);
num4 = a2_v * a2_v * r2 * m2 * sketch.cos(a1 - a2);
den = r2 * (2 * m1 + m2 - m2 * sketch.cos(2 * a1 - 2 * a2));
let acceleration2 = (num1 * (num2 + num3 + num4)) / den;
a1_v += acceleration1
a2_v += acceleration2
a1 += a1_v
a2 += a2_v
a1_v *= 1
a2_v *= 1
}
}
}
})