I put together my own quadtree implementation that I mostly didn’t write but I made it easy for you to put into your own projects.
Go look at KaiHH’s implementation instead. My “implementation” was really just code stolen from a tutorial that I thought “worked” but clearly doesn’t.
Example:
In order for your entities to be compatible with the QuadTree implementation, you must implement the interface Sizeable which returns X, Y, and width and height.
QuadTree quadtree = new QuadTree(<your width>, <your height>);
// every game update (preferably not every frame)
quadtree.clear();
for (int i = 0; i < entities.size; i++) {
quadtree.insert(<your entity that implements Sizeable here>);
}
// to return a list of entities near a given entity
ArrayList<Sizeable> array = new ArrayList<>();
array.clear();
// fills the arraylist with entities near a given entity
quadtree.retrieve(array, <your entity>);
I didn’t make most of this, I just wanted to share (and have a little bit of competition from the other one submitted here ;D). Supports floats.