2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
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.
19 // The pair manager is used by the broad-phase to quickly add/remove/find pairs
20 // of overlapping proxies. It is based closely on code provided by Pierre Terdiman.
21 // http://www.codercorner.com/IncrementalSAP.txt
23 #ifndef B2_PAIR_MANAGER_H
24 #define B2_PAIR_MANAGER_H
26 #include "../Common/b2Settings.h"
27 #include "../Common/b2Math.h"
34 const uint16 b2_nullPair = USHRT_MAX;
35 const uint16 b2_nullProxy = USHRT_MAX;
36 const int32 b2_tableCapacity = b2_maxPairs; // must be a power of two
37 const int32 b2_tableMask = b2_tableCapacity - 1;
43 e_pairBuffered = 0x0001,
44 e_pairRemoved = 0x0002,
48 void SetBuffered() { status |= e_pairBuffered; }
49 void ClearBuffered() { status &= ~e_pairBuffered; }
50 bool IsBuffered() { return (status & e_pairBuffered) == e_pairBuffered; }
52 void SetRemoved() { status |= e_pairRemoved; }
53 void ClearRemoved() { status &= ~e_pairRemoved; }
54 bool IsRemoved() { return (status & e_pairRemoved) == e_pairRemoved; }
56 void SetFinal() { status |= e_pairFinal; }
57 bool IsFinal() { return (status & e_pairFinal) == e_pairFinal; }
75 virtual ~b2PairCallback() {}
77 // This should return the new pair user data. It is ok if the
79 virtual void* PairAdded(void* proxyUserData1, void* proxyUserData2) = 0;
81 // This should free the pair's user data. In extreme circumstances, it is possible
82 // this will be called with null pairUserData because the pair never existed.
83 virtual void PairRemoved(void* proxyUserData1, void* proxyUserData2, void* pairUserData) = 0;
91 void Initialize(b2BroadPhase* broadPhase, b2PairCallback* callback);
93 void AddBufferedPair(int32 proxyId1, int32 proxyId2);
94 void RemoveBufferedPair(int32 proxyId1, int32 proxyId2);
99 b2Pair* Find(int32 proxyId1, int32 proxyId2);
100 b2Pair* Find(int32 proxyId1, int32 proxyId2, uint32 hashValue);
102 b2Pair* AddPair(int32 proxyId1, int32 proxyId2);
103 void* RemovePair(int32 proxyId1, int32 proxyId2);
105 void ValidateBuffer();
106 void ValidateTable();
109 b2BroadPhase *m_broadPhase;
110 b2PairCallback *m_callback;
111 b2Pair m_pairs[b2_maxPairs];
115 b2BufferedPair m_pairBuffer[b2_maxPairs];
116 int32 m_pairBufferCount;
118 uint16 m_hashTable[b2_tableCapacity];