Apos.Spatial

Spatial partitioning library for MonoGame and KNI.

DiscordNuGetNuGet

Description

This library finds the items in an area of your game world without checking every item. It’s useful for culling what’s off screen, picking what’s under the mouse, finding what a bullet hits, or finding which objects are close enough to collide.

Thanks!

Special thanks to RandyGaul for providing me the AABBTree algorithm.

Thanks to Erin Catto for Box2D.

Features

  • AABBTree for 2D and IntervalTree for 1D
  • Point and rectangle queries
  • Ray casts that return every hit, or that can stop at the closest one
  • Items that move only restructure the tree once they leave their expanded AABB
  • Queries and iteration don’t allocate
  • Rebuild to optimize the tree after adding a lot of items at once
  • Debug queries that return every node of the tree so you can draw it
  • Works with MonoGame and KNI

Documentation

Getting started

Install with:

dotnet add package Apos.Spatial

Create a tree, add items with the top left corner and size of their AABB, then query it:

using Apos.Spatial;

AABBTree<Entity> tree = new AABBTree<Entity>();

tree.Add(e.XY, e.Size, e);

foreach (Entity e in tree.Query(cameraXY, cameraSize)) {
    // Draw e.
}

e.XY += velocity * dt;
tree.Update(e, e.XY, e.Size);

tree.Remove(e);

Read the Getting started guide for the full walkthrough.

Other projects you might like

Edit this page on GitHub