Skip to content

kwarkGorny/QuadTree

Repository files navigation

QuadTree

One header only implementation of non-recursive quadtree

Constructing a quadtree

QuadTree<YourType> quadtree(maxLevels, maxObjectsPerNode, { x, y, width, height});

Inserting objects

quadtree.insert({ x, y, width, height}, yourTypeData);

Removing objects

//Removing all objects with predicate
quadTree.erase_if([](Entry<YourType>& entry) { ... });

//Removing first object with predicate
quadTree.erase_first_if([](Entry<YourType>& entry) { ... });

//Removing all objects equal to value
quadTree.erase(value);

//Removing first object equal to value
quadTree.erase_first(value);

Collision checking

// resovling collision using default collision check function
quadtree.each_collision( [](Entry<YourType>& entryL, Entry<YourType>& entryR) {
  std::cout << "collision detected"
});
// resolving collsions with own intersection function
auto yourCollsionCheckFuncion = [](Entry<YourType>& entryL, Entry<YourType>& entryR) {
     return entryL.bound.intersects(entryR.bound);
 };
 
 auto yourCollsionResolveFunction = [](Entry<YourType>& entryL, Entry<YourType>& entryR) {
     std::cout << "collision detected"
 };
 
quadtree.each_collision(yourCollsionCheckFuncion, yourCollsionResolveFunction);
// resolving collsions with object outside quadtree
quadtree.each_collision(mousePosition, yourCollsionCheckFuncion, yourCollsionResolveFunction);

Utility functions

// reserve memory for 5 QuadNodes
quadtree.reserve(5);
// removing all nodes
quadtree.clear();
// release unneccesery memmory
quadtree.shrink_to_fit();

About

Implementation of non-recursive quadtree

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages