Skip to content

Latest commit

History

19 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

A simple and efficient Java QuadTree implementation

Very usefull and for game development this QuadTree implementation will help you create an efficient collision detection System.

Installation

Maven

<dependency>
<groupId>org.alwex</groupId>
<artifactId>quad-tree</artifactId>
<version>1.3.0</version>
<type>pom</type>
</dependency>

Gradle

compile 'org.alwex:quad-tree:1.3.0'

Ivy

<dependencyorg='org.alwex'name='quad-tree'rev='1.3.0'>
<artifactname='quad-tree'ext='pom' ></artifact>
</dependency>

Usage

initialize the world with a 10 by 10 dimension starting at point (0,0)

// configure the QuadTreeQuadTree.maxItemByNode = 20;
QuadTree.maxLevel = 10;
// init the spatial world starting at level 0 (root node)QuadTree<QuadRectangle> quadTree = newQuadTree<Entity>(newQuadRectangle(0, 0, 10, 10), 0);
// create entitiesEntitye1 = newEntity();
Entitye2 = newEntity();
Entitye3 = newEntity();
Entitye4 = newEntity();
Entitye5 = newEntity();
Entitye6 = newEntity();
// push entities to the spatial world// we defined some rectangle representing the boudaries// of each entitiesquadTree.insert(newQuadRectangle(1, 1, 1, 1), e1);
quadTree.insert(newQuadRectangle(2, 2, 1, 1), e2);
quadTree.insert(newQuadRectangle(4, 4, 1, 1), e3);
quadTree.insert(newQuadRectangle(6, 6, 1, 1), e4);
quadTree.insert(newQuadRectangle(4, 4, 2, 2), e5);
// YES! this QuadTree implementation accept floatsquadTree.insert(newQuadRectangle(0.5f, 6.5f, 0.5f, 0.5f), e6);
// retrieve the matching element within a defined zoneArrayList<Entity> list = newArrayList<Entity>();
quadTree.getElements(list, newQuadRectangle(2, 2, 1, 1));

Debug

If you need to debug you QuadTree you can retrieve the different zones created on the fly when element are inserted

ArrayList<QuadRectangle> zoneList = newArrayList<QuadRectangle>();
quadTree.getAllZones(zoneList);
// display the zones (Libgdx example)shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.Line); for (QuadRectanglez : zoneList) {
shapeRenderer.rect(
z.x, z.y, z.width, z.height, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN
);
}
shapeRenderer.end();

About

A lightweight and efficient QuadTree usefull for game dev and collision detection

Resources

Stars

40 stars

Watchers

5 watching

Forks

Releases

Packages

Contributors

Languages