A set of highly-optimized, flexible and powerful 2D spatial partitions for MonoGame
- Make a Quadtree variable
Quadtree tree = new Quadtree(x: 0, y: 0, width: 500, height: 500, maxItems: 100, maxDepth: 8);
- Add/Update an item(s)
tree.Update(0, x: 5, y: 5, width: 80, height: 25);
- Query an area(s)
foreach (int i in tree.Query(new Point(3, 4))) {
// ...
}
foreach (int i in tree.Query(new Vector2(32.5f, 25))) {
// ...
}
foreach (int i in tree.Query(new Rectangle(x: 7, y: 2, width: 32, height: 27))) {
// ...
}
foreach (int i in tree.Query(new Rectangle(x: 7, y: 2, width: 32, height: 27), rotation: 0, origin: Vector2.Zero)) {
// ...
}
foreach (int i in tree.Query(new Point(3, 4), radius: 10)) {
// ...
}
foreach (int i in tree.Linecast(new Vector2(3, 4), new Vector2(8, 12), thickness: 3)) {
// ...
}
foreach (int i in tree.Raycast(new Vector2(3, 4), direction: new Vector2(.5f, .75f), thickness: 3)) {
// ...
}
foreach (int i in tree.Raycast(new Vector2(3, 4), rotation: MathF.PI, thickness: 3)) {
// ...
}
- When an item moves, update it!
tree.Update(0, x: 5, y: 5, width: 80, height: 25);
- Did an entity despawn? Remove it!
tree.Remove(0);
- Call tree update at the end of every game update!
tree.Update();