// Pendulum simulation with interactive controls and accurate physics
let canvasSize = 400;
let lengthSlider, massSlider, gravitySlider, angleSlider, dampingSlider, timeScaleSlider;
let resetBtn, pauseBtn, presetBtn1, presetBtn2;
let length, mass, gravity, angle, damping, timeScale;
let angularVelocity = 0;
let angularAcceleration = 0;
let originX, originY;
let isPaused = false;
function setup() {
createCanvas(canvasSize, canvasSize);
angleMode(RADIANS);
frameRate(60);
originX = width / 2;
originY = 80;
// Sliders with realistic ranges and defaults
lengthSlider = createSlider(50, 300, 200, 1);
lengthSlider.position(10, height + 10);
lengthSlider.style('width', '180px');
massSlider = createSlider(0.1, 10, 1, 0.1);
massSlider.position(210, height + 10);
massSlider.style('width', '180px');
gravitySlider = createSlider(1, 20, 9.81, 0.1);
gravitySlider.position(10, height + 40);
gravitySlider.style('width', '180px');
angleSlider = createSlider(0, PI, PI / 4, 0.01);
angleSlider.position(210, height + 40);
angleSlider.style('width', '180px');
dampingSlider = createSlider(0, 0.1, 0.01, 0.001);
dampingSlider.position(10, height + 70);
dampingSlider.style('width', '180px');
timeScaleSlider = createSlider(0.1, 3, 1, 0.01);
timeScaleSlider.position(210, height + 70);
timeScaleSlider.style('width', '180px');
// Buttons
resetBtn = createButton('Reset');
resetBtn.position(10, height + 110);
resetBtn.mousePressed(resetSim);
pauseBtn = createButton('Pause/Play');
pauseBtn.position(80, height + 110);
pauseBtn.mousePressed(togglePause);
presetBtn1 = createButton('Preset: Small Angle');
presetBtn1.position(180, height + 110);
presetBtn1.mousePressed(presetSmallAngle);
presetBtn2 = createButton('Preset: Large Angle');
presetBtn2.position(320, height + 110);
presetBtn2.mousePressed(presetLargeAngle);
resetSim();
}
function draw() {
background(245);
// Update parameters from sliders
length = lengthSlider.value();
mass = massSlider.value();
gravity = gravitySlider.value();
damping = dampingSlider.value();
timeScale = timeScaleSlider.value();
// Only update angle if not paused
if (!isPaused) {
// Physics: angular acceleration = -(g / L) * sin(theta) - damping * angularVelocity
angularAcceleration = (-gravity / length) * sin(angle) - damping * angularVelocity;
// Integrate using semi-implicit Euler for stability
angularVelocity += angularAcceleration * (deltaTime / 1000) * timeScale;
angle += angularVelocity * (deltaTime / 1000) * timeScale;
}
// Draw title
fill(30);
noStroke();
textAlign(CENTER, CENTER);
textSize(20);
text('Pendulum Motion', width / 2, 30);
// Draw grid lines and origin reference
stroke(200);
strokeWeight(1);
for (let y = originY; y < height; y += 40) {
line(0, y, width, y);
}
for (let x = 0; x < width; x += 40) {
line(x, originY, x, height);
}
// Draw origin pivot
stroke(50);
strokeWeight(3);
fill(80);
ellipse(originX, originY, 12, 12);
// Calculate bob position
let bobX = originX + length * sin(angle);
let bobY = originY + length * cos(angle);
// Draw rod
stroke(50, 100, 200);
strokeWeight(4);
line(originX, originY, bobX, bobY);
// Draw bob with mass-dependent size
let bobRadius = map(mass, 0.1, 10, 10, 40);
fill(200, 50, 50);
stroke(120, 0, 0);
strokeWeight(2);
ellipse(bobX, bobY, bobRadius * 2, bobRadius * 2);
// Draw trail of bob (simple fading trail)
drawTrail(bobX, bobY);
// Draw velocity vector (tangential)
let velocityMag = angularVelocity * length;
let vx = velocityMag * cos(angle + HALF_PI);
let vy = velocityMag * sin(angle + HALF_PI);
drawVector(bobX, bobY, vx, vy, color(50, 150, 50), 'Velocity');
// Draw acceleration vector (tangential)
let accMag = angularAcceleration * length;
let ax = accMag * cos(angle + HALF_PI);
let ay = accMag * sin(angle + HALF_PI);
drawVector(bobX, bobY, ax, ay, color(200, 100, 0), 'Acceleration');
// Draw force vector (gravity component along arc)
let forceMag = mass * gravity * sin(angle);
let fx = forceMag * cos(angle + HALF_PI);
let fy = forceMag * sin(angle + HALF_PI);
drawVector(bobX, bobY, fx / mass, fy / mass, color(0, 0, 200), 'Force');
// Display parameters (minimal and neat)
noStroke();
fill(30);
textAlign(LEFT, TOP);
textSize(14);
text('Angle: ' + degrees(angle).toFixed(1) + '°', 10, 50);
text('Angular Vel.: ' + angularVelocity.toFixed(3) + ' rad/s', 10, 70);
text('Angular Accel.: ' + angularAcceleration.toFixed(3) + ' rad/s²', 10, 90);
text('Length: ' + length.toFixed(0) + ' px', width - 140, 50);
text('Mass: ' + mass.toFixed(1) + ' kg', width - 140, 70);
text('Gravity: ' + gravity.toFixed(2) + ' m/s²', width - 140, 90);
text('Damping: ' + damping.toFixed(3), width - 140, 110);
text('Time Scale: ' + timeScale.toFixed(2) + 'x', width - 140, 130);
// Draw origin label
fill(80);
noStroke();
textAlign(CENTER, CENTER);
textSize(12);
text('Pivot', originX, originY - 15);
}
// Trail storage and drawing
let trail = [];
function drawTrail(x, y) {
trail.push({ x, y, alpha: 255 });
if (trail.length > 60) trail.shift();
noFill();
stroke(200, 50, 50);
strokeWeight(2);
beginShape();
for (let i = 0; i < trail.length; i++) {
let p = trail[i];
stroke(200, 50, 50, p.alpha);
vertex(p.x, p.y);
p.alpha -= 4;
if (p.alpha < 0) p.alpha = 0;
}
endShape();
}
// Draw vector with arrowhead and label
function drawVector(x, y, vx, vy, col, label) {
push();
stroke(col);
fill(col);
strokeWeight(3);
translate(x, y);
line(0, 0, vx * 30, vy * 30);
let arrowSize = 7;
push();
translate(vx * 30, vy * 30);
rotate(atan2(vy, vx));
triangle(0, 0, -arrowSize, arrowSize / 2, -arrowSize, -arrowSize / 2);
pop();
noStroke();
textSize(12);
fill(col);
textAlign(LEFT, BOTTOM);
text(label, vx * 30 + 5, vy * 30 - 5);
pop();
}
// Reset simulation to current slider values
function resetSim() {
angle = angleSlider.value();
angularVelocity = 0;
angularAcceleration = 0;
trail = [];
isPaused = false;
}
// Pause/play toggle
function togglePause() {
isPaused = !isPaused;
}
// Preset: Small angle oscillation (linear approx)
function presetSmallAngle() {
lengthSlider.value(200);
massSlider.value(1);
gravitySlider.value(9.81);
angleSlider.value(PI / 12);
dampingSlider.value(0.005);
timeScaleSlider.value(1);
resetSim();
}
// Preset: Large angle oscillation (nonlinear)
function presetLargeAngle() {
lengthSlider.value(150);
massSlider.value(2);
gravitySlider.value(9.81);
angleSlider.value(PI / 2);
dampingSlider.value(0.02);
timeScaleSlider.value(1);
resetSim();
}
// Mouse drag interaction to set angle directly
function mouseDragged() {
if (mouseY < originY) return; // Only below pivot
let dx = mouseX - originX;
let dy = mouseY - originY;
let newAngle = atan2(dx, dy);
if (newAngle < 0) newAngle += TWO_PI;
if (newAngle > PI) newAngle = PI; // Limit to physically meaningful range
angle = newAngle;
angularVelocity = 0;
trail = [];
angleSlider.value(angle);
}