QT
A Quadtree implementation using JavaScript
Check out the demo here
Getting Started
Clone this repository or download it as a zip file.
Installation
npm
is required to install the library. Get it here
npm init
npm install qt-js
To use the modules with vanilla HTML/JavaScript install browserify
and/or watchify
to bundle your JavaScript codes along with the modules.
npm install -g browserify
npm install -g watchify
Use require
to include the modules in your JavaScript.
const Geom QT Collision = ;
To create the bundle, use the command
browserify scripts/logic.js -o scripts/logic-bundle.js
or if you want the codes to be bundled whenever you save
watchify scripts/logic.js -o scripts/logic-bundle.js
use a <script>
tag to include your new bundle
<script src="scripts/logic-bundle.js"></script>
Modules
GEOM
The Geom module allows the creation of new geoms (point, box and circ).
The module also contains helpers to calculate the distance between points, the angle of a vector and split a vector into X and Y components.
point(x, y)
create a new point
let point = 0 0; //point.x is 0;//point.y is 0;
box(x, y, width, height)
create a new box
let box = 0 0 50 25; //box.x = 0;//box.y = 0;//box.width = 50;//box.height = 25;
circ(x, y, radius)
create a new circle
let circle = 0 0 25; //circle.x = 0;//circle.y = 0;//circle.radius = 25;//circle.width = 50;//circle.height = 50;
dist2Points(x1, y1, x2, y2)
calculate distance between two points
let distance = Geom; //distance = 14.142135623730951
angle(xLen, yLen)
calculates the angle (degrees) of a vector
let pt1 = x: 0 y: 0 pt2 = x: 10 y: 10; let xLen = pt2x - pt1x;let yLen = pt2y - pt1y; let angle = Geom; //angle = 45
vector(magnitude, angle)
breaks a vector into X and Y components
let vector = Geom; //vector.x = 10;// vector.y = 10;
QT
The QT module allows the creation of quadtrees for spatial partitioning.
This module contains methods to insert or delete a rectangle from a tree and retrieve a list of possible objects it can collide with.
PROPERTIES
maxChildren
the maximum objects a node can contain before splitting
quadTree.maxChildren = 15;
maxLevel
the maximum number of times that a node can be split
quadTree.maxLevel = 15;
METHODS
node(level, bounds)
creates a new quadtree node
const bounds = 0 0 100 100;const quadTree = 0 bounds;
clear()
clears all child nodes
quadTreeclear;
split()
split current node into quadrants
quadTree;
getQuadrant(rectangle)
get quadrant containing the rectangle in the current node
let rec = 0 0 25 25; quadTree;
insert(rectangle)
insert a rectangle into the node (can be a Geom.box or Geom.circ)
let rec = 0 0 25 25; quadTree;
retrieve(returnList, rectangle)
retrieves all possible objects that the rectangle can collide with
and stores them in the returnList
let rec = 0 0 25 25;let colliderList = ; quadTree;
delete(rectangle)
deletes a rectangle from the quadtree
quadTree;
COLLISION
The Collision module contains methods for testing rectangular or circular collision.
testRect(rec1, rec2)
creates a new quadtree node
let rect1 = 0 0 50 25;let rect2 = 5 10 50 25; let result = Collision; //result = true;
testCirc(circ1, circ2)
creates a new quadtree node
let circ1 = 0 0 25;let circ2 = 5 10 25; let result = Collision; //result = true;
License
This project is licensed under the MIT License - see the LICENSE.md file for details