Code Monkey home page Code Monkey logo

quadtree-js's Introduction

quadtree-js

This is a JavaScript Quadtree implementation based on the Java Methods described on gamedevelopment.tutsplus.com by Steven Lambert:

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.

This 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.

Demos

Install

Now also available on npm! Install this module via npm and import or require it:

npm i -D @timohausmann/quadtree-js
import Quadtree from '@timohausmann/quadtree-js';
const Quadtree = require('@timohausmann/quadtree-js');

Alternatively, download the source and include it the old-fashioned way:

<script src="quadtree.min.js"></script>

How to use

Create a new Quadtree with default values for max_objects (10) and max_levels (4)

var myTree = new Quadtree({
	x: 0,
	y: 0,
	width: 400,
	height: 300
}, 10, 4);

MAX_OBJECTS defines how many objects a node can hold before it splits and MAX_LEVELS defines the deepest level subnode.

If 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.

var myTree = new Quadtree({
	x: 0,
	y: 0,
	width: 800,
	height: 600
}, 15, 6);

Insert an element in the Quadtree

myTree.insert({
	x: 200,
	y: 150,
	width: 20,
	height: 20
});

Retrieve elements from nodes that intersect with the given bounds

var elements = myTree.retrieve({
	x: 150,
	y: 100,
	width: 20,
	height: 20
});

Clear the Quadtree

myTree.clear();

Check out the examples for more information.

Browser Support

This library is supported in all modern browsers including IE9 and above.

Changelog

1.2.1

Allow float boundaries for Quads

Simplified getIndex function

1.2.0

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.

1.1.3

Support for npm and module.exports

Update single objects

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.

quadtree-js's People

Contributors

charlespwd avatar timohausmann avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.