Apos.Spatial
Spatial partitioning library for MonoGame and KNI.
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
AABBTreefor 2D andIntervalTreefor 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
Rebuildto 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
- Apos.Gui - UI library for MonoGame.
- Apos.Input - Polling input library for MonoGame.
- Apos.Shapes - Shape rendering library for MonoGame.
- Apos.Camera - Camera library for MonoGame.
- Apos.Editor - Editor project built with MonoGame.
- Apos.History - A C# library that makes it easy to handle undo and redo.
- Apos.Framework - Game architecture for MonoGame.
- AposGameStarter - MonoGame project starter. Common files to help create a game faster.