We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello Processing community,
I am new with p5.js and I am learning to do some fun simulations with it. But I would like to ask for some help.
I am trying to do a simulation about diffusion. Particles moving randomly from left to right. So far this is what I have done:
https://www.openprocessing.org/sketch/435895
I would like to know how to count the number of particles crossing the middle wall in the simulation. Or how can I count particles from one region to another?
Please, if you have any advice or a link where I can learn about this, I would appreciate it.
Thanks in advance...
Answers
http://www.JeffreyThompson.org/collision-detection/circle-rect.php
What can you say about the horizontal position of a particle that is on the left of the center line? What value must it be less than?
Then you just need to loop over all the particles and add one to a total for each particle that is on the left.
The total on the right is then the total number of particles minus the number on the left.
Hey guys,
I will study the methods from the link, thanks GoToLoop
I will try your suggestions TfGuy44
Have a good day.
Hello everybody,
Thanks again for the suggestions and link. I was able to adapt some code from Jeffrey Thompson from processing to p5.js. And I was able to count the number of particles from one region to another.
Here is what I have done:
https://www.openprocessing.org/sketch/437388
Other suggestions to improve the code will be appreciated.
Have a great day. Kind regards... :)
Sigh.
Hi TfGuy44,
I tried to count particles using your code. And it works fine when the particles don't move around. That is when they are not being drawn constantly. If I write the code in the draw function, then the variable "count" increases constantly. It seems, I am missing something :(
Is there a way to stop that loop in the draw function and count the particles just when they are the specific region?
On the code I wrote, I defined another array where I store when a particle is the region or not (with 0s and 1s). Then I sum the elements of the array, so I get the number of particles.
Here, you can see it with the counting:
https://www.openprocessing.org/sketch/437525
Cheers!
Did you make sure to set the count variable to 0 each time draw() runs, before you start counting?
There we go... That is what I forgot to add. ups!
Now I will be working adding some graphs... Maybe in the future I will ask for more help...
Thanks alot for your patience and time to have look of my code... :)
Have a great day...