Hierachical Spatial Hash Grid, original source code from Andrew Pertsen's Gist.

Usage no npm install needed!

<script type="module">
  import hexrHshg from 'https://cdn.skypack.dev/hexr-hshg';



Hierachical Spatial Hash Grid, original source code from Andrew Pertsen's Gist.

This is a hacked, modified version which forms the broad-phase collision detection part of "hexr-shit-physics" which is an incredibly shit arcade physics system that barely works, but it'll be coming to an npm repository near you soon. Sorry, world.

I really haven't put too much thought into whether this is remotely performant or even if it's even vaguely the correct solution for the particular problem I'm trying to solve. But... here it is.

Hack One:

It now accepts objects that have a body which contains a structure called AABB that contains two arrays, min and max representing the bounding box, and a boolean 'static' flag.

  body : {
    AABB : {
      min: [minx, miny],
      max: [maxy, maxy]
    static : boolean

Interesting technical note: In a WebGL world where +y is upwards, you'll want your 'min' co-ords to be the bottom left and the 'max' co-ords to be the top right. Just telling you this to spare you the many hours of pain this caused me.

Other interesting technical note: The original HSHG is already set up for you to override this stuff without needing to hack at the actual library but it was the middle of a Ludum Dare competition and didn't notice.

Hack Two:

Objects flagged as static will never show as being in collision with each other.

 Hack Three:

Long forgotten things that are long forgotten.


var HSHG = require('hexr-hshg')

var shitGrid = new HSHG();

shitGrid.addObject( someObjectWithThePreviouslyMentionedBodyAndBoundingBox )
shitGrid.addObject( someOtherObjectThatAlsoHasThatStuff )

// Lots of exciting things happen here, including a change to the bounding box for one of those objects!

// get the grid to make sure it's all up to date..

// right then. Are any of these bounding boxes overlapping? 
var collisionPairs = shitGrid.getCollisionPairs();

I'll leave what happened to collisionPairs as a sort of exciting dramatic cliffhanger for another day.

Actually Using This Thing

It sort of makes sense, if you're using this in a game, to pass that list of collisions around to all entities that are doing any sort of collision detection so that they've got a sigificantly shorter list of things to be interested in.

At least, that's how I've used it.


I have no idea. Nothign on the original Gist so let's say MIT and hope that no-one gets angry.


You probably shouldn't.