Lab Week 9

Particle Motion

Learning Objectives

  • Modify an object definition to give particles more properties and update the particle simulation to account for these new properties.
  • Modify the program so that the particles shrink in size over time based on their age.
  • Create a new particle simulator that includes a repeller, a point that repels particles with some force.


In this lab/recitation, you will write some p5.js programs that use the techniques you’ve learned in class so far. The goal here is not just to get the programs done as quickly as possible, but also to help your peers if they get stuck and to discuss alternate ways to solve the same problems. You will be put into breakout rooms in Zoom to work on your code and then discuss your answers with each other, or to help each other if you get stuck. Then we will return to discuss the results together as a group.

For each problem, you will start with a copy of the uncompressed  in a folder named lab-09. Rename the folder as andrewID-09-AandrewID-09-B, etc. as appropriate.

A. Color Particles

Modify the “Mouse Particles” particle system (the last example from class yesterday) so that particles have a variety of colors and sizes.

  1. Since each particle has two new properties, size and color, modify the makeParticle function so that each particle has two more properties. Set the size to a random value between 5 and 15 and the color to a random color but not black or white). Note that each particle should have a random size and color but this should not change for each particle throughout the program run. In other words, your objects shouldn’t flash different colors or change sizes as they’re moving.
  2. Which other function(s) must be modified? Make the appropriate modifications and test your program.

For convenience, here’s a copy of the code:

Here is one frame of a possible solution:

B. Shrinking Particles

Code the program from the previous problem into a new project. In this exercise, you will decrease the size of the particle as it gets older. After the particle ages every 40 steps, shrink the size of the particle by 20%. Think: where in the code would you make this update?

Test your program to make sure it works correctly. (You may want to reduce the number of particles so you can see what’s happening more easily.)

C. Repelling Particles

Let (rpx, rpy) represent a location that repels particles. The value rpc represents a fixed, constant factor that you should adjust to get the right “look”: if rpc is too small, particles will not be noticeably affected by the force. If  is extremely large, particles will all be immediately pushed away, possibly “running” to the corners to “get away.”

  1. Copy the original mouse particles program into a new project. You don’t need the colors and sizes this time. In this new project, comment out (or remove) the code in particleStep that deals with drag and gravity. You don’t need them for this problem.
  2. Add global variables for rpx, rpy, and rpc. Set rpx = 200, rpy = 100, and try rpc = 1000 for now. You can change it later.
  3. In the particleStep function, after you compute the location of the particle after a possible bounce, compute the effect of the repeller on the particle. To implement repulsion, you want to accelerate away from (rpx, rpy) by the repulsive force. Let’s say the distance from the particle to the repeller is dp (use the dist function to compute dp) and the repelling force is:
    f = rpc / (Math.pow(dp, 2))

    Then the direction of the force is:
    dirx = (x – rpx) / dp
    diry = (y – rpy) / dp
    Add f * dirx to the x coordinate of the particle, and add f * diry to the y coordinate of the particle.
  4. In the draw function, draw a small red square at the location (rpx, rpy) to give the impression that particles are being repelled by the object.
  5. Between runs of your program, modify rpc until you see the particles being repelled from the square.

This challenge is inspired by “Example 4.7: ParticleSystem with repeller” from Chapter 4 of Daniel Shiffman, The Nature of Code.