Lab Week 9 – Particles with Size and Color
Modify the “Mouse Particles” particle system (the last example in Week 9 notes) so that particles have a variety of colors and sizes.
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.size rather than simply
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
setSize OR you can set color and size variables randomly in makeParticle.
Here is one possible solution:
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.
- ____ Challenge problem: Add a coordinate point rpx, rpy to
particleStepto represent a location that repels particles. The repelling force will be equal to
rpc / (Math.pow(d, 2), where
dis the distance from the particle to (
rpy), computed easily with the Processing
rpcis a fixed, constant factor you should adjust to get the right “look”: if
rpcis too small, particles will not be noticeably affected by the force. If
rpcis 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 (
rpy) by the repulsive force. Let’s say the force, computed according the formula above, is
f, and assume the particle is at location (
y). Then the direction of the force is
dirx = (x - rpx) / dand
diry = (y - rpy) / d, where
dagain is the distance from (
y) to (
rpy). The repulsive force to add to (
this.dy) is (
dirx * f,
diry * f).
Draw something at the location (
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.