npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hshg

v1.0.3

Published

1D, 2D, and 3D Hierarchical Spatial Hash Grid

Downloads

10

Readme

const { HSHG_2D } = require("./hshg");

To create a HSHG, you need to know the number of cells you want to use, size of a cell, and the maximum number of entities you are planning on inserting.

The number of cells and the cell size must both be powers of 2. You needn't be concerned however, as no operation scales with the number of cells. If you require 50x50 cells, just go for 64x64.

You won't be able to change any of these initialization values afterwards.

/* in 2D, the total number of cells is then 32 * 32 */
const cells_on_edge = 32;
const cell_size = 32;
const max_entities = 1000;

const engine = await new HSHG_2D(cells_on_edge, cell_size, max_entities);

/* or */

new HSHG_2D(cells_on_edge, cell_size, max_entities).then(function(engine) {
    /* ... */
});

Now that you have that, your next goal should be to decide what you will want to do upon updating, colliding, or querying. You can do that like so:

engine.update = function(entity) {
    /* reported once per entity */
};

engine.collide = function(ent_a, ent_b) {
    /* reported once per collision */
};

engine.query = function(entity) {
    /* reported once per entity in searched area */
};

At the same time, you can begin inserting some entities. Note that since we are in 2D, you will need x and y coordinates. In 1D, you will only need x, and in 3D, you will also need z.

The entities you insert are stored internally. You do not have access to them in any other way than via the engine calls. For instance, if you want to go through all of them, you can just call engine.update(). You can modify the callbacks you set before as many times as you like.

The entities you insert must meet certain requirements. Namely, they must have fields called x, y, and r (for 2D).

class Ball {
    x = 2;
    y = 3;
    r = 1;

    constructor(){}
}

engine.insert(new Ball());

Now this ball that was just inserted will be reported in engine.update() once it's called.

If you wish to remove an entity, you need to do it explicitly from the engine.update callback like so:

engine.update = function(entity) {
    if(entity.designated_to_be_deleted) {
        return this.remove(); /* or engine.remove() */
    }

    /* else, do other fun stuff */

    entity.x += entity.vx;
    entity.y += entity.vy;

    if(entity.x - entity.r < 0) {
        entity.designated_to_be_deleted = true;
    }
};

Any changes you make to entity.x, entity.y, or entity.r, will be propagated to the underlying WASM module.

engine.collide() simply detects collision. No collision will ever be detected twice. If A and B collide, this fact will only be reported via one call to engine.collide, with A and B being in any order in the arguments, so it could be either A B or B A, it doesn't matter.

Note that this provides broad collision detection - the items might not be colliding. In fact, their bounding boxes might not even be in contact. It's up to you to check.

engine.query(min_x, min_y, max_x, max_y) searches the specified area and calls engine.query on every found entity. It will only be called once per entity. Any entities intersecting the queried area will be included, not just those which are entirely inside.

You can access engine.mem to see roughly how many bytes of memory it has allocated for itself.

If you want to learn more, check out the source repository.