Skip to content

Spatial Hash

The Spatial Hash algorithm is a performant non-physics based query system that returns a list of objects contained in a position and a certain radius.

Performance

This algorithm scales with the amount of objects tracked. Its performance shines the most when there are multiple queries launched in a single frame. For more information about how this algorithm works check this Twitter post:

https://twitter.com/catsoftstudios/status/1201520331724333058

Creating a Domain

The first thing needed is to create a world domain from where to track all objects and organize the space partitioning. We recommend setting up a static class that will handle registering all the changes that happen in the scene. For example:

public static class MySpatialHash
{
    public static SpatialHash Value { get; private set; } = new SpatialHash();

    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
    private static void OnSubsystemsInit()
    {
        Value = new SpatialHash();
    }
}

The previous code snippet initializes the Value property with the default SpatialHash constructor. The OnSubsystemsInit() is a method that gets called at the very beginning of starting the game, before any scene is loaded, thanks to its attribute.

Tracking Changes

Each object instance is responsible for registering itself in the domain. To do so, the object must be a Component that implements the ISpatialHash interface, as well as call the Insert() and Remove() methods to start and stop tracking it. For example:

public class MyComponent : MonoBehaviour, ISpatialHash
{
    void OnEnable() 
    {
        // Start tracking this object
        MySpatialHash.Value.Insert(this);
    }

    void OnDisable() 
    {
        // Stop tracking this object
        MySpatialHash.Value.Remove(this);
    }
}

The ISpatialHash interface already provides the Position (the position of the component's transform) and the UniqueCode (a unique identifier of the component's transform) properties, so there is nothing else to implement.

Moving objects

There is no need to notify the spatial hash when an object moves. Objects that are not marked as static are automatically updated before the next query, and only if their position has changed since the last time.

Requesting Collections

To request all the objects around a point and within a specific radius, use the Find(Vector3 point, float radius, List<ISpatialHash> results, ISpatialHash except = null) method. It clears and fills the results list with the objects contained in the specified region, sorted from the closest to the farthest one. The optional except parameter allows you to exclude one object from the results (for example, the object that is performing the query).

// Define a point and radius in the 3D space:
Vector3 point = new Vector3(0, 0, 0);
float radius = 10f;

// Request all tracked objects within the region:
List<ISpatialHash> list = new List<ISpatialHash>();
MySpatialHash.Value.Find(point, radius, list);

The list contains all components that implement the ISpatialHash interface tracked in this domain that are within the spherical region defined.

Reusing the list

Because the Find(...) method fills a list you provide, it doesn't allocate any memory. We recommend creating the list once and reusing it across multiple queries.