UNPKG

6.24 kBMarkdownView Raw
1# quadtree-js
2
3This is a JavaScript Quadtree implementation based on the Java Methods described on [gamedevelopment.tutsplus.com by Steven Lambert](https://gamedevelopment.tutsplus.com/tutorials/quick-tip-use-quadtrees-to-detect-likely-collisions-in-2d-space--gamedev-374):
4
5> 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.
6
7This implementation can store and retrieve rectangles 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.
8
9*Only 639 Bytes! (Compressed + Gzipped)*
10
11
12----
13
14### checkout "2.0": [quadtree-ts](https://github.com/timohausmann/quadtree-ts/) with multiple primitives, Typescript and more.
15
16----
17
18## Demos
19
20* [Simple Demo](https://timohausmann.github.io/quadtree-js/simple.html) – add static objects and see the Quadtree split
21* [Dynamic Demo](https://timohausmann.github.io/quadtree-js/dynamic.html) – continuously track moving objects
22* [Many to many Demo](https://timohausmann.github.io/quadtree-js/many.html) – check all objects against each other
23* [Benchmark v1.2.6](https://timohausmann.github.io/quadtree-js/test-retrieve.html) - Performance test with 1.000.000 objects
24
25## Install
26
27Install this module via [npm](https://www.npmjs.com/package/@timohausmann/quadtree-js) and import or require it:
28
29```bash
30npm i -D @timohausmann/quadtree-js
31```
32
33```javascript
34import Quadtree from '@timohausmann/quadtree-js';
35```
36
37```javascript
38const Quadtree = require('@timohausmann/quadtree-js');
39```
40
41Alternatively, [download the source](https://github.com/timohausmann/quadtree-js/archive/master.zip) and include it the old-fashioned way:
42
43```html
44<script src="quadtree.min.js"></script>
45```
46
47Or use an awesome CDN like [jsdelivr](https://www.jsdelivr.com/package/npm/@timohausmann/quadtree-js) or [unpkg](https://unpkg.com/browse/@timohausmann/quadtree-js@latest/):
48
49```html
50<script src="https://cdn.jsdelivr.net/npm/@timohausmann/quadtree-js/quadtree.min.js"></script>
51```
52
53```html
54<script src="https://unpkg.com/@timohausmann/quadtree-js/quadtree.min.js"></script>
55```
56
57
58## How to use
59
60Create a new Quadtree (with default values for `max_objects` (10) and `max_levels` (4)).
61
62```javascript
63var myTree = new Quadtree({
64 x: 0,
65 y: 0,
66 width: 400,
67 height: 300
68});
69```
70
71> MAX_OBJECTS defines how many objects a node can hold before it splits and MAX_LEVELS defines the deepest level subnode.
72
73If you want to specify `max_objects` and `max_levels` on your own, you can pass them as a 2nd and 3rd argument. I recommend using low values for `max_levels` because each level will quadruple the possible amount of nodes. Using lower values for `max_levels` 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.
74
75```javascript
76var myTree = new Quadtree({
77 x: 0,
78 y: 0,
79 width: 800,
80 height: 600
81}, 15, 6);
82```
83
84Insert an element in the Quadtree
85```javascript
86myTree.insert({
87 x: 100,
88 y: 100,
89 width: 100,
90 height: 100
91});
92```
93
94Retrieve elements from nodes that intersect with the given bounds
95```javascript
96var elements = myTree.retrieve({
97 x: 150,
98 y: 150,
99 width: 100,
100 height: 100
101});
102```
103
104Clear the Quadtree
105```javascript
106myTree.clear();
107```
108
109Check out the examples for more information.
110
111## Typescript
112
113Type definitions are included. Inserted objects need to conform to the `Quadtree.Rect` interface.
114
115```javascript
116import Quadtree, { Rect } from '@timohausmann/quadtree-js';
117
118/*
119 * interface Rect {
120 * x: number
121 * y: number
122 * width: number
123 * height: number
124 * }
125 */
126
127interface Player extends Rect {
128 name: string;
129 health: number;
130}
131
132const hero:Player = {
133 name: 'Shiffman',
134 health: 100,
135 x: 100,
136 y: 100,
137 width: 32,
138 height: 32
139}
140
141myTree.insert(hero);
142```
143
144## Update single objects
145
146This was often requested and is now supported in [quadtree-ts](https://timohausmann.github.io/quadtree-ts/examples/update/).
147This might be handy when most of the objects in your Quadtree are static.
148
149
150## Browser Support
151
152This library is supported in all modern browsers and runtimes.
1532023 Update: now using ES6 (`new Set()`) which breaks IE9 compatibility. For IE9 support, use [1.2.5](https://github.com/timohausmann/quadtree-js/releases/tag/1.2.5).
154
155## Development scripts
156
157* `npm run build` to minify the source
158
159## Changelog
160
161### 1.2.6
162
163* Performance improvement of `retrieve()` (was O(n^2), now O(n)) (thanks to [xixileng](https://github.com/timohausmann/quadtree-ts/pull/8)) Pushing the retrieval on a tree with 1.000.000 objects from ~160ms to ~5ms (MacBook M1 Pro).
164* New example: <a href="https://timohausmann.github.io/quadtree-js/test-retrieve.html" target="_blank">test-retrieve</a>
165* Local copy of `quadtree.min.js` for the docs folder so it's always up to date
166
167### 1.2.5
168
169Typescript Definition File Bugfix (thanks to [pietrovismara](https://github.com/timohausmann/quadtree-js/pull/18))
170
171### 1.2.4
172
173Added definition files for Typescript support
174
175JSDoc Fixes
176
177### 1.2.3
178
179Using github.io for examples (docs), CDN URLs
180
181### 1.2.2
182
183Removed `grunt` dev dependency, now using `uglify-js` to minifiy
184
185### 1.2.1
186
187Allow float boundaries for Quads
188
189Simplified getIndex function
190
191### 1.2.0
192
193This 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.
194
195### 1.1.3
196
197Support for npm and `module.exports`
\No newline at end of file