File: DynamicTreeBroadPhase.h

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (248 lines) | stat: -rw-r--r-- 7,502 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// MIT License
//
// Copyright (c) 2019 Erin Catto
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include "DynamicTree.h"

namespace Jazz2::Collisions
{
	/** @brief Collided pair of objects found by collision detection */
	struct CollisionPair {
		/** @brief Proxy ID of the first node */
		std::int32_t ProxyIdA;
		/** @brief Proxy ID of the second node */
		std::int32_t ProxyIdB;
	};

	/**
		@brief Broad-phase for collision detection

		The broad-phase is used for computing pairs and performing volume queries and ray casts.
		This broad-phase does not persist pairs. Instead, this reports potentially new pairs.
		It is up to the client to consume the new pairs and to track subsequent overlap.
	*/
	class DynamicTreeBroadPhase
	{
		friend class DynamicTree;

	public:
		DynamicTreeBroadPhase();
		~DynamicTreeBroadPhase();

		/**
		 * @brief Creates a proxy with an initial AABB
		 * 
		 * Pairs are not reported until @ref UpdatePairs() is called
		 */
		std::int32_t CreateProxy(const AABBf& aabb, void* userData);

		/** @brief Destroys a proxy */
		void DestroyProxy(std::int32_t proxyId);

		/**
		 * @brief Moves a proxy with a swepted AABB
		 *
		 * If the proxy has moved outside of its fattened AABB, then the proxy is removed from
		 * the tree and re-inserted. Otherwise the function returns immediately.
		 *
		 * @return `true` if the proxy was re-inserted.
		 */
		void MoveProxy(std::int32_t proxyId, const AABBf& aabb, Vector2f displacement);

		/** @brief Triggers a re-processing of it's pairs on the next call to @ref UpdatePairs() */
		void TouchProxy(std::int32_t proxyId);

		/** @brief Returns the fat AABB for a proxy */
		const AABBf& GetFatAABB(std::int32_t proxyId) const;

		/** @brief Returns a user data from a proxy */
		void* GetUserData(std::int32_t proxyId) const;

		/** @brief Tests overlap of fat AABBs */
		bool TestOverlap(std::int32_t proxyIdA, std::int32_t proxyIdB) const;

		/** @brief Returns the number of proxies */
		std::int32_t GetProxyCount() const;

		/** @brief Updates the pairs */
		template <typename T>
		void UpdatePairs(T* callback);

		/**
		 * @brief Queries an AABB for overlapping proxies
		 * 
		 * The callback class is called for each proxy that overlaps the supplied AABB.
		 */
		template <typename T>
		void Query(T* callback, const AABBf& aabb) const;

		// Ray-cast against the proxies in the tree. This relies on the callback
		// to perform a exact ray-cast in the case were the proxy contains a shape.
		// The callback also performs the any collision filtering. This has performance
		// roughly equal to k * log(n), where k is the number of collisions and n is the
		// number of proxies in the tree.
		// @param input the ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
		// @param callback a callback class that is called for each proxy that is hit by the ray.
		//template <typename T>
		//void RayCast(T* callback, const b2RayCastInput& input) const;

		/** @brief Returns the height of the embedded tree */
		std::int32_t GetTreeHeight() const;

		/** @brief Returns the balance of the embedded tree */
		std::int32_t GetTreeBalance() const;

		/** @brief Returns the quality metric of the embedded tree */
		float GetTreeQuality() const;

		/**
		 * @brief Shifts the world origin
		 *
		 * Useful for large worlds. The shift formula is: `position -= newOrigin`
		 *
		 * @param newOrigin the new origin with respect to the old origin
		 */
		void ShiftOrigin(Vector2f newOrigin);

	private:
		static constexpr std::int32_t DefaultPairCapacity = 16;
		static constexpr std::int32_t DefaultMoveCapacity = /*16*/64;

		DynamicTree _tree;

		std::int32_t _proxyCount;

		std::int32_t* _moveBuffer;
		std::int32_t _moveCapacity;
		std::int32_t _moveCount;

		CollisionPair* _pairBuffer;
		std::int32_t _pairCapacity;
		std::int32_t _pairCount;

		std::int32_t _queryProxyId;

		void BufferMove(std::int32_t proxyId);
		void UnBufferMove(std::int32_t proxyId);

		bool OnCollisionQuery(std::int32_t proxyId);
	};

	inline void* DynamicTreeBroadPhase::GetUserData(std::int32_t proxyId) const
	{
		return _tree.GetUserData(proxyId);
	}

	inline bool DynamicTreeBroadPhase::TestOverlap(std::int32_t proxyIdA, std::int32_t proxyIdB) const
	{
		const AABBf& aabbA = _tree.GetFatAABB(proxyIdA);
		const AABBf& aabbB = _tree.GetFatAABB(proxyIdB);
		return aabbA.Overlaps(aabbB);
	}

	inline const AABBf& DynamicTreeBroadPhase::GetFatAABB(std::int32_t proxyId) const
	{
		return _tree.GetFatAABB(proxyId);
	}

	inline std::int32_t DynamicTreeBroadPhase::GetProxyCount() const
	{
		return _proxyCount;
	}

	inline std::int32_t DynamicTreeBroadPhase::GetTreeHeight() const
	{
		return _tree.GetHeight();
	}

	inline std::int32_t DynamicTreeBroadPhase::GetTreeBalance() const
	{
		return _tree.GetMaxBalance();
	}

	inline float DynamicTreeBroadPhase::GetTreeQuality() const
	{
		return _tree.GetAreaRatio();
	}

	template <typename T>
	void DynamicTreeBroadPhase::UpdatePairs(T* callback)
	{
		// Reset pair buffer
		_pairCount = 0;

		// Perform tree queries for all moving proxies.
		for (std::int32_t i = 0; i < _moveCount; ++i) {
			_queryProxyId = _moveBuffer[i];
			if (_queryProxyId == NullNode) {
				continue;
			}

			// We have to query the tree with the fat AABB so that
			// we don't fail to create a pair that may touch later.
			const AABBf& fatAABB = _tree.GetFatAABB(_queryProxyId);

			// Query tree, create pairs and add them pair buffer.
			_tree.Query(this, fatAABB);
		}

		// Send pairs to caller
		for (std::int32_t i = 0; i < _pairCount; ++i) {
			CollisionPair* primaryPair = &_pairBuffer[i];
			void* userDataA = _tree.GetUserData(primaryPair->ProxyIdA);
			void* userDataB = _tree.GetUserData(primaryPair->ProxyIdB);

			callback->OnPairAdded(userDataA, userDataB);
		}

		// Clear move flags
		for (std::int32_t i = 0; i < _moveCount; ++i) {
			std::int32_t proxyId = _moveBuffer[i];
			if (proxyId == NullNode) {
				continue;
			}

			_tree.ClearMoved(proxyId);
		}

		// Reset move buffer
		_moveCount = 0;
	}

	template <typename T>
	inline void DynamicTreeBroadPhase::Query(T* callback, const AABBf& aabb) const
	{
		_tree.Query(callback, aabb);
	}

	/*template <typename T>
	inline void DynamicTreeBroadPhase::RayCast(T* callback, const b2RayCastInput& input) const
	{
		_tree.RayCast(callback, input);
	}*/

	inline void DynamicTreeBroadPhase::ShiftOrigin(Vector2f newOrigin)
	{
		_tree.ShiftOrigin(newOrigin);
	}
}