A body sliding down an inclined plane in Processing

Lesson 9 of 14 · Processing

A simulation of a body sliding down an inclined plane is a useful way to show the basic laws of physics in practice, including the forces of gravity, friction and the reaction of the surface. On an inclined plane, the weight of the body splits into components that act parallel and perpendicular to the surface. This Processing animation uses visual elements to simulate the motion of the body down the slope, with a moving coordinate system that follows the body. The simulation helps you understand dynamics on an inclined plane and makes learning more interactive.

See a simulation made with p5.js on the web page (in Serbian): simulacije.svetprogramiranja.com/klizanje_tela_niz_strmu_ravanLet's create a simulation of a body sliding down an inclined plane.

 Inclined plane – the forces acting on the body
Figure 1: Inclined plane – the forces acting on the body

The body is on an inclined plane, tilted by some angle alpha relative to the horizontal plane. The force of gravity Q acts on the body, and it can be split into 2 components: Fa, parallel to the inclined plane, and FN, the normal component, perpendicular to the inclined plane. The reaction force of the surface N cancels this normal component, because it has the same direction line but the opposite orientation, so it does not affect the motion. The friction force opposes the motion; it acts along the slope, opposite to the velocity vector.

For the body to start sliding down, this condition must be met:

F a >= F tr

The friction force is a passive force: it exists only if there is an active force that tends to move the body, and it always acts along the same line as the velocity of the body, but in the opposite direction. The active force here is the component of the body's weight along the surface, marked Fa.

The friction force depends, on one side, on the kind of surface, and this effect is measured by the friction coefficient, and on the other side on the magnitude of the normal force on the surface N. So the friction force is calculated as:

F tr = μ * N;
gde je
        μ -
    koeficijent trenja,
    N - normalna sila koja deluje na podlogu

To create the simulation, let's look at the right triangle that represents the ground on which the body slides along the hypotenuse.

The legs of the triangle are known: we call the opposite leg H (height) and the adjacent leg L.

The angle alfa can be found as:

alpha = atan(H / L);

We will create the simulation in a moving coordinate system attached to the body (X1 O1 Y1)

 Animation of a body sliding down an inclined plane in Processing. The moving coordinate system
Figure 2: Animation of a body sliding down an inclined plane in Processing. The moving coordinate system

The moving coordinate system is obtained by first translating the XOY coordinate system by H-height/2-h/2/cos(fi), and then rotating it by the angle fi.

Animation of a body sliding down an inclined plane in Processing. Rotating the coordinate system
Figure 3: Animation of a body sliding down an inclined plane in Processing. Rotating the coordinate system

Let's open a new sketch and add the setup and draw methods. In them we can set the background color background(100) and create the shapes of the triangle and the body (a rectangle).

Code from figure 4: Animation of a body sliding down an inclined plane in Processing, the basics

float H = 300;          // height of the inclined plane
float L = 600;          // length of the base
float fi;               // slope angle
PShape tlo;
PShape rect;
float w = 80;           // width of the body
float h = 40;           // height of the body

void setup() {
  size(1000, 500, P2D);
  fi = atan(H / L);
  tlo = createShape(TRIANGLE, 0, height - H, 0, height, L, height);
  tlo.setFill(color(0, 255, 0));

  rectMode(CENTER);
  rect = createShape(RECT, 0, 0, w, h);
  rect.setFill(color(0, 0, 255));
}

void draw() {
  background(100);
}

To create the triangle, we first need to define and initialize the height H and the length L. Inside the setup method we can compute the slope angle with the function arctangent of H/L:

To create the body and the triangle, we use Processing's class PShape (see the reference: https://processing.org/tutorials/pshape)

The variables w and h are the width and height of the body. Fill colors are set for these two shapes with the method setFill, using the method color, which creates a color from RGB values.

Next we move the coordinate system with translations, as shown in the following code:

Code from figure 5: Animation of a body sliding down an inclined plane in Processing, drawing the ground

float H = 300;          // height of the inclined plane
float L = 600;          // length of the base
float fi;               // slope angle
PShape tlo;
PShape rect;
float w = 80;           // width of the body
float h = 40;           // height of the body

void setup() {
  size(1000, 500, P2D);
  fi = atan(H / L);
  tlo = createShape(TRIANGLE, 0, height - H, 0, height, L, height);
  tlo.setFill(color(0, 255, 0));

  rectMode(CENTER);
  rect = createShape(RECT, 0, 0, w, h);
  rect.setFill(color(0, 0, 255));
}

void draw() {
  background(100);

  shape(tlo);
  translate(0, height - H - h / 2);
  shape(rect);
}

If we run the code, we will see:

 Animation of a body sliding down an inclined plane in Processing, translating the origin
Figure 5: Animation of a body sliding down an inclined plane in Processing, translating the origin

The next thing to do is to rotate by the angle fi and move the starting position with a translation by 50px:

rotate(fi);
translate(50, 0);

We will also draw the coordinate axes, to follow how the moving system moves. The code now looks like the following picture:

Code from figure 6: Animation of a body sliding down an inclined plane in Processing, drawing the axes

float H = 300;          // height of the inclined plane
float L = 600;          // length of the base
float fi;               // slope angle
PShape tlo;
PShape rect;
float w = 80;           // width of the body
float h = 40;           // height of the body
PShape xOsa;
PShape yOsa;

void setup() {
  size(1000, 500, P2D);
  fi = atan(H / L);
  tlo = createShape(TRIANGLE, 0, height - H, 0, height, L, height);
  tlo.setFill(color(0, 255, 0));

  rectMode(CENTER);
  rect = createShape(RECT, 0, 0, w, h);
  rect.setFill(color(0, 0, 255));
  xOsa = createShape(LINE, 0, 0, 100, 0);
  yOsa = createShape(LINE, 0, 0, 0, 100);
}

void draw() {
  background(100);
  shape(tlo);
  translate(0, height - H - h / 2);
  rotate(fi);
  translate(50, 0);
  shape(rect);
  xOsa.setStroke(color(255, 255, 50));
  yOsa.setStroke(color(255, 150, 50));
  shape(xOsa);
  shape(yOsa);
}

Adding motion to the body

To add motion to the body, we need to add the following physical quantities:

PVector polozaj;
PVector v;
PVector a;
static final float G = 9.81;
float m = 1;

The position vector gives the center point of the body sliding along the slope. v is the velocity of the body, a is the acceleration, G is the gravitational constant and m is the mass, whose value is set to 1kg.

Inside the setup() method, add the following code:

//Creating the acceleration, velocity and position vectors needed for the motion of the body
a = new PVector(1, 0);
v = new PVector(0, 0);
polozaj = new PVector(50, 0);

Inside the draw() method, add the following:

//motion
v.add(PVector.div(a, frameRate));
polozaj.add(v);
translate(polozaj.x, polozaj.y);

The magnitude of the acceleration a is first divided by the constant frameRate, to get the change of velocity for the time interval between two calls of the draw() method. This value is added to the velocity vector, which increases its magnitude. Finally, the change of position is given by the value of v, so the method add is used to add the position and the velocity vector v.

Read more about vectors on the page: Vectors in Processing.The complete sketch can be seen in the following picture, where the code for the motion is underlined with red lines:

Code from figure 7: Animation of a body sliding down an inclined plane in Processing, the complete code

float H = 300;          // height of the inclined plane
float L = 600;          // length of the base
float fi;               // slope angle
PShape tlo;
PShape rect;
float w = 80;           // width of the body
float h = 40;           // height of the body
PShape xOsa;
PShape yOsa;
PVector polozaj;        // ← new
PVector v;              // ← new
PVector a;              // ← new
static final float G = 9.81;   // ← new
float m = 1;            // ← new

void setup() {
  size(1000, 500, P2D);
  fi = atan(H / L);
  tlo = createShape(TRIANGLE, 0, height - H, 0, height, L, height);
  tlo.setFill(color(0, 255, 0));
  rectMode(CENTER);
  rect = createShape(RECT, 0, 0, w, h);
  rect.setFill(color(0, 0, 255));
  xOsa = createShape(LINE, 0, 0, 100, 0);
  yOsa = createShape(LINE, 0, 0, 0, 100);
  // Motion
  a = new PVector(1, 0);        // ← new
  v = new PVector(0, 0);        // ← new
  polozaj = new PVector(50, 0); // ← new
}

void draw() {
  background(100);
  shape(tlo);
  translate(0, height - H - h / 2);
  rotate(fi);
  // motion
  v.add(PVector.div(a, frameRate));       // ← new
  polozaj.add(v);                         // ← new
  translate(polozaj.x, polozaj.y);        // ← new
  shape(rect);
  xOsa.setStroke(color(255, 255, 50));
  yOsa.setStroke(color(255, 150, 50));
  shape(xOsa);
  shape(yOsa);
}
Note: In the picture the code was cut off after shape(yOsa); – the closing bracket of the draw() method has been added. The lines marked with “← novo” (new) were underlined in the picture.

In the picture below you can see what the animation of a body sliding down an inclined plane looks like.

Processing example: Inclined plane 2

The previous example of a body sliding down an inclined plane can be developed further: clicking the control point selects it, and dragging the mouse (mouseDragged) changes its height, and with it the slope angle of the plane. The following data should also be shown: the sliding speed, the slope angle, the magnitude of the friction force and the friction coefficient. The animation is shown below, and the solution (code) is on the next page.

Click (tap) the small square at the top of the inclined plane and drag it up or down. A double click or the r key puts the body back at the start.

Read the explanation and the complete analysis of the example on the page: Analysis of a body sliding down an inclined plane – example

Check your knowledge

Open the quiz

Quiz: Inclined plane

1. Which component of the weight moves the body down the inclined plane?




2. Why is the coordinate system rotated by the angle of the inclined plane?




3. The acceleration of a body on a frictionless inclined plane is:




4. What does rotate(ugao) do?