Auios.QuadTree
Auios.QuadTree copied to clipboard
finding positions outside a rectangle using negative offsets
Hi!
Thanks a lot for your work! But i have problems with negative values. I wanted to check for all enemies who are outside of my playview and i got to this code below where i get many enemies as a found in 'outside' but i only expected one.
What i am doing wrong here?
quadTree = new QuadTree<Enemy>(new System.Numerics.Vector2(-200, -200), new System.Numerics.Vector2(2200f, 1280f), new EnemyBounds());
// Generate some data to insert
Random random = new Random();
List<Vector2> myPositions = new List<Vector2>();
for (int i = 0; i < 1000; i++)
{
var e = new Enemy(
new RectangleF(random.Next(10, Game1._graphics.GraphicsDevice.Viewport.Width - 10), random.Next(10, Game1._graphics.GraphicsDevice.Viewport.Height - 10), 256, 128),
new Vector2(random.NextSingle() / 10f, random.NextSingle() / 10f),
100);
quadTree.Insert(e);
}
quadTree.Insert(new Enemy(
new RectangleF(-100, 100, 100, 128),
new Vector2(random.NextSingle() / 10f, random.NextSingle() / 10f),
100));
// Define search area (x, y, width, height)
QuadTreeRect searchArea = new QuadTreeRect(0, 0, 1920, 1080);
// Find objects in leaf quadrants which overlap the search area
var enemies = quadTree.FindObjects(searchArea);
searchArea = new QuadTreeRect(-250, 0, 200, 1080);
var outside = quadTree.FindObjects(searchArea);
EDIT: I total forgot to post the EnemyBounds class that will read from a RectangleF from monogame.extended
public class EnemyBounds : IQuadTreeObjectBounds<Enemy>
{
public float GetBottom(Enemy obj) => obj.Position.Y;
public float GetTop(Enemy obj) => obj.Position.Y;
public float GetLeft(Enemy obj) => obj.Position.X;
public float GetRight(Enemy obj) => obj.Position.X;
}
Hi @laibach
Glad you found this project interesting. It was a fun little project at the time. Unfortunately I haven't touched this or even used it in over 3, or maybe 4 years? If I had the time I'd be happy to help look into this issue. But don't hold your breath for me. 😅
If you do figure out the issue before I come back with an answer then please post it here. Also if you found something legitimately broken with the system then Id greatly appreciate a PR if possible.
Wish I had more time.