hunar4321/particle-life

[Feature request] Distance-based force

redblobgames opened this issue · 8 comments

The current simulation applies a force that is independent of distance. If the distance is 3 or 30 the force applied is the same.

suppose dx = 3 ; dy = 0
then r = sqrt(dx*dx + dy*dy) = 3
then fx  = dx / r = 1

or

suppose dx = 30 ; dy = 0
then r = sqrt(dx*dx + dy*dy) = 30
then fx  = dx / r = 1

There is a force that does not depend on distance, and then stops at some threshold; it is the Strong force, but it is mainly for the quarks inside an atom nucleus, not for artificial life. It would be interesting to use some force that depends on distance, because that would apply to real life forms. Maybe 1/d² (gravity, electromagnetism in a 3d world), 1/d (gravity or electromagnetism in a 2d world), van der waals force which can take into account the size of the particles, or Lennard-Jones which is the force between molecules. The interesting thing with Lennard-Jones is that at close distances it pushes molecules apart and at far distances it pulls them closer. This can create really interesting patterns. There is also a generalization of Lennard-Jones called Mie potential that might be useful for adapting Lennard-Jones to work in 2d.

Before you try these, I have to warn: I did try some of these and I think it will be a bit of work. It is easy to change the formula in the code. But I think all the other numbers in the code like the 0.5 and the -100 to +100 values and the G / -100 are all designed to work with the original distance-irrelevant force, and I think all of those would have to be somehow tweaked to handle distance.

Interesting! That may explain why there was sub-atomic look-like behavior in some demos. Though I think constant forces might exist at higher levels too especially in small viscous spaces. Anyway, thanks for the suggestion, I'll try them when I have time and would like to see your results too if you could share them. Thanks.

I think the constant forces essentially mean that each particle looks for nearby particles and moves towards (or away from) the "center of mass". This reminds me of flocking, which has rules for creatures to look for nearby creatures and try to move towards them or away from them.

ker2x commented

I believe that would make the simulation a plain old nBody simulation that would remove the uniqueness of this software.

Hi! I was thinking of suggesting that you introduce a matrix of the radii of action of each particle interaction. For example, if the interaction matrix states that red attracts red with a force of 0.27, it should also state that this interaction only takes place over a maximum distance of 50. In the program I saw that there is a possibility to set the action radius for a particular color, but it is for the interaction between that color and any other, but it would be more interesting to set one for each other color.
For example, we have the following matrix of interaction forces between 3 colours, red, green and blue: rr +0.20, rg +0.30, rb -0.65, gr +0.15, gg +0.23, gb -0.80, br +0.23, bg -0.45, bb -0.74.
Range = 80.
Let a range matrix be as follows: rr 50, rg 10, rb 35, gr 60, gg 120, gb 200, br 0, bg 200, bb 150. This would say that red repels red with a force of 20% only if they are within 50 of each other. Or that blue repels red with a force of 23% only if they are within 0 of each other (i.e. if they are glued together).
Of course, a matrix of coloured reactions can also be introduced, in which a particle of one colour can turn into another if it touches a particle of a certain colour. For example: rr -> rr, rg -> rb, rb -> rg, gg -> gg, gb -> gr, bb -> bb.
So there would be 3 matrices: one of interaction forces between colours, one of interaction distances between colours, and one of reactions between colours.
I would love to see all three matrices implemented in your code. I'm very curious what would come out. I don't know how to program that if I did I would do this myself.

ker2x commented

you mean something like this ?
https://i.imgur.com/NxGj6Ju.png

you mean something like this ? https://i.imgur.com/NxGj6Ju.png

Yes. Basically it would be a combination of Tims Insane Meta Universe and Particle Life.

ker2x commented

Well my screenshot is from the C++ version of particle life :)
https://www.youtube.com/watch?v=aw1tUiN5gNs

(i optimized it since the video, it run at 90FPS on my laptop now)

Well my screenshot is from the C++ version of particle life :) https://www.youtube.com/watch?v=aw1tUiN5gNs

(i optimized it since the video, it run at 90FPS on my laptop now)

Wow, you really have found some interesting patterns especially the one at 13:19 and 14:10. You should have put some music with it.