All variables should be global (use let/var at top level)
Use standard p5.js function names: createCanvas, background, fill, etc.
NO sketch wrapper function - write direct p5.js code
SIMULATION ACCURACY FOR PROJECTILE PHYSICS:
Use correct physics equations, mathematical relationships, or algorithmic principles
Realistic parameter ranges and default values that make physical sense
Smooth, continuous animations with proper time scaling
Proper scaling and units where applicable
Include scientific constants or realistic coefficients
ENSURE every parameter controlled by sliders affects the simulation visibly
Use appropriate time steps and scaling factors to avoid too-fast animations
TEXT AND DISPLAY REQUIREMENTS:
Show ONLY the simulation title at the top
Display ONLY key physics parameters (velocity, acceleration, force, etc.) - no instructions or explanations
Keep text minimal and clean - maximum 4-5 parameter displays
NO instruction text like "Click to...", "Drag to...", etc.
Use concise parameter labels like "Velocity: 5.2 m/s" not "Current velocity value is 5.2"
Position parameter text neatly in corners or edges, not cluttering the center
VISUAL REQUIREMENTS:
Clear, educational visualization of the concept
Multiple visual elements (trails, vectors, measurements, etc.) that don't conflict
Color coding to distinguish different aspects
Grid lines, axes, or reference markers where helpful
Immediate and obvious visual response to parameter changes
Smooth, realistic motion that matches real-world physics
Proper visual hierarchy - important elements should stand out
Clean, uncluttered display with minimal text
EXAMPLE OF FUNCTIONAL CONTROLS WITH PROPER TIMING:
javascript
// Global variables that ACTUALLY affect simulation
let gravitySlider, dampingSlider;
let gravity = 0.5; // This gets updated by slider
let damping = 0.99; // This gets updated by slider
let resetBtn, pauseBtn;
let isPaused = false;
let timeScale = 0.02; // Control animation speed
function setup() {
createCanvas(400, 400);
// Create sliders that CONTROL the simulation with realistic ranges
gravitySlider = createSlider(0.1, 1.0, 0.4, 0.05);
gravitySlider.position(10, height + 10);
dampingSlider = createSlider(0.9, 1.0, 0.995, 0.001);
dampingSlider.position(10, height + 40);
// Create buttons that DO something - positioned to avoid overlap
resetBtn = createButton('Reset');
resetBtn.position(10, height + 40);
pauseBtn = createButton('Pause/Play');
pauseBtn.position(80, height + 40);
}
function draw() {
background(220);
// ACTUALLY USE the slider values - this is critical!
gravity = gravitySlider.value();
damping = dampingSlider.value();
if (!isPaused) {
// Use gravity and damping in actual physics calculations with proper time scaling
velocity += gravity * timeScale;
velocity *= damping;
position += velocity;
}
// Show ONLY title and key parameters - keep it minimal
fill(0);
textAlign(CENTER);
text('Projectile Physics', width/2, 20);
textAlign(LEFT);
text('Gravity: ' + gravity.toFixed(2), 10, 40);
text('Velocity: ' + velocity.toFixed(2), 10, 60);
// Draw simulation that responds to parameters with realistic motion
ellipse(position, height/2, 20, 20);
}
// Functions that actually work
function resetSimulation() {
position = width/2;
velocity = 0;
}
function togglePause() {
isPaused = !isPaused;
}
IMPORTANT:
Focus on the specific physics, mathematics, or principles of projectile physics
Make controls that ACTUALLY matter for understanding the concept
Every slider must control a variable used in the simulation
Every button must perform a visible action
Ensure the simulation behaves realistically and responds to all controls
Use proper time scaling to avoid unrealistically fast animations
Position controls carefully to avoid visual conflicts
Make animations smooth and scientifically accurate
Keep text displays MINIMAL - only title and key parameters
NO instruction text or explanations in the simulation
Only output JavaScript code, no explanations
Use global mode p5.js format (no sketch wrapper)
Position controls below the 400x400 canvas with proper spacing
Generate an accurate, interactive simulation of: projectile physics with WORKING controls, REALISTIC timing, and MINIMAL text displays
javascript
// Global variables controlling simulation parameters and state
let canvasWidth = 600;
let canvasHeight = 400;
let angleSlider, velocitySlider, gravitySlider, airResSlider, massSlider, radiusSlider;
let resetBtn, pauseBtn, presetBtn1, presetBtn2;
let angle = 45; // degrees
let velocity = 20; // m/s
let gravity = 9.81; // m/s^2
let airResistance = 0.02; // drag coefficient (dimensionless, small)
let mass = 1.0; // kg
let radius = 10; // pixels for drawing projectile size
let isPaused = false;
let t = 0; // simulation time in seconds
let dt = 0; // delta time in seconds (from deltaTime)
let pos, vel, acc; // vectors for position, velocity, acceleration
let groundLevel; // y position of ground in pixels
let trail = []; // store past positions for trail visualization
function setup() {
createCanvas(canvasWidth, canvasHeight);
frameRate(60);
// Initialize ground level (bottom margin)
groundLevel = height - 40;
// Create sliders with realistic ranges and default values
angleSlider = createSlider(0, 90, 45, 1);
angleSlider.position(10, height + 10);
angleSlider.style('width', '120px');
velocitySlider = createSlider(1, 50, 20, 0.1);
velocitySlider.position(150, height + 10);
velocitySlider.style('width', '120px');
gravitySlider = createSlider(0.1, 20, 9.81, 0.01);
gravitySlider.position(290, height + 10);
gravitySlider.style('width', '120px');
airResSlider = createSlider(0, 0.1, 0.02, 0.001);
airResSlider.position(430, height + 10);
airResSlider.style('width', '120px');
massSlider = createSlider(0.1, 10, 1, 0.1);
massSlider.position(10, height + 50);
massSlider.style('width', '120px');
radiusSlider = createSlider(5, 30, 10, 1);
radiusSlider.position(150, height + 50);
radiusSlider.style('width', '120px');
// Buttons
resetBtn = createButton('Reset');
resetBtn.position(290, height + 50);
resetBtn.mousePressed(resetSim);
pauseBtn = createButton('Pause/Play');
pauseBtn.position(360, height + 50);
pauseBtn.mousePressed(togglePause);
presetBtn1 = createButton('Preset: No Air');
presetBtn1.position(460, height + 50);
presetBtn1.mousePressed(presetNoAir);
presetBtn2 = createButton('Preset: High Drag');
presetBtn2.position(560, height + 50);
presetBtn2.mousePressed(presetHighDrag);
resetSim();
}
function draw() {
background(245);
// Update parameters from sliders (CRITICAL: use slider.value())
angle = angleSlider.value();
velocity = velocitySlider.value();
gravity = gravitySlider.value();
airResistance = airResSlider.value();
mass = massSlider.value();
radius = radiusSlider.value();
// Draw title
fill(30);
noStroke();
textAlign(CENTER, TOP);
textSize(20);
text('Projectile Physics', width / 2, 10);
// Draw ground line
stroke(100);
strokeWeight(1);
line(0, groundLevel, width, groundLevel);
// Draw grid lines (every 50 pixels vertically and horizontally)
stroke(220);
strokeWeight(1);
for (let x = 0; x <= width; x += 50) {
line(x, 0, x, groundLevel);
}
for (let y = 0; y <= groundLevel; y += 50) {
line(0, y, width, y);
}
// Draw axes labels (meters)
noStroke();
fill(120);
textSize(10);
for (let x = 0; x <= width; x += 50) {
let meters = (x) / 10; // scale: 10 pixels = 1 meter
text(meters.toFixed(0) + 'm', x + 2, groundLevel + 12);
}
for (let y = groundLevel; y >= 0; y -= 50) {
let meters = (groundLevel - y) / 10;
text(meters.toFixed(0) + 'm', 2, y - 2);
}
// Time step in seconds, capped for stability
dt = min(deltaTime / 1000, 0.033);
if (!isPaused) {
// Update physics with air resistance and gravity
// Drag force: Fd = -0.5 * Cd * A * rho * v^2 * v_unit
// Simplified drag coefficient * velocity squared * direction
// Here airResistance slider acts as combined drag coefficient term
// Calculate drag acceleration vector
let speed = vel.mag();
if (speed > 0) {
let dragMag = airResistance * speed * speed / mass; // a = F/m
let drag = vel.copy().mult(-1).normalize().mult(dragMag);
acc = createVector(0, gravity).add(drag);
} else {
acc = createVector(0, gravity);
}
// Euler integration for velocity and position
vel.add(p5.Vector.mult(acc, dt));
pos.add(p5.Vector.mult(vel, dt));
// Store trail positions (limit length)
trail.push(pos.copy());
if (trail.length > 200) trail.shift();
// Stop simulation if projectile hits ground
if (pos.y >= groundLevel - radius) {
pos.y = groundLevel - radius;
vel.set(0, 0);
acc.set(0, 0);
isPaused = true;
}
t += dt;
}
// Draw trail
noFill();
stroke(0, 150, 255, 150);
strokeWeight(2);
beginShape();
for (let p of trail) {
vertex(p.x, p.y);
}
endShape();
// Draw projectile
noStroke();
fill(255, 50, 50);
ellipse(pos.x, pos.y, radius * 2);
// Draw velocity vector (blue)
drawVector(vel, pos, color(0, 100, 255), 50);
// Draw acceleration vector (green)
drawVector(acc, pos, color(0, 180, 0), 200);
// Draw initial velocity vector at launch point (gray)
let initVel = p5.Vector.fromAngle(radians(angle), velocity * 10);
drawVector(initVel, createVector(50, groundLevel), color(100), 1);
// Display parameters (minimal, neat)
noStroke();
fill(0);
textSize(14);
textAlign(LEFT, TOP);
text('Angle: ' + angle.toFixed(1) + '°', 10, 35);
text('Velocity: ' + velocity.toFixed(2) + ' m/s', 10, 55);
text('Gravity: ' + gravity.toFixed(2) + ' m/s²', 10, 75);
text('Air Drag: ' + airResistance.toFixed(3), 10, 95);
text('Mass: ' + mass.toFixed(2) + ' kg', width - 140, 35);
text('Radius: ' + radius.toFixed(0) + ' px', width - 140, 55);
text('Time: ' + t.toFixed(2) + ' s', width - 140, 75);
// Draw launch point marker
fill(0);
ellipse(50, groundLevel, 6);
// Draw ground label
fill(80);
textAlign(CENTER, BOTTOM);
text('Ground', width / 2, height - 5);
}
// Draw vector from base position with scaling and color
function drawVector(v, base, col, scale) {
push();
stroke(col);
strokeWeight(3);
fill(col);
translate(base.x, base.y);
line(0, 0, v.x * scale, v.y * scale);
// Arrowhead
push();
translate(v.x * scale, v.y * scale);
rotate(v.heading());
let arrowSize = 7;
triangle(0, 0, -arrowSize, arrowSize / 2, -arrowSize, -arrowSize / 2);
pop();
pop();
}
// Reset simulation to initial state using current slider values
function resetSim() {
t = 0;
isPaused = false;
let rad = radians(angleSlider.value());
let v0 = velocitySlider.value();
// Position at launch point (50 px from left, ground level)
pos = createVector(50, groundLevel - radiusSlider.value());
// Initial velocity vector
vel = createVector(cos(rad) * v0, -sin(rad) * v0);
acc = createVector(0, gravitySlider.value());
trail = [];
}
// Toggle pause/play state
function togglePause() {
isPaused = !isPaused;
}
// Preset: No air resistance, standard gravity, medium velocity and angle
function presetNoAir() {
angleSlider.value(45);
velocitySlider.value(25);
gravitySlider.value(9.81);
airResSlider.value(0);
massSlider.value(1);
radiusSlider.value(10);
resetSim();
}
// Preset: High air resistance, low velocity, low angle
function presetHighDrag() {
angleSlider.value(30);
velocitySlider.value(15);
gravitySlider.value(9.81);
airResSlider.value(0.08);
massSlider.value(1);
radiusSlider.value(15);
resetSim();
}