Lab Week 9

Lab Week 9 – Particles with Size and Color

Problem 1

Modify the “Mouse Particles” particle system (the last example in Week 9 notes) so that particles have a variety of colors and sizes. For convenience, here’s a copy of the code:

You will need to modify the code so that particle objects have new properties to indicate size and color. In the specifications (below), “previous behavior” means what is already in the code which you are free to use, and “new behavior” is behavior that you create by modifying the code.

You should modify the particleDraw function to use the color and size. Remember that you access properties by writing this.color and this.size rather than simply color and size, which would access local or global variables by the same names.

You can set the color and size by adding new property functions such as setColor and setSize OR you can set color and size variables randomly in makeParticle.

Example

Here is one possible solution:

lab9

Specification Check List

  • ___ (Previous behavior) Pressing the mouse generates particles at the mouse position.
  • ____ (Previous behavior) Particles follow a physics-based trajectory.
  • ____ (Previous behavior) Particles bounce off the sides and bottom of the canvas.
  • ____ (Previous behavior) Particles disappear after a fixed number of steps.
  • ____ (New behavior) Particles must have multiple (at least 5 not including black and white) colors. Particles must not flash or change colors from frame to frame.
  • ____ (New behavior) Particles must have multiple (at least 5) sizes. Particle sizes must not change from frame to frame.
  • ____ (New behavior) This requirement is brought to you by the Department of Redundancy Department. As previously stated, particles retain their size and color from frame to frame.

Problem 2

Add a coordinate point rpx, rpy to particleStep to represent a location that repels particles. The repelling force will be equal to rpc / (Math.pow(d, 2), where d is the distance from the particle to (rpx, rpy), computed easily with the Processing dist function. rpc is a fixed, constant factor you should adjust to get the right “look”: if rpc is too small, particles will not be noticeably affected by the force. If rpc is extremely large, particles will all be immediately pushed away, possibly “running” to the corners to “get away.”

To implement repulsion, you want to accelerate away from (rpx, rpy) by the repulsive force. Let’s say the force, computed according the formula above, is f, and assume the particle is at location (x, y). Then the direction of the force is dirx = (x - rpx) / d and diry = (y - rpy) / d, where d again is the distance from (x, y) to (rpx, rpy). The repulsive force to add to (this.dx, this.dy) is (dirx * f, diry * f).

Draw something at the location (rpx, rpy) to give the impression that particles are bouncing off the object.

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