spatial-hashmap

1.0.6 • Public • Published

Spatial Hashmap

CircleCI

Spatial Hashmap is a module for answering the question "What is near an object with these coordinates?". It was created based off the algorithm defined in "Optimization of Large-Scale, Real-Time Simulations by Spatial Hashing"

Install

npm i spatial-hashmap

Usage

// pass in the width, height, and cell size
// cell size - use this to control how big a region is returned when asking for nearby objects
const sm = new SpatialManager(100, 100, 10)
// this can be literally anything
const yourObject = {your: 'object'}
// pass in a geometry with this shape
const geometry = {
  pos: {
    x: 0,
    y: 0
  },
  aabb: {
    min: {
      x: 0,
      y: 0
    }
    max: {
      x: 12,
      y: 10
    }
  }
}
sm.registerObject(yourObject, geometry)
sm.registerObject({your: 'otherObject'}, ...)

// returns Set of nearby objects
sm.getNearby(geometryOfOtherObject)
sm.clearMap()

API

clearMap()

Empties Spatial Hashmap and reinitializes.

registerObject(obj, geo)

How you get your obj into the map

@param obj - what you want to register in the spatial hashmap

@param geo - a description of the objects position and bounding box

{
  // position of the object
  pos: {
    x: 0,
    y: 0
  },
  // bounding box relative to position
  aabb: {
    min: {
      x: 0,
      y: 0
    }
    max: {
      x: 12,
      y: 10
    }
  }
}

getIdsForGeometry(geo)

Returns all the possible buckets an object's geometry is in as a set

SpatialManager.idForPoint(point, cellsizeInv, numCols) (static)

Given a Point return its bucket ID

@param cellsizeInv - 1 / cellsize

@param numcols - the number of cols in the spatial map

spatialManager.idForPoint(point) (instance)

Given a Point return its bucket ID

getNearby(geo)

Given a geometry return the objects nearby

Package Sidebar

Install

npm i spatial-hashmap

Weekly Downloads

2

Version

1.0.6

License

MIT

Unpacked Size

33.3 kB

Total Files

16

Last publish

Collaborators

  • derduher