Forces and Newtonian Physics

A force is a vector representing a push or a pull that causes an object to accelerate.

Issac Newton developed three laws to describe the motion of objects subject to forces.

Table of Contents

  1. Objectives:
  2. Textbook Chapter
  3. Exploring Physics in Code
  4. Newton’s Laws of Motion
  5. Forces
  6. Newton’s First Law - Inertia
  7. Newton’s Second Law - Force
  8. Newton’s Third Law - Action & Reaction
  9. Weight vs Mass
  10. Newton’s Laws as Code
  11. Wind and Friction
  12. Wind and Friction Simulation
  13. Adding Gravity

Objectives:

By the end of this module you should be able to:

  • Describe Newton’s three laws of motion.
  • Explain the difference between weight and mass.
  • Using p5.js to implement Newton’s second law as code and use it to simulate the forces of gravity, friction, and drag.

Textbook Chapter

Chapter 2 - Forces - Nature of Code [pdf]

Attribution: The textbook for this course is Daniel Shiffman’s Nature of Code. The Java Processing version of the book is available online. PDF of the p5js version linked above is under development and is licensed under tbuhe Creative Commons Attribution-NonCommercial 3.0 Unported License.

Exploring Physics in Code

Sir Isaac Newton. Painting by Godfrey Kneller

Let’s build a simple physics engine!

We use our new knowledge of vectors, combined with the Three Laws of Motion first described by Issac Newton in the 1680s.

Newton’s Laws of Motion

Netwon’s three laws are the foundation of classical mechanics, the part of physics that describes the movement of objects in the world.

In short, the three law’s are:

  • An object at rest stays at rest and an object in motion stays in motion.
  • Force equals mass times acceleration.
  • For every action there is an equal and opposite reaction.

Forces

A force is a vector representing a push or a pull that causes an object with mass to accelerate.

There are contact forces like friction and tension.

There are also action-at-a-distance forces like gravity and magnetism.

Forces are measured in a unit known as a Newton (N). One Newton is the amount of force needed to apply 1 m/s² of acceleration to a 1kg object. Force is a vector, so the applied acceleration of a Newton has both a magnitude and a direction.

Newton’s First Law - Inertia

An object at rest stays at rest, and an object in motion stays in motion at a constant speed and direction, unless acted upon by an unbalanced force.

This is the law of inertia, first formulated and refined by Galileo Galilei and René Descartes.

It might seem weird to think of moving objects remaining forever at a constant speed. If you roll a ball it eventually stops, but that’s because the forces of friction and air resistance slow it down.

Thinking in vectors, we might restate the law as:

An object’s velocity remains constant until an unbalance force changes its acceleration.

The force must be unbalance, because balanced forces will cancel each other out. For example, a one Newton force applied due East to an object will be cancelled out by an equal force applied due West.

Newton’s Second Law - Force

Force equals mass times acceleration.

This law quantifies the changes a force can produce on the motion of an object. If we take an object in motion, we can say that this object has a momentum equal to its velocity times its mass. An object’s momentum is a vector with the same direction as its velocity. A force applied to an object can change its the direction or magnitude of its momentum.

Mathematically the second law look like this:

The Second Law

From this we can see that:

  • Acceleration is directly proportion to force: The harder you are pushed, the faster you’ll accelerate.
  • Acceleration is inversely proportion to mass: The heavier you are, the slower you’ll move.

Newton’s Third Law - Action & Reaction

For every action there is an equal and opposite reaction.

In other words, forces always occur in pairs. When two objects interact, they apply forces to each other that are equal is magnitude and opposite in direction.

In terms of vectors, these paired forces could be labeled f1 and f2 where f2 = -f1.

For simplicity we’ll play fast and loose with this law, sometimes only modelling one of the forces in any given pair. For example, when adding wind to a simulation we might add the force of the wind to the objects in the scene, but we’ll ignore the equal force exerted by objects back on the air.

Weight vs Mass

Note that the second law mentions mass, not weight. There’s a technical difference between these two quantities that we gloss over in everyday life.

  • The mass of an object is the amount of matter in an object, measured in Kilograms.
  • The weight of an object is the force of gravity on an object, measured in Newtons.

From the 2nd law, if weight is the force of gravity:

F = m * A
Weight = mass * Gravity

A cannonball has the same mass on earth as on the moon, but due to the decrease in gravity, it weighs less on the moon.

Newton’s Laws as Code

Starting with a simple Mover class:

class Mover {
  constructor(position, velocity, mass) {
    this.position = position;
    this.velocity = velocity;
    this.acceleration = createVector();
    this.mass = mass;
  }
}

The first law tells us that if we want to change the Movers velocity we need to apply a force.

Let’s add an applyForce() method to the Mover class based on the second law:

applyForce(force) {
    this.acceleration = p5.Vector.div(force, this.mass);
}

In order to allow multiple forces to act of a Mover we need the forces to accumulate:

applyForce(force) {
    let scaledForce = p5.Vector.div(force, this.mass);
    this.acceleration.add(scaledForce);
}

We can now change a Mover’s velocity like this, assuming we have wind and gravity vectors:

mover.applyForce(wind);
mover.applyForce(gravity);
mover.update();

We also need to reset acceleration to zero on every update() otherwise old forces will continue to have an affect:

update() {
    this.velocity.add(this.acceleration);
    this.position.add(this.velocity);
    this.acceleration.set(0, 0);
}

Wind and Friction

As seen above we can simulate wind simply by apply a small force in a specific direction.

For friction we will use an idealized version of the static friction formula:

F = -1 * μ * N * v̂
  • μ (mu) is called the coefficient of friction. The smaller the number, the less friction.
  • N is the Normal Force of gravity times mass. For now we’ll only consider horizontal surfaces.
  • v̂ is the velocity of the moving object.
  • The -1 indicates that friction works in the opposite direction of the object’s velocity.

If we ignore mass we can get away with a simple scaling of the velocity:

friction() {
  // Scale velocity to 95% regardless of mass.
  this.velocity.mult(0.95);
}

Implementing the friction formula isn’t too tricky:

friction() {
   // Direction of friction
    let friction = this.velocity.copy().normalize().mult(-1);

    // Magnitude of Friction
    let normal = this.mass; // Simplified by ignoring gravity constant.
    friction.setMag(this.mu * normal);

    // Apply the friction force to the mover.
    this.applyForce(friction);
}

Wind and Friction Simulation

Here’s a simulation with idealized wind and friction. Imagine you are looking “top down” at a pool table or a golf green. The size of the object scales with its mass. Apply wind by clicking. Hit any key to randomize object sizes. For simplicity objects do not interact with each other.

Edit Code Using p5.js Web Editor

Adding Gravity

Here’s another simulation, but this time with gravity added as constant downwards force. Imagine this time that the bottom of the canvas is the ground. You can still add wind by clicking, and hitting any key restarts the simulation with newly randomized objects.

🎵 Note:

Friction is now serving two different purposes in this simulation:

  1. Horizontal ground friction.
  2. Loss of energy on bounce, like would happen in real life due to momentum lost to sound and heat.

Edit Code Using p5.js Web Editor