// Pendulum simulation with accurate physics and interactive controls
// Pendulum physical parameters
let lengthSlider, massSlider, gravitySlider, dampingSlider, angleSlider, timeScaleSlider;
let resetBtn, pauseBtn, presetBtn1, presetBtn2;
let length; // Length of pendulum (m)
let mass; // Mass of bob (kg)
let gravity; // Gravity acceleration (m/s^2)
let damping; // Damping coefficient (dimensionless, <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
let originX, originY; // Pivot point coordinates in pixels
let bobX, bobY; // Bob position in pixels
let isPaused = false;
const PIXELS_PER_METER = 150; // Scale: 1 meter = 150 pixels
// Trail for bob path
let trail = [];
function setup() {
createCanvas(400, 400);
frameRate(60);
originX = width / 2;
originY = 80;
// Sliders with realistic ranges and defaults
lengthSlider = createSlider(0.2, 2.0, 1.0, 0.01); // length in meters
lengthSlider.position(10, height + 10);
lengthSlider.style('width', '120px');
massSlider = createSlider(0.1, 5.0, 1.0, 0.1); // mass in kg
massSlider.position(150, height + 10);
massSlider.style('width', '120px');
gravitySlider = createSlider(1.0, 20.0, 9.81, 0.1); // gravity m/s^2
gravitySlider.position(290, height + 10);
gravitySlider.style('width', '120px');
dampingSlider = createSlider(0.90, 1.00, 0.995, 0.001); // damping factor
dampingSlider.position(10, height + 40);
dampingSlider.style('width', '120px');
angleSlider = createSlider(-PI / 2, PI / 2, PI / 4, 0.01); // initial angle radians
angleSlider.position(150, height + 40);
angleSlider.style('width', '120px');
timeScaleSlider = createSlider(0.1, 3.0, 1.0, 0.01); // time scale multiplier
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('Preset: Earth');
presetBtn1.position(180, height + 75);
presetBtn1.mousePressed(() => {
gravitySlider.value(9.81);
lengthSlider.value(1.0);
massSlider.value(1.0);
dampingSlider.value(0.995);
angleSlider.value(PI / 4);
timeScaleSlider.value(1.0);
resetSim();
});
presetBtn2 = createButton('Preset: Moon');
presetBtn2.position(280, height + 75);
presetBtn2.mousePressed(() => {
gravitySlider.value(1.62);
lengthSlider.value(1.0);
massSlider.value(1.0);
dampingSlider.value(0.995);
angleSlider.value(PI / 4);
timeScaleSlider.value(1.0);
resetSim();
});
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 angleSlider if paused or reset, else keep physics controlling angle
if (isPaused) {
angle = angleSlider.value();
angularVelocity = 0;
} else {
// Physics integration with fixed timestep scaled by timeScale and deltaTime
let dt = (deltaTime / 1000) * timeScale; // deltaTime in seconds scaled
// Equation of motion for simple pendulum with damping:
// angularAcceleration = -(g / L) * sin(angle) - damping * angularVelocity
// Damping here is a multiplicative factor on velocity (close to 1)
angularAcceleration = (-gravity / length) * sin(angle);
angularVelocity += angularAcceleration * dt;
angularVelocity *= damping;
angle += angularVelocity * dt;
// Update angle slider to reflect current angle for visual sync
angleSlider.value(angle);
}
// Calculate bob position in pixels
bobX = originX + length * PIXELS_PER_METER * sin(angle);
bobY = originY + length * PIXELS_PER_METER * cos(angle);
// Add current bob position to trail
trail.push({ x: bobX, y: bobY });
if (trail.length > 150) trail.shift();
// Draw grid and axes
drawGrid();
// Draw pivot
stroke(0);
strokeWeight(3);
fill(50);
ellipse(originX, originY, 12, 12);
// Draw rod
stroke(30, 100, 200);
strokeWeight(4);
line(originX, originY, bobX, bobY);
// Draw bob with mass-dependent size
let bobRadius = map(mass, 0.1, 5, 10, 30);
fill(200, 50, 50);
stroke(150, 0, 0);
strokeWeight(2);
ellipse(bobX, bobY, bobRadius * 2, bobRadius * 2);
// Draw trail
noFill();
stroke(200, 50, 50, 100);
strokeWeight(2);
beginShape();
for (let p of trail) {
vertex(p.x, p.y);
}
endShape();
// Draw vectors: velocity and acceleration at bob
drawVectors();
// Display minimal text: title and key parameters
fill(30);
noStroke();
textAlign(CENTER, CENTER);
textSize(20);
text('Pendulum', width / 2, 30);
textSize(14);
textAlign(LEFT, TOP);
// Angular velocity in rad/s and linear velocity in m/s
let linearVelocity = angularVelocity * length; // v = ω * L
let linearAcceleration = angularAcceleration * length; // a = α * L
let tension = mass * (gravity * cos(angle) + angularVelocity * angularVelocity * length); // Tension force approx
text(`Length: ${length.toFixed(2)} m`, 10, 50);
text(`Mass: ${mass.toFixed(2)} kg`, 10, 70);
text(`Gravity: ${gravity.toFixed(2)} m/s²`, 10, 90);
text(`Velocity: ${linearVelocity.toFixed(2)} m/s`, 10, 110);
text(`Acceleration: ${linearAcceleration.toFixed(2)} m/s²`, 10, 130);
text(`Tension: ${tension.toFixed(2)} N`, 10, 150);
}
// Draw background grid and reference lines
function drawGrid() {
stroke(200);
strokeWeight(1);
// Horizontal lines every 50 pixels
for (let y = 50; y < height; y += 50) {
line(0, y, width, y);
}
// Vertical lines every 50 pixels
for (let x = 0; x < width; x += 50) {
line(x, 0, x, height);
}
// Draw pivot vertical reference line
stroke(150, 150, 255, 100);
strokeWeight(2);
line(originX, originY, originX, height);
}
// Draw velocity and acceleration vectors at bob
function drawVectors() {
// Velocity vector (tangential)
let vLen = angularVelocity * length * PIXELS_PER_METER;
let vScale = 0.3; // scale for visibility
let vx = vScale * vLen * cos(angle);
let vy = -vScale * vLen * sin(angle);
stroke(0, 150, 0);
strokeWeight(3);
fill(0, 150, 0, 150);
drawArrow(bobX, bobY, bobX + vx, bobY + vy);
// Acceleration vector (tangential + radial)
// Tangential acceleration = angularAcceleration * length
// Radial acceleration = ω² * length towards pivot
let at = angularAcceleration * length * PIXELS_PER_METER;
let ar = angularVelocity * angularVelocity * length * PIXELS_PER_METER;
// Tangential acceleration vector perpendicular to rod (direction of motion)
let atx = at * cos(angle + HALF_PI);
let aty = at * sin(angle + HALF_PI);
// Radial acceleration vector towards pivot
let arx = -ar * sin(angle);
let ary = ar * cos(angle);
// Total acceleration vector
let ax = atx + arx;
let ay = aty + ary;
let aScale = 0.3;
stroke(200, 50, 200);
strokeWeight(3);
fill(200, 50, 200, 150);
drawArrow(bobX, bobY, bobX + aScale * ax, bobY + aScale * ay);
}
// Draw arrow helper
function drawArrow(x1, y1, x2, y2) {
push();
strokeWeight(3);
fill(strokeColor());
line(x1, y1, x2, y2);
let angle = atan2(y2 - y1, x2 - x1);
translate(x2, y2);
rotate(angle);
let size = 7;
triangle(0, 0, -size, size / 2, -size, -size / 2);
pop();
}
// Return current stroke color for fill in drawArrow
function strokeColor() {
let c = get();
return color(c[0], c[1], c[2], c[3]);
}
// Reset simulation to initial conditions from sliders
function resetSim() {
angle = angleSlider.value();
angularVelocity = 0;
angularAcceleration = 0;
trail = [];
isPaused = false;
}
// Toggle pause/play state
function togglePause() {
isPaused = !isPaused;
if (isPaused) {
angularVelocity = 0;
angularAcceleration = 0;
}
}
// Mouse interaction: drag bob to set angle when paused
function mousePressed() {
if (isPaused) {
let dx = mouseX - originX;
let dy = mouseY - originY;
let distToBob = dist(mouseX, mouseY, bobX, bobY);
if (distToBob < 30) {
// Start dragging
dragging = true;
}
}
}
function mouseDragged() {
if (isPaused && dragging) {
let dx = mouseX - originX;
let dy = mouseY - originY;
angle = atan2(dx, dy);
angleSlider.value(angle);
}
}
function mouseReleased() {
dragging = false;
}
let dragging = false;