xxxxxxxxxx
41
// Pendulum Simulator
//
// V0.1 2020-04-17 NPGS
//
const PIX = 500;
const PIX_P_M = 200;
const BOB_DIAM = 20;
const L = 1;
const THETA = 0.7;
const THETA_DOT = +6.15;
const STOKES_DRAG = 0.05;
const G = 9.80665;
const TSTEP = 0.033; // Reciprocal of frame rate
function setup() {
createCanvas(PIX, PIX);
fill(0, 20, 200);
stroke(150);
background(225);
frameRate(30);
theta = THETA;
theta_dot = THETA_DOT;
theta_dotdot = 0;
}
function draw() {
background(225);
x = PIX_P_M * L * sin(theta);
y = PIX_P_M * L * cos(theta);
line(PIX / 2, PIX / 2, PIX / 2 + x, PIX / 2 + y);
circle(PIX / 2 + x, PIX / 2 + y, BOB_DIAM);
theta_dotdot = -G * sin(theta) / L - STOKES_DRAG * L * theta_dot;
theta_dot = theta_dot + theta_dotdot * TSTEP;
theta = theta + theta_dot * TSTEP;
}