Dura2D  v0.1.0
An educational 2D game physics library
Loading...
Searching...
No Matches
d2AABBTree.h
Go to the documentation of this file.
1#ifndef D2AABBTREE_H
2#define D2AABBTREE_H
3
4#include "d2Broadphase.h"
5#include "d2AABB.h"
6
7// Forward declarations
8class d2Draw;
9
10struct d2Node
11{
17
18 d2Node(void)
19 : parent(nullptr)
20 , data(nullptr)
21 {
22 children[0] = nullptr;
23 children[1] = nullptr;
24 }
25
26 inline bool IsLeaf(void) const
27 {
28 return children[0] == nullptr;
29 }
30
31 inline void SetBranch(d2Node *n0, d2Node *n1)
32 {
33 n0->parent = this;
34 n1->parent = this;
35
36 children[0] = n0;
37 children[1] = n1;
38 }
39
40 inline void SetLeaf(d2AABB *data)
41 {
42 this->data = data;
43 data->userData = this;
44
45 children[0] = nullptr;
46 children[1] = nullptr;
47 }
48
49 void UpdateAABB(float margin)
50 {
51 if (IsLeaf())
52 {
53 // Fat AABB
54 const d2Vec2 marginVec(margin, margin);
55 aabb.lowerBound = data->lowerBound - marginVec;
56 aabb.upperBound = data->upperBound + marginVec;
57 }
58 else
59 {
61 }
62 }
63
64 inline d2Node *GetSibling(void) const
65 {
66 return this == parent->children[0]
67 ? parent->children[1]
68 : parent->children[0];
69 }
70};
71
73{
74public:
75
77 : m_root(nullptr)
78 , m_margin(2.0f)
79 { }
80
81 void Add(d2Body* body) override;
82 void Remove(d2Body* body) override;
83 void Update(void) override;
85 d2Body* Pick(const d2Vec2 &point) const override;
86 void Query(const d2AABB &aabb, ColliderList &output) const override { (void)aabb; (void)output; }
87
88 void Draw(const d2Draw &draw) const override;
89
90private:
91
92 typedef std::vector<d2Node *> NodeList;
93
94 void UpdateNodeHelper(d2Node *node, NodeList &invalidNodes);
95 void InsertNode(d2Node *node, d2Node **parent);
96 void RemoveNode(d2Node *node);
97 void ComputePairsHelper(d2Node *n0, d2Node *n1);
98 void ClearChildrenCrossFlagHelper(d2Node *node);
99 void CrossChildren(d2Node *node);
100
101 d2Node *m_root;
102 ColliderPairList m_pairs{};
103 float m_margin;
104 NodeList m_invalidNodes;
105};
106
107#endif //D2AABBTREE_H
Definition d2AABBTree.h:73
d2Body * Pick(const d2Vec2 &point) const override
void Add(d2Body *body) override
d2AABBTree(void)
Definition d2AABBTree.h:76
void Update(void) override
ColliderPairList & ComputePairs(void) override
void Query(const d2AABB &aabb, ColliderList &output) const override
Definition d2AABBTree.h:86
void Draw(const d2Draw &draw) const override
void Remove(d2Body *body) override
A class representing a 2D rigid body.
Definition d2Body.h:28
Definition d2Broadphase.h:16
std::vector< d2Body * > ColliderList
Definition d2Broadphase.h:39
Definition d2Draw.h:67
std::list< ColliderPair > ColliderPairList
Definition d2Broadphase.h:13
Definition d2AABB.h:10
void * userData
Definition d2AABB.h:14
d2Vec2 upperBound
Definition d2AABB.h:12
d2Vec2 lowerBound
Definition d2AABB.h:11
void Combine(const d2AABB &aabb)
Definition d2AABB.h:39
Definition d2AABBTree.h:11
d2AABB aabb
Definition d2AABBTree.h:15
d2Node * GetSibling(void) const
Definition d2AABBTree.h:64
bool IsLeaf(void) const
Definition d2AABBTree.h:26
d2Node * children[2]
Definition d2AABBTree.h:13
void UpdateAABB(float margin)
Definition d2AABBTree.h:49
bool childrenCrossed
Definition d2AABBTree.h:14
d2Node(void)
Definition d2AABBTree.h:18
d2Node * parent
Definition d2AABBTree.h:12
void SetLeaf(d2AABB *data)
Definition d2AABBTree.h:40
d2AABB * data
Definition d2AABBTree.h:16
void SetBranch(d2Node *n0, d2Node *n1)
Definition d2AABBTree.h:31
Represents a 2D vector.
Definition d2Math.h:22