SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
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
4
#include <
Physics/Colliders.hpp
>
5
#include <functional>
6
#include <vector>
7
8
namespace
Sleak
{
9
namespace
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
{
17
AABB
fatAABB
;
18
void
*
userData
=
nullptr
;
19
int
parent
=
NULL_NODE
;
20
int
left
=
NULL_NODE
;
21
int
right
=
NULL_NODE
;
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
29
class
DynamicAABBTree
{
30
public
:
31
DynamicAABBTree
();
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_
Colliders.hpp
Sleak::Math::Vector3D
Definition
Vector.hpp:354
Sleak::Physics::DynamicAABBTree::Query
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.
Definition
DynamicAABBTree.cpp:327
Sleak::Physics::DynamicAABBTree::Insert
int Insert(const AABB &aabb, void *userData)
Adds a new proxy with a fattened AABB and returns its id.
Definition
DynamicAABBTree.cpp:58
Sleak::Physics::DynamicAABBTree::GetFatAABB
const AABB & GetFatAABB(int proxyId) const
Definition
DynamicAABBTree.hpp:47
Sleak::Physics::DynamicAABBTree::~DynamicAABBTree
~DynamicAABBTree()=default
Sleak::Physics::DynamicAABBTree::MoveProxy
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.
Definition
DynamicAABBTree.cpp:73
Sleak::Physics::DynamicAABBTree::Remove
void Remove(int proxyId)
Removes a proxy and rebalances the tree around it.
Definition
DynamicAABBTree.cpp:68
Sleak::Physics::DynamicAABBTree::GetUserData
void * GetUserData(int proxyId) const
Definition
DynamicAABBTree.hpp:48
Sleak::Physics::DynamicAABBTree::RayCast
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.
Definition
DynamicAABBTree.cpp:351
Sleak::Physics::DynamicAABBTree::DynamicAABBTree
DynamicAABBTree()
Definition
DynamicAABBTree.cpp:9
Sleak::Physics
Collision shapes, the broadphase tree, and the world that steps them.
Definition
SceneBase.hpp:29
Sleak::Physics::NULL_NODE
static constexpr int NULL_NODE
Definition
DynamicAABBTree.hpp:11
Sleak::Physics::FAT_AABB_MARGIN
static constexpr float FAT_AABB_MARGIN
Definition
DynamicAABBTree.hpp:12
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
Sleak::Physics::AABB
Definition
Colliders.hpp:20
Sleak::Physics::TreeNode
Definition
DynamicAABBTree.hpp:16
Sleak::Physics::TreeNode::userData
void * userData
Definition
DynamicAABBTree.hpp:18
Sleak::Physics::TreeNode::parent
int parent
Definition
DynamicAABBTree.hpp:19
Sleak::Physics::TreeNode::left
int left
Definition
DynamicAABBTree.hpp:20
Sleak::Physics::TreeNode::height
int height
Definition
DynamicAABBTree.hpp:22
Sleak::Physics::TreeNode::fatAABB
AABB fatAABB
Definition
DynamicAABBTree.hpp:17
Sleak::Physics::TreeNode::right
int right
Definition
DynamicAABBTree.hpp:21
Sleak::Physics::TreeNode::IsLeaf
bool IsLeaf() const
Definition
DynamicAABBTree.hpp:24
include
public
Physics
DynamicAABBTree.hpp
Generated on
for SleakEngine by
1.18.0