# Help with physarum simulation

**URL:** <https://discourse.processing.org/t/help-with-physarum-simulation/21276>\
**Category:** Coding Questions\
**Created:** [May 25, 2020, 11:32am UTC](https://discourse.processing.org/t/help-with-physarum-simulation/21276 "2020-05-25T11:32:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![musiciantriescoding](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/musiciantriescoding/32/7662_2.png) [@musiciantriescoding](https://discourse.processing.org/u/musiciantriescoding)\
**Post date:** [May 25, 2020, 11:32am UTC](https://discourse.processing.org/t/help-with-physarum-simulation/21276/1 "2020-05-25T11:32:33Z")

</div>

Hello everyone! Recently, physarum simulations have become quite popular. Check out Sage Jenson’s simulations for some beautiful visuals ([https://sagejenson.com/physarum](https://sagejenson.com/physarum)).

I tried to simulate physarum as well using the procedure Sage described in the article. I have the simulation up and running with no errors, but it isn’t behaving the way I would imagine. My particles are jittery and seem to move randomly. Here’s the repo to my code:

> **[chosamuel/physarum\_protoype](https://github.com/chosamuel/physarum_protoype)**
>
> Contribute to chosamuel/physarum\_protoype development by creating an account on GitHub.

Does anyone want to have a go at checking it out? I hope posting this here will open up discussion about physarum simulation in processing!

Best  
Sam

---

<div class="post-metadata">

**Author:** ![noahbuddy](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@noahbuddy](https://discourse.processing.org/u/noahbuddy)\
**Post date:** [May 25, 2020, 7:19pm UTC](https://discourse.processing.org/t/help-with-physarum-simulation/21276/2 "2020-05-25T19:19:36Z")

</div>

Interesting. Gives me the impression of a fluid version of Conway’s game of life.

I have made some changes to your sense function. (comments added)  
Perhaps not _exactly_ your vision, but will hopefully get you heading in the right direction. 😉

```auto
void sense(trailmap tm){
    //
    float nextIntensity = 0;
    float maxIntensity = 0;
    float maxHeading = 0;
    for(int i = -1; i<2; i++){
      //get radians angle from heading direction

      // Change the view relative to our current heading
      float look = heading + i;
      float angle = radians(look*45);

      // increased how far we can see, mostly for diagonals
      PVector offset = PVector.fromAngle(angle).mult(1.41);

      // Removed rounding steps till the end of calculations
      //offset.normalize();
      //offset.x = floor(offset.x);
      //offset.y = floor(offset.y);

      int currentX, currentY;
      //currentX = int(floor(pos.x) + offset.x);
      //currentY = int(floor(pos.y) + offset.y);
      currentX = int(pos.x + offset.x);
      currentY = int(pos.y + offset.y);

      if(currentX > width-1){
        currentX = 0;
      } else if(currentX < 0){
        currentX = width-1;
      }

      if(currentY > height-1){
        currentY = 0;
      } else if(currentY < 0){
        currentY = height-1;
      }

      nextIntensity = tm.grid[currentX][currentY];
      if(maxIntensity < nextIntensity){
        maxIntensity = nextIntensity;
        dir.x = offset.x;
        dir.y = offset.y;
        dir.setMag(maxSpeed);
        maxHeading = i;
      }
    }
   //turn particle
    heading+=maxHeading;
  }

```

---

<div class="post-metadata">

**Author:** ![musiciantriescoding](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/musiciantriescoding/32/7662_2.png) [@musiciantriescoding](https://discourse.processing.org/u/musiciantriescoding)\
**Post date:** [May 26, 2020, 6:11am UTC](https://discourse.processing.org/t/help-with-physarum-simulation/21276/3 "2020-05-26T06:11:42Z")

</div>

Hey @noahbuddy , yeah, it’s a kind of cellular automata and there’s a whole genre called cyber fungi dedicated to simulating this.

I just implemented your changes and the simulation now works! I can’t believe I forgot to adjust the heading when sensing the trailmap. Also, the sensing distance is also a great idea. Thank you!

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [June 12, 2020, 11:06pm UTC](https://discourse.processing.org/t/help-with-physarum-simulation/21276/4 "2020-06-12T23:06:12Z")

</div>

@musiciantriescoding – Sorry for the late comment, I am just discovering this thread now.

I remember trying my hands at this algorithm about a year ago, hoping like you to generate similar outputs, and quickly came to the conclusion that Sage Jenson was not telling the whole story about his implementations. The animations are much more fluid, material transportation and deposit looks incredibly more organic. So far, I have not seen anyone getting even close to his outputs based solely on the paper mentioned in his article.  
Some believe there is some sort of fluid simulation at play, other think it is a combination of different growth algorithms… the magic recipe has still to be found.

It is worth mentioning that Jeff Jones (author of the original paper in 2010) has published an another [document](https://arxiv.org/ftp/arxiv/papers/1212/1212.0023.pdf) a couple of years later entitled _Emergence of Self-Organized Amoeboid Movement in a Multi-Agent Approximation of Physarum polycephalum_. I didn’t give it much time yet but it might worth checking.

In a nutshell he introduces the difference between oscillatory and non-oscillatory behaviors and suggests to adjust the strength of the inertial effect probabilistically.

Some old unsatisfactory outputs:

 ![physarum_evolution](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/83f3e8a3c0101a2aa3dce0c7b430ee2945dd90bc.jpeg)  
(_non-osciallatory behavior_)  
 ![physarum-oscillation](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b204c06dde172bc50f442cb653cd20a5d9aa5248.jpeg)  
(_osciallatory behavior_)

---

<div class="post-metadata">

**Author:** ![musiciantriescoding](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/musiciantriescoding/32/7662_2.png) [@musiciantriescoding](https://discourse.processing.org/u/musiciantriescoding)\
**Post date:** [June 13, 2020, 6:24am UTC](https://discourse.processing.org/t/help-with-physarum-simulation/21276/5 "2020-06-13T06:24:21Z")

</div>

Hey @solub, thanks for the reply! No worries, this thread is not that old and I’m still thinking about this topic.

Here’s an output I got

![0899](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f9302f3ef537ddb635edb5ed289bd42f5e30e44b.png)

I initially suspected the same thing as you did. However, Sage Jenson mentioned that all he did was played with the parameters he listed in his article. I’ve thought of a few ways one could play with the parameters. Here are a few:

1. Scaling the deposition amount by the particle’s velocity
2. Scaling the particle’s velocity by the intensity of the trailmap
3. Scaling the turning angle by the particle’s velocity
4. Scaling particle velocity by distance to center of screen

and so on…

I’m quite sure by playing with the parameters like this in a creative way and in creative combinations, we could arrive at similar outputs as Sage did. After all, this is the part of creative coding that’s for the artist to play with!

> The animations are much more fluid, material transportation and deposit looks incredibly more organic.

I believe this has to do with how he implemented it: fully on the gpu. The sheer amount of particles (5-10 million) will definitely make the simulation look more refined. My simulation had only 5,000 particles because I implemented it on the CPU.

Thanks for the link to the new document! I will check it out.

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [June 13, 2020, 2:01pm UTC](https://discourse.processing.org/t/help-with-physarum-simulation/21276/6 "2020-06-13T14:01:27Z")

</div>

Interesting, thank you for sharing your thoughts and the outcome.

> [@musiciantriescoding](#):
>
> My simulation had only 5,000 particles because I implemented it on the CPU.

Have you tried increasing the amount of particles ? Your code indicates a default number of 20000 and I was able to run your sketch with up to about 100000 particles. On my side I didn’t notice any improvement in the overall behavior even though it is still far from the 5 millions indicated in the article.

> [@musiciantriescoding](#):
>
> Sage Jenson mentioned that all he did was played with the parameters he listed in his article.

Have you seen him saying this explicitly somewhere ? In the article he mentions Jones’s paper then talks about his implementation and how it differs from the original rules (no collision detection) but nowhere he says having restricted the algorithm to the initial parameters. Again, I wouldn’t be surprised if the whole thing was a combination of more complex behaviors.

That said, like you I do think there is some sort of dynamic scaling at play and I am suspecting the angle of rotation (turning angle) to be somehow depending on the amount of chemo-attractant detected by the sensors. In his article Sage J. also mentions a “contagion behavior” that seems to affect the shape of the trails. The more “contaminated” a particle is, the more diffuse and widespread its displacement. At least that’s how I understand it.

Some notes:

 ![Screenshot (441)](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c9c142030deee8d12dc056cbeba86a402f4db7c6.jpeg)

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [June 19, 2020, 7:55pm UTC](https://discourse.processing.org/t/help-with-physarum-simulation/21276/7 "2020-06-19T19:55:36Z")

</div>

Hi @solub  
I don’t know how to reach you, so I’ll try it this way.  
In one of your posts you recommended the “hemeshgui” tool.  
I installed it, but it gave an error that came from the HE\_Mesh lib.  
The “Ref\_HEC\_ConvexHull” example also gives the same type of error “The function setN(int) does not exist.” @wblut is not a subscribed member, so I already posted an issue on Github, but I’m afraid that it will take a long time to receive an answer. because another issue is waiting for months.  
Do you have a working version?
