Simulation of separation forces is n bodies

I am writing a simulation for small creatures that walk around the screen. One of the problems that I encounter is that they sometimes get too close - I believe that one way to solve this is to have a certain threshold distance, and when any two creatures get close to this distance, the force will push them apart.

However, before I accomplished this, I was wondering if there were any known algorithms for this other than brute force solutions enter image description here.

I studied several different algorithms, and one of the notes was the Barnes-Hut algorithm, which works in enter image description heretime - however, I'm not sure if this will work for this problem.

Any help is appreciated

+4
source share
2 answers

Barnes-Hut and options are a common way to do this, but I would not have implemented a complete algorithm for your use case, because it seems redundant. I would probably just divide the plane into a square grid of, say, 20 x 20, and save the set for each cell with all the creatures that are currently inside the cell. Then, for each creature, just look into your cell and 8 (or 5 on the sides or 3 at the corners) of neighboring cells and use a force that drops to zero within the same cell.

- , , . , , , , 8 , , .

+5

, 2D octree 3D. O(n log n) , log n n. , . , , , - . , -.

, , ( , - . - )

, . , . , . , .

O(n^2), O(n log n). , n-body.

+2

Source: https://habr.com/ru/post/1540591/


All Articles