aevyrie / big_space

Floating origin plugin for spaces larger than the universe
Apache License 2.0
168 stars 22 forks source link

Spatial Hashing #31

Open aevyrie opened 4 months ago

aevyrie commented 4 months ago

Implements spatial hashing, to performantly run distance checks, find all entities located in the same cell, or neighboring cells.

Finding all entities in a GridCell is a single pre-hashed lookup, so it only takes ~ns. Finding all entities in a cell and its 26 neighbors takes only ~100ns. You can query for neighbors within any radius, but this becomes exponentially more expensive. This also provides a neighbors_flood to find all contiguous cells that have entities in them, recursively. This is useful to find islands of occupied cells, intended to be used for placing physics simulation instances. Neighbor flood is extremely dependent on the amount of objects and their arrangement in the scene, in this case it takes 1.6ms.

The numbers above come from the added benchmarks, running on a MPB M3 Max, with 10,000 entities moving around a cube of 50 cells^3.