Particle Life

Try opening the program in a separate tab, right now the embedded program is a bit small. It will go fullscreen.

I tried to recreate the amazing particle life program(s) that exist on the internet, in particular this one by Tom Mohr on Youtube:

Although I came nowhere near making something as beautiful and elegant, I took a decent first step.

How it Works

Each color of particle is attracted to other colors by a certain “attraction factor”, which is a randomly chosen integer between -3 and 3. Negative integers represent repulsion, and positive integers represent attraction (0 means no attraction or repulsion). For example, pink can be attracted to green, but repelled by itself, like in the example below:

As you can see, particles clump together to form these “cell” like structure with “membranes" and a stunning geometry. It all emerges out of a few stupidly simple rules of attraction.

The rules of attraction are also displayed in the grid in the bottom left corner:

The grid is read as such: (row) color is attracted to (column) color, with the color of the square shown on the grid corresponding to the attraction factor. Red represents repulsion, and green represents attraction. A higher brightness of color means a stronger attraction/repulsion, and black means neither attraction nor repulsion.

If that was confusing, here’s an example: white is strongly attracted to green, since the square in the white row and green column is bright green. However, green is mildly repelled by white, since the square in the green row and white column is a dull red.

Best of all, you can click in any of the squares in the grid to change the attraction factor of the corresponding colors. This allows you to play with the rules and create different configurations.

Note: I used chat gpt to create the grid and it’s clicking functionality. Pretty dope.

Here are just a few more examples of beautiful patterns that emerged from the program:

Sometimes like in the example above, the particles form a beautifully dynamic pattern where some colors are constantly “chasing” other, while some colors “run away”, resulting in a never ending spiral of movement.

First Attempts (and failure)

At first I had a really hard time getting it to work at all. The particles would inexplicably just get stuck in the corner of the cavnas, which I had no idea how to fix. I seriously almost gave up right from the beginning.

Somehow, amazingly I finally did fix the issue, which turned out to be the most ridiculous issue possible. It was in the get_force function, which calculates the force to apply on a particle given another particle:

The function takes a target particle object and a given object to calculate the force on. However, this line of code actually modifies the original vector objects that were passed in, so every time a force was calculated (which is supposed to be non-destructive), the actual value of the position gets modified (destructive) by running the .sub function.

Not even chat gpt was able to catch this error for me. Absolutely maddening.

I finally fixed it by making copies of the original position vectors, and using those to make the calculation:

Coding can really be a pain in the ass sometimes...

Next Steps

Originally my plan was to use a quad tree to allow for more particles. Since right now each particle has to scan every other particle in existence to calculate its own force, I’m limited to 500 particles. That is already a lot more than I expected was possible. I’m hoping that by implementing a quad tree succesfully, I’d be able to support several thousand particles for a more complex and expansive particle universe.

I also want to add in more sophisticated behaviors for the particles, like “chemical bonds”, “bonding structures” etc and see what kind of amazing patterns I can create out of that. Or maybe I’d want to modify it somehow to make patterns resembling cosmic bodies, like galactic formations, planetary systems, etc (go macro rather than micro).