Remove entity boundaries from Quadtree

The Quadtree did no meaningful management of entity boundaries. All it did was store the maximum and inflate the query rectangle accordingly. The caller can easily take over this responsibility. Consequently, this simplifies queries.
This commit is contained in:
2025-09-27 23:23:38 +02:00
parent 157fea7761
commit 85fcd2358f
2 changed files with 33 additions and 47 deletions

View File

@@ -1,4 +1,3 @@
using NSubstitute;
using SpatialCollections;
using System.Numerics;
@@ -6,13 +5,9 @@ namespace QuadtreeTests
{
public class Tests
{
private const float BoundingRadius = 1.0f;
private class TestEntity(Vector2 position)
{
public Vector2 Position { get; } = position;
public Rectangle BoundingBox { get; } = new Rectangle(position - Vector2.One, position + Vector2.One);
}
private List<Vector2> _positions;
@@ -26,7 +21,7 @@ namespace QuadtreeTests
{
_positions = [new Vector2(1.0f, 1.0f), new Vector2(3.0f, 3.0f), new Vector2(5.0f, 8.0f)];
_entities = new();
_quadtree = new(20, 4, e => e.Position, e => e.BoundingBox);
_quadtree = new(20, 4, e => e.Position);
for (int i = 0; i < _positions.Count; i++)
{
@@ -58,7 +53,7 @@ namespace QuadtreeTests
{
AddObjectsAndAssertCount();
List<TestEntity> result = new();
_quadtree.Query(new Rectangle(new Vector2(3.5f, 3.5f), new Vector2(10.0f, 10.0f)), result);
_quadtree.Query(new Rectangle(new Vector2(2.5f, 2.5f), new Vector2(10.0f, 10.0f)), result);
Assert.That(result.Count, Is.EqualTo(2));
}
@@ -66,7 +61,7 @@ namespace QuadtreeTests
{
for (int i = 0; i < _entities.Count; i++)
{
_quadtree.Add(_entities[i], BoundingRadius);
_quadtree.Add(_entities[i]);
Assert.That(_quadtree.Count, Is.EqualTo(i + 1));
}
}