// Pendulum simulation with accurate physics and interactive controls
// Pendulum physical variables
let length; // Length of pendulum (m)
let mass; // Mass of bob (kg)
let gravity; // Gravitational acceleration (m/s^2)
let damping; // Damping coefficient (dimensionless, 0-1)
let angle; // Current angle (rad)
let angularVelocity; // Angular velocity (rad/s)
let angularAcceleration; // Angular acceleration (rad/s^2)
let timeScale; // Time scaling factor for simulation speed
// Controls
let lengthSlider, massSlider, gravitySlider, dampingSlider, angleSlider, timeScaleSlider;
let resetBtn, pauseBtn, presetBtn1, presetBtn2;
let isPaused = false;
function setup() {
createCanvas(400, 400);
// Default values
length = 1.0; // meters
mass = 1.0; // kg
gravity = 9.81; // m/s^2
damping = 0.995; // dimensionless
angle = PI / 4; // 45 degrees initial displacement
angularVelocity = 0;
angularAcceleration = 0;
timeScale = 1.0;
// Sliders with realistic ranges and steps
lengthSlider = createSlider(0.2, 3.0, length, 0.01);
lengthSlider.position(10, height + 10);
lengthSlider.style('width', '120px');
massSlider = createSlider(0.1, 5.0, mass, 0.01);
massSlider.position(150, height + 10);
massSlider.style('width', '120px');
gravitySlider = createSlider(0.1, 20.0, gravity, 0.01);
gravitySlider.position(290, height + 10);
gravitySlider.style('width', '120px');
dampingSlider = createSlider(0.90, 1.00, damping, 0.0005);
dampingSlider.position(10, height + 40);
dampingSlider.style('width', '120px');
angleSlider = createSlider(-PI / 2, PI / 2, angle, 0.001);
angleSlider.position(150, height + 40);
angleSlider.style('width', '120px');
timeScaleSlider = createSlider(0.1, 3.0, timeScale, 0.01);
timeScaleSlider.position(290, height + 40);
timeScaleSlider.style('width', '120px');
// Buttons
resetBtn = createButton('Reset');
resetBtn.position(10, height + 75);
resetBtn.mousePressed(resetSim);
pauseBtn = createButton('Pause/Play');
pauseBtn.position(80, height + 75);
pauseBtn.mousePressed(togglePause);
presetBtn1 = createButton('Small Angle');
presetBtn1.position(180, height + 75);
presetBtn1.mousePressed(presetSmallAngle);
presetBtn2 = createButton('Large Angle');
presetBtn2.position(280, height + 75);
presetBtn2.mousePressed(presetLargeAngle);
frameRate(60);
}
function draw() {
background(245);
// Update parameters from sliders
length = lengthSlider.value();
mass = massSlider.value();
gravity = gravitySlider.value();
damping = dampingSlider.value();
timeScale = timeScaleSlider.value();
// If paused, do not update physics but allow angle slider to set angle directly
if (isPaused) {
angle = angleSlider.value();
angularVelocity = 0;
angularAcceleration = 0;
} else {
// If user moves angle slider while running, sync angle to slider for immediate effect
if (abs(angleSlider.value() - angle) > 0.001) {
angle = angleSlider.value();
angularVelocity = 0;
}
// Physics calculations with fixed timestep using deltaTime (seconds)
let dt = (deltaTime / 1000) * timeScale;
// Equation of motion: θ'' = -(g / L) * sin(θ) - damping * θ'
angularAcceleration = (-gravity / length) * sin(angle) - (1 - damping) * angularVelocity;
// Update angular velocity and angle
angularVelocity += angularAcceleration * dt;
angularVelocity *= damping; // damping applied here for stability
angle += angularVelocity * dt;
// Update angle slider to reflect current angle in real-time
angleSlider.value(angle);
}
// Draw pendulum origin
translate(width / 2, 80);
// Draw grid lines for reference
drawGrid();
// Calculate bob position
let px = length * 150 * sin(angle);
let py = length * 150 * cos(angle);
// Draw arm
stroke(50);
strokeWeight(3);
line(0, 0, px, py);
// Draw bob with mass-dependent size
let bobRadius = map(mass, 0.1, 5, 12, 40);
fill(70, 130, 180);
stroke(30, 80, 130);
strokeWeight(2);
ellipse(px, py, bobRadius * 2, bobRadius * 2);
// Draw pivot point
fill(30);
noStroke();
ellipse(0, 0, 12, 12);
// Draw velocity vector (tangential)
let vMag = angularVelocity * length;
let vScale = 50;
stroke(200, 50, 50);
strokeWeight(2);
push();
translate(px, py);
rotate(angle + HALF_PI);
line(0, 0, vMag * vScale, 0);
pop();
// Draw acceleration vector (tangential)
let aMag = angularAcceleration * length;
let aScale = 200;
stroke(50, 150, 50);
strokeWeight(2);
push();
translate(px, py);
rotate(angle + HALF_PI);
line(0, 0, aMag * aScale, 0);
pop();
// Draw trail (optional: simple fading trail)
drawTrail(px, py);
// Draw title
resetMatrix();
fill(0);
noStroke();
textAlign(CENTER, TOP);
textSize(20);
text('Pendulum Motion', width / 2, 10);
// Display key parameters neatly in corners
textSize(14);
textAlign(LEFT, TOP);
fill(0);
noStroke();
// Angular velocity in rad/s and m/s tangential velocity
let velocityTangential = angularVelocity * length;
text('Angle: ' + degrees(angle).toFixed(1) + '°', 10, 40);
text('Angular Vel: ' + angularVelocity.toFixed(3) + ' rad/s', 10, 60);
text('Velocity: ' + velocityTangential.toFixed(3) + ' m/s', 10, 80);
textAlign(RIGHT, TOP);
text('Length: ' + length.toFixed(2) + ' m', width - 10, 40);
text('Mass: ' + mass.toFixed(2) + ' kg', width - 10, 60);
text('Gravity: ' + gravity.toFixed(2) + ' m/s²', width - 10, 80);
}
let trail = [];
function drawTrail(x, y) {
trail.push(createVector(x, y));
if (trail.length > 100) trail.shift();
noFill();
stroke(70, 130, 180, 100);
strokeWeight(2);
beginShape();
for (let i = 0; i < trail.length; i++) {
let pos = trail[i];
vertex(pos.x, pos.y);
}
endShape();
}
function drawGrid() {
stroke(200);
strokeWeight(1);
for (let i = -width / 2; i <= width / 2; i += 50) {
line(i, 0, i, height - 160);
}
for (let j = 0; j <= height - 160; j += 50) {
line(-width / 2, j, width / 2, j);
}
}
function resetSim() {
angle = PI / 4;
angularVelocity = 0;
angularAcceleration = 0;
damping = 0.995;
timeScale = 1.0;
lengthSlider.value(length);
massSlider.value(mass);
gravitySlider.value(gravity);
dampingSlider.value(damping);
angleSlider.value(angle);
timeScaleSlider.value(timeScale);
trail = [];
isPaused = false;
}
function togglePause() {
isPaused = !isPaused;
}
function presetSmallAngle() {
angle = radians(10);
angularVelocity = 0;
angularAcceleration = 0;
angleSlider.value(angle);
isPaused = false;
}
function presetLargeAngle() {
angle = radians(75);
angularVelocity = 0;
angularAcceleration = 0;
angleSlider.value(angle);
isPaused = false;
}