Create a complete, highly interactive p5.js simulation demonstrating: projectile motion with example
CRITICAL REQUIREMENTS:
ACCURACY: The simulation must be scientifically/mathematically accurate for projectile motion with example
FUNCTIONAL INTERACTIVITY: All sliders and buttons MUST actually control and modify the simulation behavior in real-time
EDUCATIONAL: Users should learn about projectile motion with example through experimentation and parameter manipulation
VISUAL PRECISION: Animations should be smooth, realistic, and visually accurate to real-world physics
MANDATORY INTERACTIVE ELEMENTS THAT MUST WORK:
At least 4-6 sliders controlling key parameters with realistic ranges - these MUST change simulation behavior
At least 3-4 buttons for actions (reset, pause/play, presets, etc.) - these MUST perform actual functions
Real-time parameter display showing current values that update as sliders move
Visual feedback that responds immediately and visibly to control changes
Mouse interactions where physically meaningful
CRITICAL: CONTROLS MUST BE FUNCTIONAL, NOT DECORATIVE
Every slider must control a variable that actively affects the simulation
Every button must perform a meaningful action that changes the simulation state
Parameter changes must be immediately visible in the simulation output
Use the actual slider values in mathematical calculations and physics equations
ANIMATION AND TIMING REQUIREMENTS:
Use realistic time scaling - avoid animations that are too fast or unrealistic
Implement proper frame rate control using deltaTime or fixed timesteps
Use realistic physical constants and scaling factors
Ensure smooth, stable animations at 60fps
Avoid conflicting visual elements or overlapping controls
Make animations visually appealing and scientifically accurate
CONTROL IMPLEMENTATION REQUIREMENTS:
Sliders: Use createSlider(min, max, default, step) with appropriate ranges that make physical sense
Buttons: Use createButton('Label') with meaningful functions that actually execute
Position controls below canvas using .position(x, y) - ensure no overlap
Show parameter values with text() in the main canvas that update in real-time
Use function declarations for event handlers: function resetSim() {}
ALWAYS use slider.value() in your calculations - never ignore slider inputs
CODE FORMAT REQUIREMENTS:
Use GLOBAL MODE p5.js (NOT instance mode)
Assistant
Create a complete, highly interactive p5.js simulation… · The Chat Archive
Define setup() and draw() as regular functions
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 MOTION WITH EXAMPLE:
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 Motion With Example', 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 motion with example
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 motion with example with WORKING controls, REALISTIC timing, and MINIMAL text displays
javascript
// Global variables controlling simulation
let angleSlider, speedSlider, gravitySlider, airResSlider, massSlider, timeScaleSlider;
let resetBtn, pauseBtn, presetBtn1, presetBtn2;
let isPaused = false;
let timeScale = 1;
let launchAngle = 45; // degrees
let launchSpeed = 20; // m/s
let gravity = 9.81; // m/s²
let airResistance = 0; // coefficient (0 to 0.1)
let mass = 1; // kg
// Simulation state
let pos, vel, acc;
let simTime = 0;
let trail = [];
const canvasWidth = 600;
const canvasHeight = 400;
const groundLevel = canvasHeight - 50; // y position of ground in pixels
// Scaling factors for meters to pixels
const metersToPixels = 10; // 1 meter = 10 pixels
function setup() {
createCanvas(canvasWidth, canvasHeight);
// Sliders (min, max, default, step)
angleSlider = createSlider(0, 90, 45, 1);
angleSlider.position(10, height + 10);
speedSlider = createSlider(1, 50, 20, 0.1);
speedSlider.position(10, height + 40);
gravitySlider = createSlider(1, 20, 9.81, 0.1);
gravitySlider.position(10, height + 70);
airResSlider = createSlider(0, 0.1, 0, 0.001);
airResSlider.position(10, height + 100);
massSlider = createSlider(0.1, 10, 1, 0.1);
massSlider.position(10, height + 130);
timeScaleSlider = createSlider(0.1, 3, 1, 0.01);
timeScaleSlider.position(10, height + 160);
// Buttons
resetBtn = createButton('Reset');
resetBtn.position(250, height + 10);
resetBtn.mousePressed(resetSim);
pauseBtn = createButton('Pause/Play');
pauseBtn.position(310, height + 10);
pauseBtn.mousePressed(togglePause);
presetBtn1 = createButton('Preset: No Air');
presetBtn1.position(250, height + 40);
presetBtn1.mousePressed(() => {
angleSlider.value(45);
speedSlider.value(25);
gravitySlider.value(9.81);
airResSlider.value(0);
massSlider.value(1);
timeScaleSlider.value(1);
resetSim();
});
presetBtn2 = createButton('Preset: Air Resistance');
presetBtn2.position(360, height + 40);
presetBtn2.mousePressed(() => {
angleSlider.value(60);
speedSlider.value(30);
gravitySlider.value(9.81);
airResSlider.value(0.05);
massSlider.value(2);
timeScaleSlider.value(1);
resetSim();
});
resetSim();
frameRate(60);
}
function draw() {
background(245);
// Update parameters from sliders
launchAngle = angleSlider.value();
launchSpeed = speedSlider.value();
gravity = gravitySlider.value();
airResistance = airResSlider.value();
mass = massSlider.value();
timeScale = timeScaleSlider.value();
// Draw ground
stroke(100);
strokeWeight(1);
line(0, groundLevel, width, groundLevel);
// Draw grid lines (every 5 meters)
stroke(220);
for (let x = 0; x <= width; x += 5 * metersToPixels) {
line(x, 0, x, groundLevel);
}
for (let y = groundLevel; y >= 0; y -= 5 * metersToPixels) {
line(0, y, width, y);
}
// Title
noStroke();
fill(30);
textAlign(CENTER, TOP);
textSize(18);
text('Projectile Motion With Example', width / 2, 10);
// Parameter displays top-left corner
textAlign(LEFT, TOP);
textSize(14);
fill(0);
text('Angle: ' + launchAngle.toFixed(0) + '°', 10, 30);
text('Speed: ' + launchSpeed.toFixed(1) + ' m/s', 10, 50);
text('Gravity: ' + gravity.toFixed(2) + ' m/s²', 10, 70);
text('Air Resist: ' + airResistance.toFixed(3), 10, 90);
text('Mass: ' + mass.toFixed(1) + ' kg', 10, 110);
text('Time Scale: ' + timeScale.toFixed(2) + 'x', 10, 130);
if (!isPaused) {
// Physics timestep (seconds)
let dt = (deltaTime / 1000) * timeScale;
// Calculate acceleration with gravity and air resistance
// Air resistance force: Fd = -k * v * |v| (quadratic drag approx)
// acceleration = (gravity force + air resistance) / mass
let vMag = vel.mag();
let dragForce = p5.Vector.mult(vel.copy().normalize().mult(-1), airResistance * vMag * vMag);
let gravityForce = createVector(0, gravity * mass);
let netForce = p5.Vector.add(gravityForce, dragForce);
acc = p5.Vector.div(netForce, mass);
// Update velocity and position
vel.add(p5.Vector.mult(acc, dt));
pos.add(p5.Vector.mult(vel, dt));
simTime += dt;
// Add to trail
trail.push(pos.copy());
// Stop simulation if projectile hits ground
if (pos.y >= groundLevel / metersToPixels) {
pos.y = groundLevel / metersToPixels;
vel.set(0, 0);
acc.set(0, 0);
isPaused = true;
}
}
// Draw trail
noFill();
stroke(0, 100, 255, 150);
strokeWeight(2);
beginShape();
for (let p of trail) {
vertex(p.x * metersToPixels, groundLevel - p.y * metersToPixels);
}
endShape();
// Draw projectile
fill(255, 50, 50);
stroke(150, 0, 0);
strokeWeight(1.5);
ellipse(pos.x * metersToPixels, groundLevel - pos.y * metersToPixels, 16, 16);
// Draw velocity vector
drawVector(vel, pos, color(0, 150, 0), 50, 'Velocity');
// Draw acceleration vector
drawVector(acc, pos, color(200, 0, 200), 100, 'Acceleration');
// Draw launch vector at start position
if (simTime < 0.05) {
let launchVec = p5.Vector.fromAngle(radians(launchAngle), launchSpeed);
drawVector(launchVec, createVector(0, 0), color(0, 0, 0), 50, 'Launch');
}
}
// Draw vector as arrow from position p (in meters), scaled for display
function drawVector(v, p, col, scale, label) {
push();
translate(p.x * metersToPixels, groundLevel - p.y * metersToPixels);
stroke(col);
fill(col);
strokeWeight(2);
let vecScreen = v.copy().mult(scale / v.mag());
line(0, 0, vecScreen.x, -vecScreen.y);
// Arrowhead
push();
translate(vecScreen.x, -vecScreen.y);
rotate(-atan2(vecScreen.y, vecScreen.x));
triangle(0, 0, -6, 3, -6, -3);
pop();
noStroke();
textSize(12);
textAlign(LEFT, BOTTOM);
text(label, vecScreen.x + 5, -vecScreen.y - 5);
pop();
}
// Reset simulation state
function resetSim() {
simTime = 0;
trail = [];
let angleRad = radians(launchAngle);
pos = createVector(0, 0);
vel = createVector(launchSpeed * cos(angleRad), launchSpeed * sin(angleRad));
acc = createVector(0, 0);
isPaused = false;
}
// Pause/play toggle
function togglePause() {
isPaused = !isPaused;
}
// Mouse interaction: click on canvas to set launch angle and speed
function mousePressed() {
if (
mouseX >= 0 &&
mouseX <= width &&
mouseY >= 0 &&
mouseY <= height
) {
// Vector from origin (bottom-left) to mouse position
let dx = (mouseX) / metersToPixels;
let dy = (groundLevel - mouseY) / metersToPixels;
if (dx > 0 && dy >= 0) {
let angleDeg = degrees(atan2(dy, dx));
let speedVal = sqrt(dx * dx + dy * dy) * 2; // scale speed by distance clicked
angleSlider.value(constrain(angleDeg, 0, 90));
speedSlider.value(constrain(speedVal, 1, 50));
resetSim();
}
}
}