In this lesson we write the first simulation of the guide Program a physics simulation: a body released from a height of 20 m falls under gravity. The program tracks time, height and speed, and when the body lands it compares the fall time with the time from the formula. This is the finished program (click the canvas to drop the body again):
Physics: free fall – motion under gravity alone, with acceleration g = 9.81 m/s².
Programming: the functions setup() and draw(), state variables, the time step dt, Euler's method, scale metres → pixels.
Time: one lesson period.
Open editor.p5js.org. Every p5.js program has two main functions: setup() runs
once, at the start (this is where we create the canvas), and draw() runs about 60 times per second, like the frames of a cartoon. If we move the body
a little in every frame, we get motion.
We create a canvas of 320 × 380 pixels and draw a circle. For now it stays where it is.
function setup() {
createCanvas(320, 380);
}
function draw() {
background(248, 250, 252);
fill(249, 115, 22);
circle(160, 50, 24);
}
In p5.js the y coordinate grows downwards: the point (0, 0) is the top left corner of the canvas.
The simulation calculates in metres and seconds but draws in pixels. So we introduce constants and a scale – how many pixels make one metre:
const g = 9.81; // acceleration due to gravity, m/s²
const H = 20; // initial height, m
const SCALE = 15; // how many pixels make one metre
const dt = 1 / 60; // time step, s (60 frames per second)
let y, v, t, falling;
The variables y (height above the ground), v (speed) and t (time) together make up the state of the system. The initial state is set by the function
start(), which we call from setup() – and later whenever we want to drop the body again.
function start() {
y = H; // height above the ground, m
v = 0; // speed, m/s (positive downwards)
t = 0; // time, s
falling = true;
}
A height of 20 m takes 20 · 15 = 300 pixels on the canvas – enough to fit into a canvas 380 pixels high.
This is the heart of every simulation. In each frame a time dt = 1/60 s passes. During that time the speed increases by g · dt, and the height decreases by v · dt:
v = v + g * dt; // new speed
y = y - v * dt; // new height
t = t + dt;
This is exactly the definition of acceleration (change of velocity over time) and of velocity (change of position over time), just for a small time step. This procedure is called Euler's method (after the mathematician Leonhard Euler). We did not use the formula h = H − g · t² / 2 – the program “discovered” the motion by itself from the law for acceleration.
When the height becomes zero or less, the body has landed: we set y = 0 and stop calculating. We draw the body by multiplying its height in metres by the scale and subtracting it from the position of the ground (because y on the canvas grows downwards):
if (y <= 0) { // the body has hit the ground
y = 0;
falling = false;
}
// ...
circle(width / 2, groundY - y * SCALE - 12, 24);
The function text() prints the time, height and speed, and toFixed(2) rounds a number to two decimal places.
When the body lands, the program also prints the fall time from the formula t = √(2H / g) = √(40 / 9.81) ≈ 2.02 s. The simulation gives the same: t = 2.02 s. The impact speed in the simulation is 19.78 m/s, and by the formula v = √(2gH) ≈ 19.81 m/s. The small difference comes from the step dt: the computer does not follow the motion continuously, but in jumps of 1/60 s. The smaller the step, the more accurate the result (try problem 4).
Finally, the function mousePressed() calls start() when you click the canvas, so the body falls again.
Copy it into the p5.js editor and click ▶. It is the same program that runs at the top of the page (in the editor delete the line canvas.parent('lesson-canvas'); – it only places the canvas on this page).
// Free fall – program from the lesson “Program a physics simulation” (simulacije.svetprogramiranja.com)
const g = 9.81; // acceleration due to gravity, m/s²
const H = 20; // initial height, m
const SCALE = 15; // how many pixels make one metre
const dt = 1 / 60; // time step, s (60 frames per second)
let y, v, t, falling;
function setup() {
const canvas = createCanvas(320, 380);
canvas.parent('lesson-canvas');
start();
}
function start() {
y = H; // height above the ground, m
v = 0; // speed, m/s (positive downwards)
t = 0; // time, s
falling = true;
}
function draw() {
background(248, 250, 252);
if (falling) {
v = v + g * dt; // new speed
y = y - v * dt; // new height
t = t + dt;
if (y <= 0) { // the body has hit the ground
y = 0;
falling = false;
}
}
// ground
const groundY = height - 30;
stroke(51, 65, 85);
strokeWeight(3);
line(0, groundY, width, groundY);
// body: height in metres → pixels
noStroke();
fill(249, 115, 22);
circle(width / 2, groundY - y * SCALE - 12, 24);
// numbers
fill(11, 19, 36);
textSize(14);
text('t = ' + t.toFixed(2) + ' s', 10, 22);
text('h = ' + y.toFixed(2) + ' m', 10, 42);
text('v = ' + v.toFixed(2) + ' m/s', 10, 62);
if (!falling) {
const tFormula = Math.sqrt(2 * H / g);
text('formula: t = ' + tFormula.toFixed(2) + ' s', 10, 82);
text('click to drop again', 10, 102);
}
}
function mousePressed() {
if (mouseX >= 0 && mouseX <= width && mouseY >= 0 && mouseY <= height) {
start();
}
}
H = 45 and a smaller scale, e.g. SCALE = 7 (45 · 7 = 315 pixels). The fall takes 3.03 s – the same as √(2 · 45 / 9.81).
Change it to g = 1.62. A fall from 20 m takes about 4.97 s – almost 2.5 times longer.
Instead of stopping, reverse the velocity (positive speed is downwards, so after the bounce it becomes negative):
if (y <= 0) {
y = 0;
v = -0.8 * v; // bounce: the velocity changes direction and gets smaller
}dt = 1 / 600, and so that the body still falls at its real speed, repeat the step 10 times in every frame (a for loop). What is the impact speed now?
The speed is now 19.82 m/s – the difference from the formula (19.81 m/s) is only 0.01 m/s instead of 0.03 m/s. A smaller step – a more accurate simulation, but more calculation.