Currently I have many entities that are treated as points in space. I want to find all the distances between any two arbitrary groups of them. For example, n soldiers and m enemies, resulting in n * m simple distance calculations. However, this is not optimal. A possible alternative is to attach a reasonably small sphere collider to each relevant LocalToWorld-owning entity, and have Unity.Physics do the broadphase calculations to efficiently compute the distances between each relevant entity with OverlapAABB. Is this more or less efficient then simply calculating the distances between each group of entities?
Are groups defined with some lifetime or change every frame? What’s the scale on this? How many units/groups? What is the distance actually used for? What accuracy is required?
Amazing, I am just making this consideration myself.
I have a lot of volumes I want to check for overlapping colliders (to pick targets for gun turrets) and I currently use Overlap AABB in a parallel job every tick, that will probably get some load balancing to do it less often later when I figure out how to do that without ruining my chunk layout with Shared Component Data.
I was considering simplifying it to spherical n^2 tests because all of these entities are already in similar or same chunks thewthe my physics instancing is designed.
Given that a distance check is a dot product, I am sure that’s cheaper than an AABB test.
However, AABB has the advantage of treating elongated compound colliders better, but the disadvantage of being a box metric, not a sphere distance metric.
If you really need to optimize this, use a search tree or 2d/space3d partitioning (e.g. octrees) depending onwon your plan is.
A sparse octree is probably harder to get right in light of cache locality than just brute forcing it, though. Different story for dense octrees.
The groups are defined by archetype, and may lose or gain members over time. Scale is in the 50s to 100s. If, for example, an enemy is close enough to a soldier, the soldier will add the enemy to a EnemyWithinRangeComponent DynamicBuffer. It doesn’t have to be accurate.
You basically want a proximity test? Sound like a voronoi/delaunay problem.
Overengenering aside, you should just implement a test case and profile on the main target hardware. There is no other optimal solution.
Just create a test project have only points gameobject, then test various sweeps test.