# Barnes Hut and magnetic (Coulomb) forces

Hi,

I’m working working at a particle system where I need to have lots of particles (around 5K or more). I want all particles to be attracted to a couple of attraction points, though they should keep a distance form these points. Each of the particles tries to keep some space from the surrounding particels to get a shape like this:

Here I’ve implement the particles and coulomb force using just 300 particles which works fine. Because I need more I’ve googled to find some info on implementing a system where each particle interacts with surrounding particles. Someone advised me to use the Barnes Hut algorithm… so I did which works fine as far as normal interaction. Though when I added the coulomb forces it works, but only for each quadrant. Therefore I’m wondering if someone knows more about Barnes Hut or another algorithm I could use?

This is the result when using Barnes Hut together with coulomb forces:

Thanks.

hi roxlu,
there is an interesting thread on particle binning here,

not sure if it helps but looks a lot like what ur working on.

L.

i’d suggest particle binning here also.

the slow-down when you add new particles is coming from the part of the code that works out the distance between particles. it scales poorly because, expressed in Big-Oh-notation, it is O(n^2): each particle (n) has to loop through all the other particles (n) to determine distance: n*n = n^2.

particle binning means you can only focus on paricles that matter: you decide that you can ignore all particles more than, say, 10 distance units away, then finding the particles nearer than that is just a case of walking through the relevant bins and only looking at those particles.

hth
d

Hi Damian and Julapy,