If you experience problems please raise an issue and use v1 in the meantime.
- Add integrity hash to script tags
- Update and publish jsdoc/tsdoc
- Add explanations to primitives example
- Publish to npm
- Add module to retrieve nodes and objects
- Add module to update objects
This Javascript Quadtree Library can store and retrieve Rectangles, Circles and Lines in a recursive 2D Quadtree. Every Quadtree node can hold a maximum number of objects before it splits into four subnodes. Objects are only stored on leaf nodes (the lowest level). If an object overlaps into multiple leaf nodes, a reference to the object is stored in each node.
Only XXX Bytes! (Compressed + Gzipped)
The code was initially based on the Java Methods described on gamedevelopment.tutsplus.com by Steven Lambert and underwent some revisions since.
Many games require the use of collision detection algorithms to determine when two objects have collided, but these algorithms are often expensive operations and can greatly slow down a game. One way to speed things up is to reduce the number of checks that have to be made. Two objects that are at opposite ends of the screen can not possibly collide, so there is no need to check for a collision between them. This is where a quadtree comes into play.
- Simple Demo β add static objects and see the Quadtree split
- Dynamic Demo β continuously track moving objects
- Many to many Demo β check all objects against each other
- Benchmark v1.2 - Performance test with 10.000 objects
- Benchmark v1.1.3 - Performance test with 10.000 objects (old implementation)
Install this module via npm and import or require it:
npm i -D @timohausmann/quadtree-js
// ES6
import { Quadtree } from '@timohausmann/quadtree-js';
// CommonJS
const { Quadtree } = require('@timohausmann/quadtree-js');
Alternatively, download the source and include it the old-fashioned way, or use an awesome CDN like jsdelivr or unpkg. (If you only need Rectangles and want to save some bytes, use quadtree.umd.basic.js
instead):
<!-- self-hosted -->
<script src="quadtree.umd.full.js"></script>
<!-- CDN jsdelivr -->
<script src="https://cdn.jsdelivr.net/npm/@timohausmann/quadtree-js/dist/quadtree.umd.full.js"></script>
<!-- CDN unpkg -->
<script src="https://unpkg.com/@timohausmann/quadtree-js/dist/quadtree.umd.full.js"></script>
Create a new Quadtree:
const myTree = new Quadtree({
width: 800,
height: 600
});
Optional properties:
maxObjects
β defines how many objects a node can hold before it splitsmaxLevels
β defines the deepest level subnodex
andy
β coordinate offset
I recommend using low values for maxLevels
because each level will quadruple the possible amount of nodes. Using lower values for maxLevels
increases performance but may return more candidates. Finetuning these values depends on your 2D space, the amount and size of the objects and your retrieving areas.
const myTree = new Quadtree({
width: 600,
height: 400,
x: 100, // default: 0
y: 100, // default: 0
maxObjects: 15, // default: 10
maxLevels: 3 // default: 4
});
Insert an element in the Quadtree (default: Rectangle)
myTree.insert({
x: 100,
y: 100,
width: 100,
height: 100
});
Retrieve elements from nodes that intersect with the given bounds
const elements = myTree.retrieve({
x: 150,
y: 150,
width: 100,
height: 100
});
Clear the Quadtree
myTree.clear();
You can use any object directly with the Quadtree. There are a few primitive shapes available. Just specify what shape they are with qtShape
(default: Rectangle
). Each shape has required properties specific to their geometry.
Shape | Required Properties |
---|---|
Rectangle | x, y, width, height |
Circle | x, y, r |
Line | x1, y1, x2, y2 |
import { Rectangle, Circle, Line } from '@timohausmann/quadtree-js';
const player = {
qtShape: Rectangle,
name: 'Giana',
x: 100,
y: 0,
width: 24,
height: 48,
};
const explosion = {
qtShape: Circle,
damage: 100,
x: 50,
y: 50,
r: 100,
};
const Laser = {
qtShape: Line,
color: 'green',
x1: 50,
y1: 50,
x2: 100,
y2: 100,
};
These shapes are actually classes! You can also use them directly or extend them. Store custom data on the data
property.
import { Rectangle, Circle, Line } from '@timohausmann/quadtree-js';
// Class usage
const rectangle = new Rectangle({ x: 67, y: 67, width: 100, height: 100 });
const circle = new Circle({ x: 128, y: 128, r: 50, data: 'custom data here' });
const line = new Line({ x1: 67, y1: 67, x2: 128, y2: 128, data: { foo: bar } });
myTree.insert(rectangle);
myTree.insert(circle);
myTree.insert(line);
const area = new Rectangle({ x: 50, y: 50, width: 50, height: 50 });
const elements = myTree.retrieve(area);
// Class extend
class Laser extends Line {
constructor(props) {
super(props);
this.color = 'green';
}
draw(ctx) {
ctx.beginPath();
ctx.moveTo(this.x1, this.y1);
ctx.lineTo(this.x2, this.y2);
ctx.strokeStyle = this.color;
ctx.stroke();
}
}
const laser = new Laser({x1: 100, y1: 0, x2: 100, y2: 100});
myTree.insert(laser);
If you are using the UMD bundles, the classes are available as Quadtree.Rectangle
, Quadtree.Circle
and Quadtree.Line
.
Check out the examples for more information.
When using classes, you can easily tell TS the shape of your custom data with <T>
:
interface GameEntity {
name: string
health: number
}
const hero = new Rectangle<GameEntity>({
x: 100,
y: 100,
width: 24,
height: 48,
data: {
name: 'Shiffman',
health: 100,
}
})
As of 2.0.0 the UMD bundles use ES6 features (e.g. classes) that are not supported by IE11 and below. For legacy browser support, please download a 1.x version of this library or bundle and polyfill it on your own.
npm run dev
to watch and build the sourcenpm run build
execute rollup, docs, dtsnpm run rollup
to build the source onlynpm run test
to run the test suitenpm run lint
to run eslintnpm run docs
to create docsnpm run dts
to create definition files
- Named exports only: π Change
import Quadtree ...
toimport { Quadtree } ...
- Quadtree constructor:
maxObjects
andmaxLevels
are now named properties. Also,x
andy
are now optional. π Changenew Quadtree({x: 0, y: 0, width: 100, height: 100}, 5, 2);
tonew Quadtree({width: 100, height: 100, maxObjects: 5, maxLevels: 2});
- Bundle filename has changed. π Update your script tag from
quadtree.min.js
toquadtree.umd.basic.js
- Typescript: no more
Rect
interface. π useRectangle
instead
- Refactored Codebase to ES6 and Typescript
- Added modular classes for Rectangle, Circle, Line
- Added dedicated bundle files for CJS, EMS and UMD
- Added Unit Tests with Jest
- Added ESLint
- Added API docs with Typedoc
- Typescript Definition File Bugfix (thanks to pietrovismara)
- Added definition files for Typescript support
- JSDoc Fixes
- Using github.io for examples (docs)
- CDN URLs
- Removed
grunt
dev dependency, now usinguglify-js
to minifiy
- Allow float boundaries for Quads
- Simplified getIndex function
This implementation now stores objects exclusively on leaf nodes and thus differs from the tutorial it's based on. Objects, that overlap into multiple subnodes are now referenced in each matching subnode instead of their parent node. This drastically reduces the collision candidates. Prior to 1.2.0, overlapping objects were stored in parent nodes.
- Support for npm and
module.exports
There is a (currently deprecated) quadtree-js hitman branch available that allows you to update and remove single objects. This may be handy when most of the objects in your Quadtree are static. Please raise an issue if you want to see this feature maintained in future releases.