group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
BroadphaseTree.hpp
Go to the documentation of this file.
1
24
25#pragma once
26
28
29#include <cstdint>
30#include <entt/entt.hpp>
31#include <functional>
32#include <glm/vec3.hpp>
33#include <vector>
34
35namespace physics::broadphase
36{
37
40struct Proxy
41{
42 int32_t id = -1;
43 [[nodiscard]] bool valid() const noexcept { return id >= 0; }
44};
45
48class Tree
49{
50public:
53 static constexpr float k_fatAabbMargin = 4.0f;
54
57 static constexpr float k_motionPredict = 2.0f;
58
59 Tree() = default;
60
63 Proxy insertProxy(const WorldAABB& aabb, entt::entity entity);
64
71 bool updateProxy(Proxy p, const WorldAABB& newAabb, glm::vec3 displacement);
72
74 void removeProxy(Proxy p);
75
79 void queryAABB(const WorldAABB& aabb, const std::function<bool(entt::entity)>& visit) const;
80
83 void raycast(glm::vec3 origin, glm::vec3 dir, float maxT, const std::function<bool(entt::entity)>& visit) const;
84
88 void sweptAABB(glm::vec3 halfExtents,
89 glm::vec3 start,
90 glm::vec3 end,
91 const std::function<bool(entt::entity)>& visit) const;
92
94 void clear();
95
97 [[nodiscard]] int proxyCount() const noexcept { return proxyCount_; }
98
100 [[nodiscard]] int32_t rootIndex() const noexcept { return root_; }
101
104 [[nodiscard]] WorldAABB nodeAabb(int32_t nodeId) const noexcept;
105
107 [[nodiscard]] bool isLeaf(int32_t nodeId) const noexcept;
108
110 [[nodiscard]] int32_t leftChild(int32_t nodeId) const noexcept;
111 [[nodiscard]] int32_t rightChild(int32_t nodeId) const noexcept;
112
113private:
114 struct Node
115 {
116 WorldAABB aabb{glm::vec3{0.0f}, glm::vec3{0.0f}};
117 entt::entity entity{entt::null};
118 int32_t parent = -1;
119 int32_t left = -1;
120 int32_t right = -1;
121 int32_t height = 0;
122 };
123
124 std::vector<Node> nodes_;
125 int32_t root_ = -1;
126 int32_t freeList_ = -1;
127 int proxyCount_ = 0;
128
129 int32_t allocNode();
130 void freeNode(int32_t id);
131 void insertLeaf(int32_t leaf);
132 void removeLeaf(int32_t leaf);
133 static WorldAABB combine(const WorldAABB& a, const WorldAABB& b);
134 static float aabbArea(const WorldAABB& a);
135 static bool aabbOverlap(const WorldAABB& a, const WorldAABB& b);
136 static bool aabbContains(const WorldAABB& container, const WorldAABB& inside);
137 int32_t balance(int32_t a);
138};
139
140} // namespace physics::broadphase
Swept AABB and sphere collision queries against world geometry.
static constexpr float k_motionPredict
How aggressively to extend the fat AABB along the body's motion vector each update — multiplied by th...
Definition BroadphaseTree.hpp:57
void removeLeaf(int32_t leaf)
Definition BroadphaseTree.cpp:198
bool isLeaf(int32_t nodeId) const noexcept
True iff the node is a leaf (no children, has entity).
Definition BroadphaseTree.cpp:431
int32_t leftChild(int32_t nodeId) const noexcept
Left/right child of an interior node (or -1 if leaf / invalid).
Definition BroadphaseTree.cpp:438
static bool aabbContains(const WorldAABB &container, const WorldAABB &inside)
Definition BroadphaseTree.cpp:41
void sweptAABB(glm::vec3 halfExtents, glm::vec3 start, glm::vec3 end, const std::function< bool(entt::entity)> &visit) const
Walk every leaf whose AABB intersects the swept-AABB [start, end] expanded by halfExtents.
Definition BroadphaseTree.cpp:402
bool updateProxy(Proxy p, const WorldAABB &newAabb, glm::vec3 displacement)
Notify the tree that the entity's AABB moved.
Definition BroadphaseTree.cpp:84
int32_t allocNode()
Definition BroadphaseTree.cpp:47
void freeNode(int32_t id)
Definition BroadphaseTree.cpp:60
void queryAABB(const WorldAABB &aabb, const std::function< bool(entt::entity)> &visit) const
Walk every leaf whose fat AABB overlaps aabb, calling visit(entity) for each.
Definition BroadphaseTree.cpp:328
void raycast(glm::vec3 origin, glm::vec3 dir, float maxT, const std::function< bool(entt::entity)> &visit) const
Walk every leaf whose AABB intersects the ray segment [origin, origin + dir * maxT].
Definition BroadphaseTree.cpp:354
int32_t rightChild(int32_t nodeId) const noexcept
Definition BroadphaseTree.cpp:445
int32_t rootIndex() const noexcept
Root node id (or -1 if empty). Exposed for debug viz.
Definition BroadphaseTree.hpp:100
static constexpr float k_fatAabbMargin
Margin added to every leaf's AABB so it doesn't reinsert on tiny movements.
Definition BroadphaseTree.hpp:53
int32_t freeList_
Definition BroadphaseTree.hpp:126
static WorldAABB combine(const WorldAABB &a, const WorldAABB &b)
Definition BroadphaseTree.cpp:21
Proxy insertProxy(const WorldAABB &aabb, entt::entity entity)
Insert a leaf at the given AABB.
Definition BroadphaseTree.cpp:68
static bool aabbOverlap(const WorldAABB &a, const WorldAABB &b)
Definition BroadphaseTree.cpp:35
void clear()
Clear all proxies and release pool memory.
Definition BroadphaseTree.cpp:416
static float aabbArea(const WorldAABB &a)
Definition BroadphaseTree.cpp:29
void insertLeaf(int32_t leaf)
Definition BroadphaseTree.cpp:119
void removeProxy(Proxy p)
Destroy the leaf. The Proxy handle becomes invalid.
Definition BroadphaseTree.cpp:110
int proxyCount_
Definition BroadphaseTree.hpp:127
int proxyCount() const noexcept
Current proxy count (live leaves).
Definition BroadphaseTree.hpp:97
WorldAABB nodeAabb(int32_t nodeId) const noexcept
Read-only access to a node's AABB.
Definition BroadphaseTree.cpp:424
std::vector< Node > nodes_
Definition BroadphaseTree.hpp:124
int32_t root_
Definition BroadphaseTree.hpp:125
int32_t balance(int32_t a)
Definition BroadphaseTree.cpp:238
Definition BroadphaseTree.cpp:11
An axis-aligned box in world space, used as static collision geometry.
Definition SweptCollision.hpp:35
Stable handle to a tree leaf.
Definition BroadphaseTree.hpp:41
bool valid() const noexcept
Definition BroadphaseTree.hpp:43
Definition BroadphaseTree.hpp:115
int32_t height
0 = leaf; -1 = free.
Definition BroadphaseTree.hpp:121
int32_t right
Definition BroadphaseTree.hpp:120
int32_t left
Definition BroadphaseTree.hpp:119
int32_t parent
Definition BroadphaseTree.hpp:118
entt::entity entity
Leaf only.
Definition BroadphaseTree.hpp:117
WorldAABB aabb
Definition BroadphaseTree.hpp:116