Web Analytics
Program a physics simulation in p5.js – step by step

Program a physics simulation – guide

On this site you can play with ready-made simulations. In this guide we go one step further: you write the simulation yourself. Each lesson builds a simple version of one simulation in p5.js from scratch, explaining every line of code and the physics behind it. This way you learn physics and programming at the same time – and in the end you have a program you can change however you like.

What you need

The recipe for every simulation

Almost every motion simulation has the same structure. In p5.js the function setup() runs once, at the start, and draw() about 60 times per second. So:

  1. State – variables that describe the system at this moment: position, velocity, time… We set them at the start.
  2. Step – in each call of draw() time moves on by a small step dt, and we calculate the new velocity and position from a physical law.
  3. Drawing – we convert the state from metres to pixels (scale) and draw it, together with the numbers we want to watch.
function draw() {
  // 1. step: new velocity and new position from a physical law
  v = v + a * dt;
  x = x + v * dt;
  // 2. drawing: metres → pixels
  background(255);
  circle(x * SCALE, 100, 20);
}

This way of calculating is called Euler's method. It is simple and accurate enough for most school examples; in later lessons you will see when something better is needed.

Lessons

#lessonwhat you learnready-made simulation
1Free fall state, time step dt, Euler's method, scale metres → pixels, comparison with the formulafree fall
2Projectile motionmotion in two dimensions, velocity components, the trail of the path, the keyboardprojectile motion
3Projectile with air resistancea force that depends on velocity, objects, one function for two bodiesprojectile with air resistance
4Mass and springoscillations, plain Euler and Euler–Cromer, checking the energymass and spring
5Simple pendulumangle as the state, measuring the period, when the formula stops workingsimple pendulum
6Collisionsdetecting a collision, conservation of momentum, coefficient of restitutionmomentum and collisions
7Kepler orbitsgravity as a vector, convenient units, checking Kepler's third lawKepler's laws
8Wavessine in space and time, wave speed, superposition and standing waveswaves and sound
9Refraction of lightthe angle from the mouse position, Snell's law, total internal reflectiongeometrical optics

The same in Java and Python

The same recipe works in any language. On svetprogramiranja.com there are lessons in Processing (Java and Python), for example projectile motion in Processing and the whole Processing programming guide.

For teachers

The lessons are designed so that one fits into one lesson period: the first 10 minutes for the ready-made simulation and the physics, then writing the program together step by step in the p5.js editor, and finally independent tasks (changing the program). They suit computing, physics and project-based lessons.