Nature of Code Week #4: Particle Systems

Nature of Code continues to be fascinating.  Professor Shiffman’s notes are incredibly well-done and very easy to understand as he explains pretty dense physics problems sequentially — the forthcoming kickstarted Nature of Code book will be well worth its price.

This week we covered particle systems, inheritance, and polymorphism.  The particle systems go hand in hand with repelling forces, which were built upon applying forces to other objects, which was built upon vector movement.  I’ve been having to re-read the notes for each chapter several times each week to make sure I understand it.

Our homework:

“At this point we’re a bit deeper in the semester and approaching the midterm project. Feel free to simply start on a midterm idea or continue something you’ve been working on previously. If you would like to try an exercise related to particle systems, here are some suggestions:

  • Use a particle system in the design of a “Mover” object. In other words take, say, one of our earlier examples and instead of rendering a Mover object as a simple circle, emit particles from the mover’s location. Consider using the Asteroids example and emit particles from the ship when a thrust force is applied.
  • Create a particle system where the particles respond to each other via forces. For example, what if you connect the particles with spring forces? Or an attraction / repulsion force?
  • Model a specific visual effect using a particle system — fire, smoke, explosion, waterfall, etc.
  • Create a simulation of an object shattering into many pieces. How can you turn one large shape into thousands of small particles?
  • Create a particle system in which each particle responds to every other particle. (Note we’ll be doing this in detail in Week 6.)”


I immediately started playing with creating systems of systems of particles, using Prof. Shiffman’s example code.  I turned down the particle size, increased the constrained force distance, and also increased the number of repelling objects to 60.  I had some particle systems randomly placed upon sketch startup, but also made it so you could add more systems upon mousePressed().  I tweaked the color and background to look more like fading embers or fire, then made the repeller objects very faint.  The particles gradually fade away until they die, using opacity as a measure of life strength, so that when opacity reaches 0, the particle dies.  This helps to preserve framerate.

The result is that the system ends up looking like Dorsey-ish flows of traffic, or seeing dynamic internet traffic as it passes through the world’s backbones at night.  Feedback in class suggested that I try to add repeller forces as geometrical shapes, to guide the particles instead of having random patterns generated.

Particle(PVector l) {
    acceleration = new PVector(0,0);
    velocity = new PVector(random(-.3,0.3),random(-.1,.1));
    location = l.get();
    lifespan = 200.0;
    randColor = color(int(random(150,230)),int(random(90,130)),int(random(25, 90)));


for (ParticleSystem2 psX: systems) {;
    for (int i = 0; i < repellers.length; i++) {

A video:

Code at Github &

What I’m thinking now is that, for my NoC midterm and final, I can adapt my final from Intro to Computational Media last semester so that all my objects are essentially particles exerting forces on each other.

For a recap of my project, read this long blog post.  Simply put, the project was a simulation incorporating the traits of people, nations, and religions, creating offspring who are summations of their genetics and environments.

I might try to make the text labels for each person orbit the particles, but this might result in a massive drop in framerate.  Here’s a screenshot of what my final project looked like.

What’s good is that I didn’t attempt movement last semester, since I didn’t really know how to dynamically and smoothly move the “people” using vectors.  I am a little worried I’ll have to re-write some deep parts of the code in order to get this to work, but it’s a worthy project.