1.2.2 • Published 4 years ago

superquad v1.2.2

Weekly downloads
8
License
MIT
Repository
github
Last release
4 years ago

NPM version Known Vulnerabilities npm NPM downloads Gitter

Install

Superquad is offered as both a Node package and an ES6 module. To download Superquad through npm, use:

$ npm install superquad

and to use it, you can either require it or import it if you're in a browser environment:

In Node.js:

const Superquad = require('superquad');

In a browser environment:

// Browser
import Superquad from 'node_modules/superquad/superquad.js';

// Webpack
import Superquad from 'superquad';

Usage

To create a new instance of Superquad, you have to supply at minimum the width and height of the quadtree:

const dimensions = {
  width: 1024,
  height: 768
};

const superquad = new Superquad(dimensions);

Now let's say you want to customize Superquad further and adjust the number of objects a quad can hold before it splits:

const dimensions = {
  width: 1024,
  height: 768
};

const options = {
  maxObjects: 20
};

const superquad = new Superquad(dimensions, options);

The complete layout of initialization parameters is as follows:

paramtypedescriptiondefault
boundsObject
bounds.xnumberThe x position of the top left point of the quad. This should only be set if you're working with negative position values.0
bounds.ynumberThe y position of the top left point of the quad. This should only be set if you're working with negative position values.0
bounds.widthnumberThe width of the quadtree.
bounds.heightnumberThe height of the quadtree.
options
options.maxObjectsnumberThe amount of objects a quad can hold before it splits into 4 sub-quads10
options.maxLevelsnumberThe number of sub-quads a quad can have.4

API

add

Adds an object to the quadtree by specifying the space that it occupies.

paramtypedescriptiondefault
boundsObjectThe bounds of the object to add
bounds.xnumberThe x position of the object
bounds.ynumberThe y position of the object
bounds.widthnumberThe width of the object
bounds.heightnumberThe height of the object

So to add an object to the quadtree at (150, 200) with a width of 50 and a height of 75, you would do:

superquad.add({ x: 150, y: 200, width: 50, height: 75 });

Now because it takes an object of bounds specification, if your game objects automatically include this information you can just add the whole game object. However, when retrieving objects from the quadtree all of the extra data will not be present.

get

Gets any objects that are in the same sub-quad as the provided bounds. These are not guaranteed to be collisions so you will have to check for collisions manually.

paramtypedescriptiondefault
boundsObjectThe bounds to check for nearby objects
bounds.xnumberThe x position of the bounds
bounds.ynumberThe y position of the bounds
bounds.widthnumberThe width of the bounds
bounds.heightnumberThe height of the bounds
const possibleCollisions = superquad.get({ x: 5, y: 10, width: 50, height: 50 });

getIntersections

Gets any objects whose bounds intersect with the bounds provided. This does return objects that collide with the provided bounds but if your object is circular this is not very accurate as it checks for rectangular collision only.

paramtypedescriptiondefault
boundsObjectThe bounds to check for nearby objects
bounds.xnumberThe x position of the bounds
bounds.ynumberThe y position of the bounds
bounds.widthnumberThe width of the bounds
bounds.heightnumberThe height of the bounds
const intersections = superquad.getIntersections({ x: 5, y: 10, width: 50, height: 50 });

getPoints

Works in a similar fashion to getIntersections but instead it returns objects that collide with the bounds only if the objects are points (no width and no height).

paramtypedescriptiondefault
boundsObjectThe bounds to check for nearby points
bounds.xnumberThe x position of the bounds
bounds.ynumberThe y position of the bounds
bounds.widthnumberThe width of the bounds
bounds.heightnumberThe height of the bounds
const points = superquad.getPoints({ x: 5, y: 10, width: 50, height: 50 });

clear

Clears all objects from the each quad in the quadtree.

superquad.clear();

Acknowledgements

This package is based on Timo Hausmann Quadtree implementation and also JamesMilnerUK's Go Quadtree implementation.

License

MIT

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.1.5

4 years ago

0.1.1

5 years ago