- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
import Vector2d from "./../math/vector2.js";
import * as arrayUtil from "./../utils/array.js";
/**
* @import World from "./world.js";
* @import Container from "./../renderable/container.js";
* @import Bounds from "./bounds.js";
*/
/*
* A QuadTree implementation in JavaScript, a 2d spatial subdivision algorithm.
* Based on the QuadTree Library by Timo Hausmann and released under the MIT license
* https://github.com/timohausmann/quadtree-js/
**/
/**
* a pool of `QuadTree` objects
* @ignore
*/
let QT_ARRAY = [];
/**
* will pop a quadtree object from the array
* or create a new one if the array is empty
* @ignore
*/
function QT_ARRAY_POP(world, bounds, max_objects = 4, max_levels = 4, level = 0) {
if (QT_ARRAY.length > 0) {
let _qt = QT_ARRAY.pop();
_qt.world = world;
_qt.bounds = bounds;
_qt.max_objects = max_objects;
_qt.max_levels = max_levels;
_qt.level = level;
return _qt;
} else {
return new QuadTree(world, bounds, max_objects, max_levels, level);
}
}
/**
* Push back a quadtree back into the array
* @ignore
*/
function QT_ARRAY_PUSH(qt) {
QT_ARRAY.push(qt);
}
/**
* a temporary vector object to be reused
* @ignore
*/
let QT_VECTOR = new Vector2d();
/**
* @classdesc
* a QuadTree implementation in JavaScript, a 2d spatial subdivision algorithm.
* @see game.world.broadphase
*/
export default class QuadTree {
/**
* @param {World} world - the physic world this QuadTree belongs to
* @param {Bounds} bounds - bounds of the node
* @param {number} [max_objects=4] - max objects a node can hold before splitting into 4 subnodes
* @param {number} [max_levels=4] - total max levels inside root Quadtree
* @param {number} [level] - deepth level, required for subnodes
*/
constructor(world, bounds, max_objects = 4, max_levels = 4, level = 0) {
this.world = world;
this.bounds = bounds;
this.max_objects = max_objects;
this.max_levels = max_levels;
this.level = level;
this.objects = [];
this.nodes = [];
}
/*
* Split the node into 4 subnodes
*/
split() {
let nextLevel = this.level + 1,
subWidth = this.bounds.width / 2,
subHeight = this.bounds.height / 2,
left = this.bounds.left,
top = this.bounds.top;
//top right node
this.nodes[0] = QT_ARRAY_POP(
this.world,
{
left : left + subWidth,
top : top,
width : subWidth,
height : subHeight
},
this.max_objects,
this.max_levels,
nextLevel
);
//top left node
this.nodes[1] = QT_ARRAY_POP(
this.world,
{
left : left,
top: top,
width : subWidth,
height : subHeight
},
this.max_objects,
this.max_levels,
nextLevel
);
//bottom left node
this.nodes[2] = QT_ARRAY_POP(
this.world,
{
left : left,
top : top + subHeight,
width : subWidth,
height : subHeight
},
this.max_objects,
this.max_levels,
nextLevel
);
//bottom right node
this.nodes[3] = QT_ARRAY_POP(
this.world,
{
left : left + subWidth,
top : top + subHeight,
width : subWidth,
height : subHeight
},
this.max_objects,
this.max_levels,
nextLevel
);
}
/*
* Determine which node the object belongs to
* @param {Rect} rect bounds of the area to be checked
* @returns Integer index of the subnode (0-3), or -1 if rect cannot completely fit within a subnode and is part of the parent node
*/
getIndex(item) {
let pos;
let bounds = item.getBounds();
// use game world coordinates for floating items
if (item.isFloating === true) {
pos = this.world.app.viewport.localToWorld(bounds.left, bounds.top, QT_VECTOR);
} else {
pos = QT_VECTOR.set(item.left, item.top);
}
let index = -1,
rx = pos.x,
ry = pos.y,
rw = bounds.width,
rh = bounds.height,
verticalMidpoint = this.bounds.left + (this.bounds.width / 2),
horizontalMidpoint = this.bounds.top + (this.bounds.height / 2),
//rect can completely fit within the top quadrants
topQuadrant = (ry < horizontalMidpoint && ry + rh < horizontalMidpoint),
//rect can completely fit within the bottom quadrants
bottomQuadrant = (ry > horizontalMidpoint);
//rect can completely fit within the left quadrants
if (rx < verticalMidpoint && rx + rw < verticalMidpoint) {
if (topQuadrant) {
index = 1;
} else if (bottomQuadrant) {
index = 2;
}
} else if (rx > verticalMidpoint) {
//rect can completely fit within the right quadrants
if (topQuadrant) {
index = 0;
} else if (bottomQuadrant) {
index = 3;
}
}
return index;
}
/**
* Insert the given object container into the node.
* @name insertContainer
* @memberof QuadTree
* @param {Container} container - group of objects to be added
*/
insertContainer(container) {
const children = container.children;
const childrenLength = children.length;
for (let i = childrenLength, child; i--, (child = children[i]);) {
if (child.isKinematic !== true) {
if (typeof child.addChild === "function") {
if (child.name !== "rootContainer") {
this.insert(child);
}
// recursivly insert all childs
this.insertContainer(child);
} else {
// only insert object with a bounding box
// Probably redundant with `isKinematic`
if (typeof (child.getBounds) === "function") {
this.insert(child);
}
}
}
}
}
/**
* Insert the given object into the node. If the node
* exceeds the capacity, it will split and add all
* objects to their corresponding subnodes.
* @name insert
* @memberof QuadTree
* @param {object} item - object to be added
*/
insert(item) {
let index = -1;
//if we have subnodes ...
if (this.nodes.length > 0) {
index = this.getIndex(item);
if (index !== -1) {
this.nodes[index].insert(item);
return;
}
}
this.objects.push(item);
if (this.objects.length > this.max_objects && this.level < this.max_levels) {
//split if we don't already have subnodes
if (this.nodes.length === 0) {
this.split();
}
let i = 0;
//add all objects to there corresponding subnodes
while (i < this.objects.length) {
index = this.getIndex(this.objects[i]);
if (index !== -1) {
this.nodes[index].insert(this.objects.splice(i, 1)[0]);
} else {
i = i + 1;
}
}
}
}
/**
* Return all objects that could collide with the given object
* @name retrieve
* @memberof QuadTree
* @param {object} item - object to be checked against
* @param {object} [fn] - a sorting function for the returned array
* @returns {object[]} array with all detected objects
*/
retrieve(item, fn) {
let returnObjects = this.objects;
//if we have subnodes ...
if (this.nodes.length > 0) {
let index = this.getIndex(item);
//if rect fits into a subnode ..
if (index !== -1) {
returnObjects = returnObjects.concat(this.nodes[index].retrieve(item));
} else {
//if rect does not fit into a subnode, check it against all subnodes
for (let i = 0; i < this.nodes.length; i = i + 1) {
returnObjects = returnObjects.concat(this.nodes[i].retrieve(item));
}
}
}
if (typeof(fn) === "function") {
returnObjects.sort(fn);
}
return returnObjects;
}
/**
* Remove the given item from the quadtree.
* (this function won't recalculate the impacted node)
* @name remove
* @memberof QuadTree
* @param {object} item - object to be removed
* @returns {boolean} true if the item was found and removed.
*/
remove(item) {
let found = false;
if (typeof (item.getBounds) === "undefined") {
// ignore object that cannot be added in the first place
return false;
}
//if we have subnodes ...
if (this.nodes.length > 0) {
// determine to which node the item belongs to
let index = this.getIndex(item);
if (index !== -1) {
found = arrayUtil.remove(this.nodes[index], item);
// trim node if empty
if (found && this.nodes[index].isPrunable()) {
this.nodes.splice(index, 1);
}
}
}
if (found === false) {
// try and remove the item from the list of items in this node
if (this.objects.indexOf(item) !== -1) {
arrayUtil.remove(this.objects, item);
found = true;
}
}
return found;
}
/**
* return true if the node is prunable
* @name isPrunable
* @memberof QuadTree
* @returns {boolean} true if the node is prunable
*/
isPrunable() {
return !(this.hasChildren() || (this.objects.length > 0));
}
/**
* return true if the node has any children
* @name hasChildren
* @memberof QuadTree
* @returns {boolean} true if the node has any children
*/
hasChildren() {
for (let i = 0; i < this.nodes.length; i = i + 1) {
const subnode = this.nodes[i];
if (subnode.length > 0 || subnode.objects.length > 0) {
return true;
}
}
return false;
}
/**
* clear the quadtree
* @name clear
* @memberof QuadTree
* @param {Bounds} [bounds=this.bounds] - the bounds to be cleared
*/
clear(bounds) {
this.objects.length = 0;
for (let i = 0; i < this.nodes.length; i++) {
this.nodes[i].clear();
// recycle the quadTree object
QT_ARRAY_PUSH(this.nodes[i]);
}
// empty the array
this.nodes.length = 0;
// resize the root bounds if required
if (typeof bounds !== "undefined") {
this.bounds.setMinMax(bounds.min.x, bounds.min.y, bounds.max.x, bounds.max.y);
}
}
}