All Projects → timohausmann → Quadtree Js

timohausmann / Quadtree Js

A lightweight quadtree implementation for javascript

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Quadtree Js

Exengine
A C99 3D game engine
Stars: ✭ 391 (+47.55%)
Mutual labels:  game, collision
Cute headers
Collection of cross-platform one-file C/C++ libraries with no dependencies, primarily used for games
Stars: ✭ 3,274 (+1135.47%)
Mutual labels:  game, collision
Leetcode
Leetcode problems & solutions
Stars: ✭ 258 (-2.64%)
Mutual labels:  algorithms
Rusted Ruins
Extensible open world rogue like game with pixel art. Players can explore the wilderness and ruins.
Stars: ✭ 262 (-1.13%)
Mutual labels:  game
Aitrack
6DoF Head tracking software
Stars: ✭ 262 (-1.13%)
Mutual labels:  game
Megaglest Source
MegaGlest real-time strategy game engine (cross-platform, 3-d)
Stars: ✭ 259 (-2.26%)
Mutual labels:  game
Example .io Game
An example multiplayer (.io) web game.
Stars: ✭ 260 (-1.89%)
Mutual labels:  game
Coophordeshooter
C++ Coop Horde Third-person Shooter for Unreal Engine 4 (Udemy Project)
Stars: ✭ 257 (-3.02%)
Mutual labels:  game
Ggez
Rust library to create a Good Game Easily
Stars: ✭ 3,120 (+1077.36%)
Mutual labels:  game
Freeserf
Settlers 1 (Serf City) clone.
Stars: ✭ 261 (-1.51%)
Mutual labels:  game
Leetcode
LeetCode solutions with Chinese explanation & Summary of classic algorithms.
Stars: ✭ 262 (-1.13%)
Mutual labels:  algorithms
Robusttoolbox
Client/Server Backend for Space Station 14
Stars: ✭ 259 (-2.26%)
Mutual labels:  game
Flappybird
基于Java基础类库编写的Flappy Bird
Stars: ✭ 258 (-2.64%)
Mutual labels:  game
Game Datasets
🎮 A curated list of awesome game datasets, and tools to artificial intelligence in games
Stars: ✭ 261 (-1.51%)
Mutual labels:  game
Mega Interview Guide
The MEGA interview guide, JavaSciript, Front End, Comp Sci
Stars: ✭ 255 (-3.77%)
Mutual labels:  algorithms
Stanford Algs
Example Test Cases for Stanford's Algorithms Coursera Specialization
Stars: ✭ 261 (-1.51%)
Mutual labels:  algorithms
Ja2 Stracciatella
The continuation of the venerable JA2-Stracciatella project.
Stars: ✭ 258 (-2.64%)
Mutual labels:  game
Linuxgsm
The command-line tool for quick, simple deployment and management of Linux dedicated game servers.
Stars: ✭ 3,063 (+1055.85%)
Mutual labels:  game
Dino3d
🦖 Google Chrome T-Rex Run! in 3D (WebGL experiment)
Stars: ✭ 263 (-0.75%)
Mutual labels:  game
Rust Psp
Rust on PSP. Panic and allocation support. Access PSP system libraries.
Stars: ✭ 265 (+0%)
Mutual labels:  game

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.

Only 639 Bytes! (Compressed + Gzipped)

Demos

Install

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>

Or use an awesome CDN like jsdelivr or unpkg:

<script src="https://cdn.jsdelivr.net/npm/@timohausmann/quadtree-js/quadtree.min.js"></script>
<script src="https://unpkg.com/@timohausmann/quadtree-js/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
});

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: 100,
    y: 100,
    width: 100,
    height: 100
});

Retrieve elements from nodes that intersect with the given bounds

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

Clear the Quadtree

myTree.clear();

Check out the examples for more information.

Typescript

Type definitions are included. Inserted objects need to conform to the Quadtree.Rect interface.

import Quadtree, { Rect } from '@timohausmann/quadtree-js';

interface Player extends Rect {
    name: string;
    health: number;
}

const hero:Player = {
    name: 'Shiffman',
    health: 100,
    x: 100,
    y: 100,
    width: 32,
    height: 32
}

myTree.insert(hero);

Browser Support

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

Development scripts

  • npm run build to minify the source

Changelog

1.2.4

Added definition files for Typescript support

JSDoc Fixes

1.2.3

Using github.io for examples (docs), CDN URLs

1.2.2

Removed grunt dev dependency, now using uglify-js to minifiy

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].