// Pendulum Motion Simulation - Accurate, Interactive, Educational
let canvasW = 600;
let canvasH = 400;
// Pendulum parameters controlled by sliders
let lengthSlider, massSlider, gravitySlider, dampingSlider, angleSlider, timeScaleSlider;
// Buttons
let resetBtn, pauseBtn, presetBtn1, presetBtn2;
let originX, originY;
let angle; // Current angle (radians)
let angleVelocity; // Angular velocity (rad/s)
let angleAcceleration; // Angular acceleration (rad/s²)
let length; // Length of pendulum (m)
let mass; // Mass of bob (kg)
let gravity; // Gravity acceleration (m/s²)
let damping; // Damping coefficient (dimensionless, <1)
let initialAngle; // Initial angle (radians)
let timeScale; // Time scale multiplier
let isPaused = false;
let prevX, prevY; // For trail drawing
function setup() {
createCanvas(canvasW, canvasH);
frameRate(60);
originX = width / 2;
originY = 80;
// Sliders with realistic ranges and defaults
lengthSlider = createSlider(0.2, 2.5, 1.0, 0.01); // Length in meters
lengthSlider.position(20, height + 10);
lengthSlider.style('width', '140px');
massSlider = createSlider(0.1, 5.0, 1.0, 0.01); // Mass in kg
massSlider.position(180, height + 10);
massSlider.style('width', '140px');
gravitySlider = createSlider(1, 20, 9.81, 0.01); // Gravity m/s²
gravitySlider.position(340, height + 10);
gravitySlider.style('width', '140px');
dampingSlider = createSlider(0.90, 1.00, 0.995, 0.0005); // Damping factor (dimensionless)
dampingSlider.position(20, height + 50);
dampingSlider.style('width', '140px');
angleSlider = createSlider(0, 90, 30, 1); // Initial angle degrees
angleSlider.position(180, height + 50);
angleSlider.style('width', '140px');
timeScaleSlider = createSlider(0.1, 3.0, 1.0, 0.01); // Time scale multiplier
timeScaleSlider.position(340, height + 50);
timeScaleSlider.style('width', '140px');
// Buttons
resetBtn = createButton('Reset');
resetBtn.position(510, height + 10);
resetBtn.mousePressed(resetSim);
pauseBtn = createButton('Pause/Play');
pauseBtn.position(510, height + 50);
pauseBtn.mousePressed(togglePause);
presetBtn1 = createButton('Preset: Earth');
presetBtn1.position(510, height + 90);
presetBtn1.mousePressed(() => {
lengthSlider.value(1.0);
massSlider.value(1.0);
gravitySlider.value(9.81);
dampingSlider.value(0.995);
angleSlider.value(30);
timeScaleSlider.value(1.0);
resetSim();
});
presetBtn2 = createButton('Preset: Moon');
presetBtn2.position(510, height + 130);
presetBtn2.mousePressed(() => {
lengthSlider.value(1.0);
massSlider.value(1.0);
gravitySlider.value(1.62);
dampingSlider.value(0.998);
angleSlider.value(45);
timeScaleSlider.value(1.0);
resetSim();
});
resetSim();
}
function draw() {
background(245);
// Draw subtle grid lines for reference
drawGrid();
// Update parameters from sliders
length = lengthSlider.value();
mass = massSlider.value();
gravity = gravitySlider.value();
damping = dampingSlider.value();
initialAngle = radians(angleSlider.value());
timeScale = timeScaleSlider.value();
// Title
fill(30);
noStroke();
textAlign(CENTER, TOP);
textSize(22);
text('Pendulum Motion', width / 2, 10);
// Physics update if not paused
if (!isPaused) {
// Use fixed timestep for stability
let dt = (deltaTime / 1000) * timeScale; // seconds scaled by timeScale
// Equation of motion: θ'' = -(g / L) * sin(θ) - damping * θ'
angleAcceleration = (-gravity / length) * sin(angle);
angleVelocity += angleAcceleration * dt;
angleVelocity *= damping;
angle += angleVelocity * dt;
}
// Calculate bob position in pixels
// Scale length to pixels: 1m = 150px approx
let scalePx = 150;
let bobX = originX + length * scalePx * sin(angle);
let bobY = originY + length * scalePx * cos(angle);
// Draw pendulum arm
stroke(50);
strokeWeight(3);
line(originX, originY, bobX, bobY);
// Draw origin pivot
fill(50);
noStroke();
ellipse(originX, originY, 12, 12);
// Draw bob with mass-dependent size
let bobRadius = map(mass, 0.1, 5, 12, 40);
fill(70, 130, 180);
stroke(30, 80, 140);
strokeWeight(2);
ellipse(bobX, bobY, bobRadius * 2, bobRadius * 2);
// Draw trail of bob (fading)
drawTrail(bobX, bobY);
// Draw vectors: velocity and acceleration at bob position
drawVectors(bobX, bobY);
// Display parameters (minimal, clean)
fill(30);
noStroke();
textAlign(LEFT, TOP);
textSize(14);
// Angular velocity in rad/s and convert to deg/s
let angVelDeg = degrees(angleVelocity);
// Linear velocity v = L * angular velocity
let linearVelocity = abs(angleVelocity * length); // m/s
// Linear acceleration a = L * angular acceleration
let linearAcceleration = abs(angleAcceleration * length); // m/s²
// Force F = m * a (tangential)
let force = mass * linearAcceleration;
// Position parameters neatly bottom-left
let px = 10;
let py = height - 90;
text('Length: ' + length.toFixed(2) + ' m', px, py);
text('Mass: ' + mass.toFixed(2) + ' kg', px, py + 18);
text('Gravity: ' + gravity.toFixed(2) + ' m/s²', px, py + 36);
text('Angle: ' + degrees(angle).toFixed(1) + '°', px, py + 54);
text('Velocity: ' + linearVelocity.toFixed(2) + ' m/s', px, py + 72);
// Position parameters top-right
px = width - 180;
py = 40;
text('Angular Vel: ' + angVelDeg.toFixed(1) + ' °/s', px, py);
text('Acceleration: ' + linearAcceleration.toFixed(2) + ' m/s²', px, py + 18);
text('Force: ' + force.toFixed(2) + ' N', px, py + 36);
text('Damping: ' + damping.toFixed(4), px, py + 54);
text('Time Scale: ' + timeScale.toFixed(2) + 'x', px, py + 72);
// Draw arc showing current angle from vertical
drawAngleArc(originX, originY, length * scalePx, angle);
// Mouse interaction: drag bob to set angle if mouse pressed near bob
if (mouseIsPressed) {
let d = dist(mouseX, mouseY, bobX, bobY);
if (d < bobRadius + 10) {
// Calculate angle from origin to mouse
let dx = mouseX - originX;
let dy = mouseY - originY;
angle = atan2(dx, dy) * -1 + PI; // atan2(y,x) but inverted for pendulum
angleVelocity = 0; // reset velocity when dragging
}
}
}
// Draw subtle grid lines for reference
function drawGrid() {
stroke(200);
strokeWeight(1);
for (let x = 0; x <= width; x += 50) {
line(x, 0, x, height);
}
for (let y = 0; y <= height; y += 50) {
line(0, y, width, y);
}
}
// Draw fading trail behind bob
let trail = [];
function drawTrail(x, y) {
trail.push({ x: x, y: y, alpha: 255 });
if (trail.length > 60) trail.shift();
noFill();
stroke(70, 130, 180);
strokeWeight(2);
beginShape();
for (let i = 0; i < trail.length; i++) {
let p = trail[i];
stroke(70, 130, 180, p.alpha);
vertex(p.x, p.y);
p.alpha -= 4;
if (p.alpha < 0) p.alpha = 0;
}
endShape();
}
// Draw velocity and acceleration vectors at bob
function drawVectors(x, y) {
// Velocity vector (tangential)
// Direction perpendicular to pendulum arm (clockwise positive)
let scaleV = 50; // scaling for visualization
let vx = angleVelocity * length * scaleV * cos(angle + HALF_PI);
let vy = angleVelocity * length * scaleV * sin(angle + HALF_PI);
// Acceleration vector (tangential)
let scaleA = 100;
let ax = angleAcceleration * length * scaleA * cos(angle + HALF_PI);
let ay = angleAcceleration * length * scaleA * sin(angle + HALF_PI);
// Velocity vector - blue
stroke(30, 144, 255);
strokeWeight(3);
line(x, y, x + vx, y + vy);
fill(30, 144, 255);
noStroke();
push();
translate(x + vx, y + vy);
rotate(atan2(vy, vx));
triangle(0, 0, -8, 4, -8, -4);
pop();
// Acceleration vector - red
stroke(220, 20, 60);
strokeWeight(3);
line(x, y, x + ax, y + ay);
fill(220, 20, 60);
noStroke();
push();
translate(x + ax, y + ay);
rotate(atan2(ay, ax));
triangle(0, 0, -8, 4, -8, -4);
pop();
}
// Draw arc showing current angle from vertical
function drawAngleArc(x, y, r, ang) {
noFill();
stroke(100, 100, 100, 150);
strokeWeight(2);
let start = 0;
let end = ang;
if (ang < 0) {
start = ang;
end = 0;
}
arc(x, y, r / 2, r / 2, start, end);
}
// Reset simulation to initial conditions
function resetSim() {
angle = initialAngle;
angleVelocity = 0;
angleAcceleration = 0;
trail = [];
isPaused = false;
}
// Toggle pause/play state
function togglePause() {
isPaused = !isPaused;
}