Falling in p5.js
Playing with vectors, motion and gravity
In his book, The Nature of Code, Dan Shiffman adopts a nearly dimensionless approach to mechanics that simplifies coding but divorces the simulations from real-world units. In particular, there are no units of time, and the implicit unit of length is the pixel. So, for example, if his code is tracking a moving object: newX = oldx + velocity, each time through the draw loop. And velocity is updated by adding acceleration. The implication is that one frame equals one unit of time. I will make things more explicit, by defining dt = 1/fps. So for 30 frames per second, dt = 1/30 sec. I will also set a length scale, for example specifying that 10 pixels = 1 m. Shiffman also neglects to average the velocity over the time step when calculating the distance--implicitly assuming the time step is infintessimal? (This is dicussed much later in his book. He calls it "Euler Integration" and admits it is inaccurate). Mass is also treated informally, and density is never mentioned. So maybe what this page is really about is trying to relate motion to real-world units. And working with vectors for the first time. The p5.Vector class is defined with x and y components but it also has a bunch of methods for determing magnitude and direction and doing vector math. This version, from 7 Feb 2023, has only vertical motion of a single object under the influence of gravity.