SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
DynamicAABBTree.hpp
Go to the documentation of this file.
1#ifndef _DYNAMIC_AABB_TREE_HPP_
2#define _DYNAMIC_AABB_TREE_HPP_
3
5#include <functional>
6#include <vector>
7
8namespace Sleak {
9namespace Physics {
10
11 static constexpr int NULL_NODE = -1;
12 static constexpr float FAT_AABB_MARGIN = 0.1f;
13
14 /// One node of the DynamicAABBTree, either an internal node or a leaf holding a proxy's fattened AABB.
15 /// @ingroup physics
16 struct TreeNode {
18 void* userData = nullptr;
22 int height = 0;
23
24 bool IsLeaf() const { return left == NULL_NODE; }
25 };
26
27 /// Broadphase AABB tree; PhysicsWorld inserts colliders as proxies and queries overlaps against it.
28 /// @ingroup physics
30 public:
32 ~DynamicAABBTree() = default;
33
34 /// Adds a new proxy with a fattened AABB and returns its id.
35 int Insert(const AABB& aabb, void* userData);
36 /// Removes a proxy and rebalances the tree around it.
37 void Remove(int proxyId);
38 /// Refits a proxy's fat AABB to newAABB, re-inserting it only if it moved outside the fat margin.
39 bool MoveProxy(int proxyId, const AABB& newAABB, const Vector3D& displacement);
40
41 /// Visits every leaf whose fat AABB overlaps queryAABB; stop early by returning false from callback.
42 void Query(const AABB& queryAABB, const std::function<bool(int)>& callback) const;
43 /// Walks the tree along a ray, visiting candidate leaves within maxDist.
44 void RayCast(const Vector3D& origin, const Vector3D& direction, float maxDist,
45 const std::function<bool(int)>& callback) const;
46
47 const AABB& GetFatAABB(int proxyId) const { return m_nodes[proxyId].fatAABB; }
48 void* GetUserData(int proxyId) const { return m_nodes[proxyId].userData; }
49
50 private:
51 int AllocateNode();
52 void FreeNode(int nodeId);
53 void InsertLeaf(int leaf);
54 void RemoveLeaf(int leaf);
55 int Balance(int nodeId);
56
57 std::vector<TreeNode> m_nodes;
58 int m_root = NULL_NODE;
59 int m_freeList = NULL_NODE;
60 int m_nodeCount = 0;
61 int m_nodeCapacity = 0;
62 };
63
64} // namespace Physics
65} // namespace Sleak
66
67#endif // _DYNAMIC_AABB_TREE_HPP_
void Query(const AABB &queryAABB, const std::function< bool(int)> &callback) const
Visits every leaf whose fat AABB overlaps queryAABB; stop early by returning false from callback.
int Insert(const AABB &aabb, void *userData)
Adds a new proxy with a fattened AABB and returns its id.
const AABB & GetFatAABB(int proxyId) const
bool MoveProxy(int proxyId, const AABB &newAABB, const Vector3D &displacement)
Refits a proxy's fat AABB to newAABB, re-inserting it only if it moved outside the fat margin.
void Remove(int proxyId)
Removes a proxy and rebalances the tree around it.
void * GetUserData(int proxyId) const
void RayCast(const Vector3D &origin, const Vector3D &direction, float maxDist, const std::function< bool(int)> &callback) const
Walks the tree along a ray, visiting candidate leaves within maxDist.
Collision shapes, the broadphase tree, and the world that steps them.
Definition SceneBase.hpp:29
static constexpr int NULL_NODE
static constexpr float FAT_AABB_MARGIN
Root namespace for everything the engine exposes.
Definition Camera.hpp:10