diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/ModifiedBulletX/ModifiedBulletX/Collision/BroadphaseCollision/OverlappingPairCache.cs | 318 |
1 files changed, 159 insertions, 159 deletions
diff --git a/libraries/ModifiedBulletX/ModifiedBulletX/Collision/BroadphaseCollision/OverlappingPairCache.cs b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/BroadphaseCollision/OverlappingPairCache.cs index 165912a..6548560 100644 --- a/libraries/ModifiedBulletX/ModifiedBulletX/Collision/BroadphaseCollision/OverlappingPairCache.cs +++ b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/BroadphaseCollision/OverlappingPairCache.cs | |||
@@ -1,159 +1,159 @@ | |||
1 | /* | 1 | /* |
2 | Bullet for XNA Copyright (c) 2003-2007 Vsevolod Klementjev http://www.codeplex.com/xnadevru | 2 | Bullet for XNA Copyright (c) 2003-2007 Vsevolod Klementjev http://www.codeplex.com/xnadevru |
3 | Bullet original C++ version Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com | 3 | Bullet original C++ version Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com |
4 | 4 | ||
5 | This software is provided 'as-is', without any express or implied | 5 | This software is provided 'as-is', without any express or implied |
6 | warranty. In no event will the authors be held liable for any damages | 6 | warranty. In no event will the authors be held liable for any damages |
7 | arising from the use of this software. | 7 | arising from the use of this software. |
8 | 8 | ||
9 | Permission is granted to anyone to use this software for any purpose, | 9 | Permission is granted to anyone to use this software for any purpose, |
10 | including commercial applications, and to alter it and redistribute it | 10 | including commercial applications, and to alter it and redistribute it |
11 | freely, subject to the following restrictions: | 11 | freely, subject to the following restrictions: |
12 | 12 | ||
13 | 1. The origin of this software must not be misrepresented; you must not | 13 | 1. The origin of this software must not be misrepresented; you must not |
14 | claim that you wrote the original software. If you use this software | 14 | claim that you wrote the original software. If you use this software |
15 | in a product, an acknowledgment in the product documentation would be | 15 | in a product, an acknowledgment in the product documentation would be |
16 | appreciated but is not required. | 16 | appreciated but is not required. |
17 | 2. Altered source versions must be plainly marked as such, and must not be | 17 | 2. Altered source versions must be plainly marked as such, and must not be |
18 | misrepresented as being the original software. | 18 | misrepresented as being the original software. |
19 | 3. This notice may not be removed or altered from any source distribution. | 19 | 3. This notice may not be removed or altered from any source distribution. |
20 | */ | 20 | */ |
21 | 21 | ||
22 | using System; | 22 | using System; |
23 | using System.Collections.Generic; | 23 | using System.Collections.Generic; |
24 | using System.Text; | 24 | using System.Text; |
25 | 25 | ||
26 | namespace XnaDevRu.BulletX | 26 | namespace XnaDevRu.BulletX |
27 | { | 27 | { |
28 | public abstract class OverlappingPairCache : IBroadphase | 28 | public abstract class OverlappingPairCache : IBroadphase |
29 | { | 29 | { |
30 | private static int _overlappingPairCount = 0; | 30 | private static int _overlappingPairCount = 0; |
31 | private List<BroadphasePair> _overlappingPairs = new List<BroadphasePair>(); | 31 | private List<BroadphasePair> _overlappingPairs = new List<BroadphasePair>(); |
32 | //during the dispatch, check that user doesn't destroy/create proxy | 32 | //during the dispatch, check that user doesn't destroy/create proxy |
33 | private bool _blockedForChanges; | 33 | private bool _blockedForChanges; |
34 | 34 | ||
35 | public List<BroadphasePair> OverlappingPairs { get { return _overlappingPairs; } set { _overlappingPairs = value; } } | 35 | public List<BroadphasePair> OverlappingPairs { get { return _overlappingPairs; } set { _overlappingPairs = value; } } |
36 | public bool BlockedForChanges { get { return _blockedForChanges; } set { _blockedForChanges = value; } } | 36 | public bool BlockedForChanges { get { return _blockedForChanges; } set { _blockedForChanges = value; } } |
37 | 37 | ||
38 | public static int OverlappingPairCount { get { return _overlappingPairCount; } set { _overlappingPairCount = value; } } | 38 | public static int OverlappingPairCount { get { return _overlappingPairCount; } set { _overlappingPairCount = value; } } |
39 | 39 | ||
40 | public void RemoveOverlappingPair(BroadphasePair pair) | 40 | public void RemoveOverlappingPair(BroadphasePair pair) |
41 | { | 41 | { |
42 | if (!_overlappingPairs.Contains(pair)) | 42 | if (!_overlappingPairs.Contains(pair)) |
43 | return; | 43 | return; |
44 | 44 | ||
45 | CleanOverlappingPair(ref pair); | 45 | CleanOverlappingPair(ref pair); |
46 | _overlappingPairs.Remove(pair); | 46 | _overlappingPairs.Remove(pair); |
47 | } | 47 | } |
48 | 48 | ||
49 | public void AddOverlappingPair(BroadphaseProxy proxyA, BroadphaseProxy proxyB) | 49 | public void AddOverlappingPair(BroadphaseProxy proxyA, BroadphaseProxy proxyB) |
50 | { | 50 | { |
51 | //don't add overlap with own | 51 | //don't add overlap with own |
52 | bool test = proxyA != proxyB; | 52 | bool test = proxyA != proxyB; |
53 | BulletDebug.Assert(proxyA != proxyB); | 53 | BulletDebug.Assert(proxyA != proxyB); |
54 | 54 | ||
55 | if (!NeedsBroadphaseCollision(proxyA, proxyB)) | 55 | if (!NeedsBroadphaseCollision(proxyA, proxyB)) |
56 | return; | 56 | return; |
57 | 57 | ||
58 | BroadphasePair pair = new BroadphasePair(proxyA, proxyB); | 58 | BroadphasePair pair = new BroadphasePair(proxyA, proxyB); |
59 | _overlappingPairs.Add(pair); | 59 | _overlappingPairs.Add(pair); |
60 | _overlappingPairCount++; | 60 | _overlappingPairCount++; |
61 | } | 61 | } |
62 | 62 | ||
63 | //this FindPair becomes really slow. Either sort the list to speedup the query, or | 63 | //this FindPair becomes really slow. Either sort the list to speedup the query, or |
64 | //use a different solution. It is mainly used for Removing overlapping pairs. Removal could be delayed. | 64 | //use a different solution. It is mainly used for Removing overlapping pairs. Removal could be delayed. |
65 | //we could keep a linked list in each proxy, and store pair in one of the proxies (with lowest memory address) | 65 | //we could keep a linked list in each proxy, and store pair in one of the proxies (with lowest memory address) |
66 | //Also we can use a 2D bitmap, which can be useful for a future GPU implementation | 66 | //Also we can use a 2D bitmap, which can be useful for a future GPU implementation |
67 | public BroadphasePair FindPair(BroadphaseProxy proxyA, BroadphaseProxy proxyB) | 67 | public BroadphasePair FindPair(BroadphaseProxy proxyA, BroadphaseProxy proxyB) |
68 | { | 68 | { |
69 | if (!NeedsBroadphaseCollision(proxyA, proxyB)) | 69 | if (!NeedsBroadphaseCollision(proxyA, proxyB)) |
70 | return null; | 70 | return null; |
71 | 71 | ||
72 | BroadphasePair pair = new BroadphasePair(proxyA, proxyB); | 72 | BroadphasePair pair = new BroadphasePair(proxyA, proxyB); |
73 | for (int i = 0; i < _overlappingPairs.Count; i++) | 73 | for (int i = 0; i < _overlappingPairs.Count; i++) |
74 | { | 74 | { |
75 | if (_overlappingPairs[i] == pair) | 75 | if (_overlappingPairs[i] == pair) |
76 | { | 76 | { |
77 | return _overlappingPairs[i]; | 77 | return _overlappingPairs[i]; |
78 | } | 78 | } |
79 | } | 79 | } |
80 | 80 | ||
81 | return null; | 81 | return null; |
82 | } | 82 | } |
83 | 83 | ||
84 | public void CleanProxyFromPairs(BroadphaseProxy proxy) | 84 | public void CleanProxyFromPairs(BroadphaseProxy proxy) |
85 | { | 85 | { |
86 | for (int i = 0; i < _overlappingPairs.Count; i++) | 86 | for (int i = 0; i < _overlappingPairs.Count; i++) |
87 | { | 87 | { |
88 | BroadphasePair pair = _overlappingPairs[i]; | 88 | BroadphasePair pair = _overlappingPairs[i]; |
89 | if (pair.ProxyA == proxy || | 89 | if (pair.ProxyA == proxy || |
90 | pair.ProxyB == proxy) | 90 | pair.ProxyB == proxy) |
91 | { | 91 | { |
92 | CleanOverlappingPair(ref pair); | 92 | CleanOverlappingPair(ref pair); |
93 | _overlappingPairs[i] = pair; | 93 | _overlappingPairs[i] = pair; |
94 | } | 94 | } |
95 | } | 95 | } |
96 | } | 96 | } |
97 | 97 | ||
98 | public void RemoveOverlappingPairsContainingProxy(BroadphaseProxy proxy) | 98 | public void RemoveOverlappingPairsContainingProxy(BroadphaseProxy proxy) |
99 | { | 99 | { |
100 | for (int i = _overlappingPairs.Count - 1; i >= 0; i--) | 100 | for (int i = _overlappingPairs.Count - 1; i >= 0; i--) |
101 | { | 101 | { |
102 | BroadphasePair pair = _overlappingPairs[i]; | 102 | BroadphasePair pair = _overlappingPairs[i]; |
103 | if (pair.ProxyA == proxy || | 103 | if (pair.ProxyA == proxy || |
104 | pair.ProxyB == proxy) | 104 | pair.ProxyB == proxy) |
105 | { | 105 | { |
106 | RemoveOverlappingPair(pair); | 106 | RemoveOverlappingPair(pair); |
107 | i++; | 107 | i++; |
108 | } | 108 | } |
109 | } | 109 | } |
110 | } | 110 | } |
111 | 111 | ||
112 | public bool NeedsBroadphaseCollision(BroadphaseProxy proxy0, BroadphaseProxy proxy1) | 112 | public bool NeedsBroadphaseCollision(BroadphaseProxy proxy0, BroadphaseProxy proxy1) |
113 | { | 113 | { |
114 | bool collides = (proxy0.CollisionFilterGroup & proxy1.CollisionFilterMask) != 0; | 114 | bool collides = (proxy0.CollisionFilterGroup & proxy1.CollisionFilterMask) != 0; |
115 | collides = collides && ((proxy1.CollisionFilterGroup & proxy0.CollisionFilterMask) != 0); | 115 | collides = collides && ((proxy1.CollisionFilterGroup & proxy0.CollisionFilterMask) != 0); |
116 | 116 | ||
117 | return collides; | 117 | return collides; |
118 | } | 118 | } |
119 | 119 | ||
120 | public virtual void ProcessAllOverlappingPairs(IOverlapCallback callback) | 120 | public virtual void ProcessAllOverlappingPairs(IOverlapCallback callback) |
121 | { | 121 | { |
122 | List<BroadphasePair> deleting = new List<BroadphasePair>(); | 122 | List<BroadphasePair> deleting = new List<BroadphasePair>(); |
123 | for (int i = 0; i < _overlappingPairs.Count; i++) | 123 | for (int i = 0; i < _overlappingPairs.Count; i++) |
124 | { | 124 | { |
125 | BroadphasePair p = _overlappingPairs[i]; | 125 | BroadphasePair p = _overlappingPairs[i]; |
126 | if (callback.ProcessOverlap(ref p)) | 126 | if (callback.ProcessOverlap(ref p)) |
127 | { | 127 | { |
128 | CleanOverlappingPair(ref p); | 128 | CleanOverlappingPair(ref p); |
129 | deleting.Add(p); | 129 | deleting.Add(p); |
130 | _overlappingPairCount--; | 130 | _overlappingPairCount--; |
131 | } | 131 | } |
132 | } | 132 | } |
133 | 133 | ||
134 | for (int i = 0; i < deleting.Count; i++) | 134 | for (int i = 0; i < deleting.Count; i++) |
135 | _overlappingPairs.Remove(deleting[i]); | 135 | _overlappingPairs.Remove(deleting[i]); |
136 | } | 136 | } |
137 | 137 | ||
138 | public void CleanOverlappingPair(ref BroadphasePair pair) | 138 | public void CleanOverlappingPair(ref BroadphasePair pair) |
139 | { | 139 | { |
140 | if (pair.CollisionAlgorithm != null) | 140 | if (pair.CollisionAlgorithm != null) |
141 | { | 141 | { |
142 | if (pair.CollisionAlgorithm is IDisposable) | 142 | if (pair.CollisionAlgorithm is IDisposable) |
143 | (pair.CollisionAlgorithm as IDisposable).Dispose(); | 143 | (pair.CollisionAlgorithm as IDisposable).Dispose(); |
144 | pair.CollisionAlgorithm = null; | 144 | pair.CollisionAlgorithm = null; |
145 | } | 145 | } |
146 | } | 146 | } |
147 | 147 | ||
148 | public abstract void RefreshOverlappingPairs(); | 148 | public abstract void RefreshOverlappingPairs(); |
149 | 149 | ||
150 | #region IBroadphase Members | 150 | #region IBroadphase Members |
151 | public abstract BroadphaseProxy CreateProxy(MonoXnaCompactMaths.Vector3 min, MonoXnaCompactMaths.Vector3 max, BroadphaseNativeTypes shapeType, object userData, BroadphaseProxy.CollisionFilterGroups collisionFilterGroup, BroadphaseProxy.CollisionFilterGroups collisionFilterMask); | 151 | public abstract BroadphaseProxy CreateProxy(MonoXnaCompactMaths.Vector3 min, MonoXnaCompactMaths.Vector3 max, BroadphaseNativeTypes shapeType, object userData, BroadphaseProxy.CollisionFilterGroups collisionFilterGroup, BroadphaseProxy.CollisionFilterGroups collisionFilterMask); |
152 | 152 | ||
153 | public abstract void DestroyProxy(BroadphaseProxy proxy); | 153 | public abstract void DestroyProxy(BroadphaseProxy proxy); |
154 | 154 | ||
155 | public abstract void SetAabb(BroadphaseProxy proxy, MonoXnaCompactMaths.Vector3 aabbMin, MonoXnaCompactMaths.Vector3 aabbMax); | 155 | public abstract void SetAabb(BroadphaseProxy proxy, MonoXnaCompactMaths.Vector3 aabbMin, MonoXnaCompactMaths.Vector3 aabbMax); |
156 | 156 | ||
157 | #endregion | 157 | #endregion |
158 | } | 158 | } |
159 | } | 159 | } |