b2PairManager.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "b2PairManager.h"
  19. #include "b2BroadPhase.h"
  20. #include <algorithm>
  21. // Thomas Wang's hash, see: http://www.concentric.net/~Ttwang/tech/inthash.htm
  22. // This assumes proxyId1 and proxyId2 are 16-bit.
  23. inline uint32 Hash(uint32 proxyId1, uint32 proxyId2)
  24. {
  25. uint32 key = (proxyId2 << 16) | proxyId1;
  26. key = ~key + (key << 15);
  27. key = key ^ (key >> 12);
  28. key = key + (key << 2);
  29. key = key ^ (key >> 4);
  30. key = key * 2057;
  31. key = key ^ (key >> 16);
  32. return key;
  33. }
  34. inline bool Equals(const b2Pair& pair, int32 proxyId1, int32 proxyId2)
  35. {
  36. return pair.proxyId1 == proxyId1 && pair.proxyId2 == proxyId2;
  37. }
  38. inline bool Equals(const b2BufferedPair& pair1, const b2BufferedPair& pair2)
  39. {
  40. return pair1.proxyId1 == pair2.proxyId1 && pair1.proxyId2 == pair2.proxyId2;
  41. }
  42. // For sorting.
  43. inline bool operator < (const b2BufferedPair& pair1, const b2BufferedPair& pair2)
  44. {
  45. if (pair1.proxyId1 < pair2.proxyId1)
  46. {
  47. return true;
  48. }
  49. if (pair1.proxyId1 == pair2.proxyId1)
  50. {
  51. return pair1.proxyId2 < pair2.proxyId2;
  52. }
  53. return false;
  54. }
  55. b2PairManager::b2PairManager()
  56. {
  57. b2Assert(b2IsPowerOfTwo(b2_tableCapacity) == true);
  58. b2Assert(b2_tableCapacity >= b2_maxPairs);
  59. for (int32 i = 0; i < b2_tableCapacity; ++i)
  60. {
  61. m_hashTable[i] = b2_nullPair;
  62. }
  63. m_freePair = 0;
  64. for (int32 i = 0; i < b2_maxPairs; ++i)
  65. {
  66. m_pairs[i].proxyId1 = b2_nullProxy;
  67. m_pairs[i].proxyId2 = b2_nullProxy;
  68. m_pairs[i].userData = NULL;
  69. m_pairs[i].status = 0;
  70. m_pairs[i].next = uint16(i + 1);
  71. }
  72. m_pairs[b2_maxPairs-1].next = b2_nullPair;
  73. m_pairCount = 0;
  74. m_pairBufferCount = 0;
  75. }
  76. void b2PairManager::Initialize(b2BroadPhase* broadPhase, b2PairCallback* callback)
  77. {
  78. m_broadPhase = broadPhase;
  79. m_callback = callback;
  80. }
  81. b2Pair* b2PairManager::Find(int32 proxyId1, int32 proxyId2, uint32 hash)
  82. {
  83. int32 index = m_hashTable[hash];
  84. while (index != b2_nullPair && Equals(m_pairs[index], proxyId1, proxyId2) == false)
  85. {
  86. index = m_pairs[index].next;
  87. }
  88. if (index == b2_nullPair)
  89. {
  90. return NULL;
  91. }
  92. b2Assert(index < b2_maxPairs);
  93. return m_pairs + index;
  94. }
  95. b2Pair* b2PairManager::Find(int32 proxyId1, int32 proxyId2)
  96. {
  97. if (proxyId1 > proxyId2) b2Swap(proxyId1, proxyId2);
  98. int32 hash = Hash(proxyId1, proxyId2) & b2_tableMask;
  99. return Find(proxyId1, proxyId2, hash);
  100. }
  101. // Returns existing pair or creates a new one.
  102. b2Pair* b2PairManager::AddPair(int32 proxyId1, int32 proxyId2)
  103. {
  104. if (proxyId1 > proxyId2) b2Swap(proxyId1, proxyId2);
  105. int32 hash = Hash(proxyId1, proxyId2) & b2_tableMask;
  106. b2Pair* pair = Find(proxyId1, proxyId2, hash);
  107. if (pair != NULL)
  108. {
  109. return pair;
  110. }
  111. b2Assert(m_pairCount < b2_maxPairs && m_freePair != b2_nullPair);
  112. uint16 pairIndex = m_freePair;
  113. pair = m_pairs + pairIndex;
  114. m_freePair = pair->next;
  115. pair->proxyId1 = (uint16)proxyId1;
  116. pair->proxyId2 = (uint16)proxyId2;
  117. pair->status = 0;
  118. pair->userData = NULL;
  119. pair->next = m_hashTable[hash];
  120. m_hashTable[hash] = pairIndex;
  121. ++m_pairCount;
  122. return pair;
  123. }
  124. // Removes a pair. The pair must exist.
  125. void* b2PairManager::RemovePair(int32 proxyId1, int32 proxyId2)
  126. {
  127. b2Assert(m_pairCount > 0);
  128. if (proxyId1 > proxyId2) b2Swap(proxyId1, proxyId2);
  129. int32 hash = Hash(proxyId1, proxyId2) & b2_tableMask;
  130. uint16* node = &m_hashTable[hash];
  131. while (*node != b2_nullPair)
  132. {
  133. if (Equals(m_pairs[*node], proxyId1, proxyId2))
  134. {
  135. uint16 index = *node;
  136. *node = m_pairs[*node].next;
  137. b2Pair* pair = m_pairs + index;
  138. void* userData = pair->userData;
  139. // Scrub
  140. pair->next = m_freePair;
  141. pair->proxyId1 = b2_nullProxy;
  142. pair->proxyId2 = b2_nullProxy;
  143. pair->userData = NULL;
  144. pair->status = 0;
  145. m_freePair = index;
  146. --m_pairCount;
  147. return userData;
  148. }
  149. else
  150. {
  151. node = &m_pairs[*node].next;
  152. }
  153. }
  154. b2Assert(false);
  155. return NULL;
  156. }
  157. /*
  158. As proxies are created and moved, many pairs are created and destroyed. Even worse, the same
  159. pair may be added and removed multiple times in a single time step of the physics engine. To reduce
  160. traffic in the pair manager, we try to avoid destroying pairs in the pair manager until the
  161. end of the physics step. This is done by buffering all the RemovePair requests. AddPair
  162. requests are processed immediately because we need the hash table entry for quick lookup.
  163. All user user callbacks are delayed until the buffered pairs are confirmed in Commit.
  164. This is very important because the user callbacks may be very expensive and client logic
  165. may be harmed if pairs are added and removed within the same time step.
  166. Buffer a pair for addition.
  167. We may add a pair that is not in the pair manager or pair buffer.
  168. We may add a pair that is already in the pair manager and pair buffer.
  169. If the added pair is not a new pair, then it must be in the pair buffer (because RemovePair was called).
  170. */
  171. void b2PairManager::AddBufferedPair(int32 id1, int32 id2)
  172. {
  173. b2Assert(id1 != b2_nullProxy && id2 != b2_nullProxy);
  174. b2Assert(m_pairBufferCount < b2_maxPairs);
  175. b2Pair* pair = AddPair(id1, id2);
  176. // If this pair is not in the pair buffer ...
  177. if (pair->IsBuffered() == false)
  178. {
  179. // This must be a newly added pair.
  180. b2Assert(pair->IsFinal() == false);
  181. // Add it to the pair buffer.
  182. pair->SetBuffered();
  183. m_pairBuffer[m_pairBufferCount].proxyId1 = pair->proxyId1;
  184. m_pairBuffer[m_pairBufferCount].proxyId2 = pair->proxyId2;
  185. ++m_pairBufferCount;
  186. b2Assert(m_pairBufferCount <= m_pairCount);
  187. }
  188. // Confirm this pair for the subsequent call to Commit.
  189. pair->ClearRemoved();
  190. if (b2BroadPhase::s_validate)
  191. {
  192. ValidateBuffer();
  193. }
  194. }
  195. // Buffer a pair for removal.
  196. void b2PairManager::RemoveBufferedPair(int32 id1, int32 id2)
  197. {
  198. b2Assert(id1 != b2_nullProxy && id2 != b2_nullProxy);
  199. b2Assert(m_pairBufferCount < b2_maxPairs);
  200. b2Pair* pair = Find(id1, id2);
  201. if (pair == NULL)
  202. {
  203. // The pair never existed. This is legal (due to collision filtering).
  204. return;
  205. }
  206. // If this pair is not in the pair buffer ...
  207. if (pair->IsBuffered() == false)
  208. {
  209. // This must be an old pair.
  210. b2Assert(pair->IsFinal() == true);
  211. pair->SetBuffered();
  212. m_pairBuffer[m_pairBufferCount].proxyId1 = pair->proxyId1;
  213. m_pairBuffer[m_pairBufferCount].proxyId2 = pair->proxyId2;
  214. ++m_pairBufferCount;
  215. b2Assert(m_pairBufferCount <= m_pairCount);
  216. }
  217. pair->SetRemoved();
  218. if (b2BroadPhase::s_validate)
  219. {
  220. ValidateBuffer();
  221. }
  222. }
  223. void b2PairManager::Commit()
  224. {
  225. int32 removeCount = 0;
  226. b2Proxy* proxies = m_broadPhase->m_proxyPool;
  227. for (int32 i = 0; i < m_pairBufferCount; ++i)
  228. {
  229. b2Pair* pair = Find(m_pairBuffer[i].proxyId1, m_pairBuffer[i].proxyId2);
  230. b2Assert(pair->IsBuffered());
  231. pair->ClearBuffered();
  232. b2Assert(pair->proxyId1 < b2_maxProxies && pair->proxyId2 < b2_maxProxies);
  233. b2Proxy* proxy1 = proxies + pair->proxyId1;
  234. b2Proxy* proxy2 = proxies + pair->proxyId2;
  235. b2Assert(proxy1->IsValid());
  236. b2Assert(proxy2->IsValid());
  237. if (pair->IsRemoved())
  238. {
  239. // It is possible a pair was added then removed before a commit. Therefore,
  240. // we should be careful not to tell the user the pair was removed when the
  241. // the user didn't receive a matching add.
  242. if (pair->IsFinal() == true)
  243. {
  244. m_callback->PairRemoved(proxy1->userData, proxy2->userData, pair->userData);
  245. }
  246. // Store the ids so we can actually remove the pair below.
  247. m_pairBuffer[removeCount].proxyId1 = pair->proxyId1;
  248. m_pairBuffer[removeCount].proxyId2 = pair->proxyId2;
  249. ++removeCount;
  250. }
  251. else
  252. {
  253. b2Assert(m_broadPhase->TestOverlap(proxy1, proxy2) == true);
  254. if (pair->IsFinal() == false)
  255. {
  256. pair->userData = m_callback->PairAdded(proxy1->userData, proxy2->userData);
  257. pair->SetFinal();
  258. }
  259. }
  260. }
  261. for (int32 i = 0; i < removeCount; ++i)
  262. {
  263. RemovePair(m_pairBuffer[i].proxyId1, m_pairBuffer[i].proxyId2);
  264. }
  265. m_pairBufferCount = 0;
  266. if (b2BroadPhase::s_validate)
  267. {
  268. ValidateTable();
  269. }
  270. }
  271. void b2PairManager::ValidateBuffer()
  272. {
  273. #ifdef _DEBUG
  274. b2Assert(m_pairBufferCount <= m_pairCount);
  275. std::sort(m_pairBuffer, m_pairBuffer + m_pairBufferCount);
  276. for (int32 i = 0; i < m_pairBufferCount; ++i)
  277. {
  278. if (i > 0)
  279. {
  280. b2Assert(Equals(m_pairBuffer[i], m_pairBuffer[i-1]) == false);
  281. }
  282. b2Pair* pair = Find(m_pairBuffer[i].proxyId1, m_pairBuffer[i].proxyId2);
  283. b2Assert(pair->IsBuffered());
  284. b2Assert(pair->proxyId1 != pair->proxyId2);
  285. b2Assert(pair->proxyId1 < b2_maxProxies);
  286. b2Assert(pair->proxyId2 < b2_maxProxies);
  287. b2Proxy* proxy1 = m_broadPhase->m_proxyPool + pair->proxyId1;
  288. b2Proxy* proxy2 = m_broadPhase->m_proxyPool + pair->proxyId2;
  289. b2Assert(proxy1->IsValid() == true);
  290. b2Assert(proxy2->IsValid() == true);
  291. }
  292. #endif
  293. }
  294. void b2PairManager::ValidateTable()
  295. {
  296. #ifdef _DEBUG
  297. for (int32 i = 0; i < b2_tableCapacity; ++i)
  298. {
  299. uint16 index = m_hashTable[i];
  300. while (index != b2_nullPair)
  301. {
  302. b2Pair* pair = m_pairs + index;
  303. b2Assert(pair->IsBuffered() == false);
  304. b2Assert(pair->IsFinal() == true);
  305. b2Assert(pair->IsRemoved() == false);
  306. b2Assert(pair->proxyId1 != pair->proxyId2);
  307. b2Assert(pair->proxyId1 < b2_maxProxies);
  308. b2Assert(pair->proxyId2 < b2_maxProxies);
  309. b2Proxy* proxy1 = m_broadPhase->m_proxyPool + pair->proxyId1;
  310. b2Proxy* proxy2 = m_broadPhase->m_proxyPool + pair->proxyId2;
  311. b2Assert(proxy1->IsValid() == true);
  312. b2Assert(proxy2->IsValid() == true);
  313. b2Assert(m_broadPhase->TestOverlap(proxy1, proxy2) == true);
  314. index = pair->next;
  315. }
  316. }
  317. #endif
  318. }