QuadTree
A QuadTree implementation. The original code was a conversion of the Java code posted to GameDevTuts. However I've tweaked it massively to add node indexing, removed lots of temp. var creation and significantly increased performance as a result. Original version at https://github.com/timohausmann/quadtree-js/
Instance Members
A flag used to know if this quad tree is currently busy and should not be used by something else.
The maximum number of objects per node.
The maximum number of levels to break down to.
The current level.
The x position of the quadtree bounds.
The y position of the quadtree bounds.
The width of the quadtree bounds.
The height of the quadtree bounds.
The sub-width of the quadtree bounds.
The sub-height of the quadtree bounds.
The right edge of the quadtree bounds.
The bottom edge of the quadtree bounds.
Array of quadtree children.
Array of associated child nodes.
reset(x: Float, y: Float, width: Float, height: Float, ?maxObjects: Int = 10, ?maxLevels: Int = 4, ?level: Int = 0): Void
Resets the QuadTree.
Name | Type | Default | Description |
---|---|---|---|
x |
Float | The top left coordinate of the quadtree. | |
y |
Float | The top left coordinate of the quadtree. | |
width |
Float | The width of the quadtree in pixels. | |
height |
Float | The height of the quadtree in pixels. | |
maxObjects |
Int | 10 |
The maximum number of objects per node. |
maxLevels |
Int | 4 |
The maximum number of levels to iterate to. |
level |
Int | 0 |
Which level is this? |
Populates this quadtree with the children of the given Group. In order to be added the child must exist and have a body property.
Name | Type | Description |
---|---|---|
group |
Group | The Group to add to the quadtree. |
Split the node into 4 subnodes
Insert the object into the node. If the node exceeds the capacity, it will split and add all objects to their corresponding subnodes.
Name | Type | Description |
---|---|---|
body |
Body | The Body object to insert into the quadtree. |
Determine which node the object belongs to.
Name | Type | Description |
---|---|---|
left |
Float | The left edge of the bounds to check. |
top |
Float | The top edge of the bounds to check. |
right |
Float | The right edge of the bounds to check. |
bottom |
Float | The bottom edge of the bounds to check. |
Returns | Description |
---|---|
Int | Index of the subnode (0-3), or -1 if rect cannot completely fit within a subnode and is part of the parent node. |
Return all objects that could collide with the given bounds.
Name | Type | Description |
---|---|---|
left |
Float | The left edge of the bounds to check. |
top |
Float | The top edge of the bounds to check. |
right |
Float | The right edge of the bounds to check. |
bottom |
Float | The bottom edge of the bounds to check. |
Returns | Description |
---|---|
Array<Body> | Array with all detected objects. |
Clear the quadtree.
new(?pool: QuadTreePool, x: Float, y: Float, width: Float, height: Float, ?maxObjects: Int = 10, ?maxLevels: Int = 4, ?level: Int = 0): Void
Name | Type | Default |
---|---|---|
pool |
QuadTreePool | (optional) |
x |
Float | |
y |
Float | |
width |
Float | |
height |
Float | |
maxObjects |
Int | 10 |
maxLevels |
Int | 4 |
level |
Int | 0 |