#pragma once #include "Component/Collider.h" #include "Containers/Array.h" #include "Containers/Pair.h" #include "Math/AABB.h" #include namespace Seele { class BVH { public: void findOverlaps(Array>& overlaps); void updateDynamicCollider(entt::entity entity, AABB aabb); void colliderCallback(entt::registry& registry, entt::entity entity); void visualize(); private: struct AABBCenter { AABB bb; Vector center; entt::entity id; }; struct Node { AABB box; int32 parentIndex = -1; int32 left = -1; int32 right = -1; bool isLeaf; bool isValid = true; entt::entity owner; }; void traverseStaticTree(const AABB& aabb, entt::entity source, int32 nodeIndex, Array>& overlaps); void traverseDynamicTree(const AABB& aabb, entt::entity source, int32 nodeIndex, Array>& overlaps); void reinsertCollider(entt::entity, AABB aabb); void removeCollider(entt::entity entity); void addDynamicCollider(entt::entity entity, AABB aabb); void addStaticCollider(entt::entity entity, AABB aabb); void findSibling(Node newNode, int32 nodeIndex, float& bestCost, int32& result); float siblingCost(Node newNode, int32 siblingIndex); float lowerBoundCost(Node newNode, int32 branchIndex); int32 splitNode(Array aabbs); int32 allocateNode(); void freeNode(int32 nodeIndex); void validateBVH() const; Array dynamicNodes; Array staticNodes; Array staticCollider; int32 staticRoot = -1; int32 dynamicRoot = -1; }; } // namespace Seele