-
Notifications
You must be signed in to change notification settings - Fork 19
Quad Tree Legacy
Current Wiki index can be found here
Each instance of a Scene
contains its own quad tree. Entities with a QuadTreeComponent
attached to them are entered into the scene's quad tree for partitioning when they are added to the scene. Creating a QuadTreeComponent
requires a reference to the active message bus, and an sf::FloatRect
which defines the component size in local coordinates relative to the parent entity.
auto qtc = xy::Component::create<xy::QuadTreeComponent>(messageBus, {-10.f, -10.f, 20.f, 20.f});
This will create a QuadTreeComponent
with a size of 20 x 20 units, centred on the parent entity's position. To query the quad tree use Scene::queryQuadTree()
passing in an sf::FloatRect
representing the area in which potential matches are to be found. This function returns a std::vector
of QuadTreeComponent
pointers, pointing to components which exist in or around the queried area. This is useful, for example, for finding a small group of enemies near a player, before performing collision testing on them. In a large world with a lot of enemies this can greatly reduce the number of collision tests performed. A QuadTreeComponent
will return a pointer to its parent entity with getEntity()
which can be used to manipulate entities returned in the query result.
By default the quad tree covers an area the size of xy::DefaultSceneSize
. To resize the quad tree to cover a larger (or smaller) area use Scene::setSize()
, passing in an sf::FloatRect
representing the new size of the quad tree.