diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionDispatch/CompoundCollisionAlgorithm.cs | 314 |
1 files changed, 157 insertions, 157 deletions
diff --git a/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionDispatch/CompoundCollisionAlgorithm.cs b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionDispatch/CompoundCollisionAlgorithm.cs index 3fec7d0..6f2278a 100644 --- a/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionDispatch/CompoundCollisionAlgorithm.cs +++ b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionDispatch/CompoundCollisionAlgorithm.cs | |||
@@ -1,157 +1,157 @@ | |||
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 | using System.Diagnostics; | 25 | using System.Diagnostics; |
26 | using MonoXnaCompactMaths; | 26 | using MonoXnaCompactMaths; |
27 | 27 | ||
28 | namespace XnaDevRu.BulletX | 28 | namespace XnaDevRu.BulletX |
29 | { | 29 | { |
30 | public class CompoundCollisionAlgorithm : CollisionAlgorithm | 30 | public class CompoundCollisionAlgorithm : CollisionAlgorithm |
31 | { | 31 | { |
32 | private List<CollisionAlgorithm> _childCollisionAlgorithms; | 32 | private List<CollisionAlgorithm> _childCollisionAlgorithms; |
33 | private bool _isSwapped; | 33 | private bool _isSwapped; |
34 | 34 | ||
35 | public CompoundCollisionAlgorithm( | 35 | public CompoundCollisionAlgorithm( |
36 | CollisionAlgorithmConstructionInfo collisionAlgorithmConstructionInfo, | 36 | CollisionAlgorithmConstructionInfo collisionAlgorithmConstructionInfo, |
37 | CollisionObject bodyA, | 37 | CollisionObject bodyA, |
38 | CollisionObject bodyB, bool isSwapped) | 38 | CollisionObject bodyB, bool isSwapped) |
39 | : base(collisionAlgorithmConstructionInfo) | 39 | : base(collisionAlgorithmConstructionInfo) |
40 | { | 40 | { |
41 | //Begin | 41 | //Begin |
42 | _isSwapped = isSwapped; | 42 | _isSwapped = isSwapped; |
43 | 43 | ||
44 | CollisionObject collisionObject = isSwapped ? bodyB : bodyA; | 44 | CollisionObject collisionObject = isSwapped ? bodyB : bodyA; |
45 | CollisionObject otherObject = isSwapped ? bodyA : bodyB; | 45 | CollisionObject otherObject = isSwapped ? bodyA : bodyB; |
46 | 46 | ||
47 | BulletDebug.Assert(collisionObject.CollisionShape.IsCompound); | 47 | BulletDebug.Assert(collisionObject.CollisionShape.IsCompound); |
48 | 48 | ||
49 | CompoundShape compoundShape = collisionObject.CollisionShape as CompoundShape; | 49 | CompoundShape compoundShape = collisionObject.CollisionShape as CompoundShape; |
50 | int childrenNumber = compoundShape.ChildShapeCount; | 50 | int childrenNumber = compoundShape.ChildShapeCount; |
51 | int index = 0; | 51 | int index = 0; |
52 | 52 | ||
53 | _childCollisionAlgorithms = new List<CollisionAlgorithm>(childrenNumber); | 53 | _childCollisionAlgorithms = new List<CollisionAlgorithm>(childrenNumber); |
54 | 54 | ||
55 | for (index = 0; index < childrenNumber; index++) | 55 | for (index = 0; index < childrenNumber; index++) |
56 | { | 56 | { |
57 | CollisionShape childShape = compoundShape.GetChildShape(index); | 57 | CollisionShape childShape = compoundShape.GetChildShape(index); |
58 | CollisionShape orgShape = collisionObject.CollisionShape; | 58 | CollisionShape orgShape = collisionObject.CollisionShape; |
59 | 59 | ||
60 | collisionObject.CollisionShape = childShape; | 60 | collisionObject.CollisionShape = childShape; |
61 | _childCollisionAlgorithms[index] = collisionAlgorithmConstructionInfo.Dispatcher.FindAlgorithm(collisionObject, otherObject); | 61 | _childCollisionAlgorithms[index] = collisionAlgorithmConstructionInfo.Dispatcher.FindAlgorithm(collisionObject, otherObject); |
62 | collisionObject.CollisionShape = orgShape; | 62 | collisionObject.CollisionShape = orgShape; |
63 | } | 63 | } |
64 | } | 64 | } |
65 | 65 | ||
66 | public override void ProcessCollision( | 66 | public override void ProcessCollision( |
67 | CollisionObject bodyA, | 67 | CollisionObject bodyA, |
68 | CollisionObject bodyB, | 68 | CollisionObject bodyB, |
69 | DispatcherInfo dispatchInfo, ManifoldResult resultOut) | 69 | DispatcherInfo dispatchInfo, ManifoldResult resultOut) |
70 | { | 70 | { |
71 | //Begin | 71 | //Begin |
72 | 72 | ||
73 | CollisionObject collisionObject = _isSwapped ? bodyB : bodyB; | 73 | CollisionObject collisionObject = _isSwapped ? bodyB : bodyB; |
74 | CollisionObject otherObject = _isSwapped ? bodyA : bodyB; | 74 | CollisionObject otherObject = _isSwapped ? bodyA : bodyB; |
75 | 75 | ||
76 | //Debug.Assert(collisionObject.getCollisionShape().isCompound()); | 76 | //Debug.Assert(collisionObject.getCollisionShape().isCompound()); |
77 | BulletDebug.Assert(collisionObject.CollisionShape.IsCompound); | 77 | BulletDebug.Assert(collisionObject.CollisionShape.IsCompound); |
78 | 78 | ||
79 | CompoundShape compoundShape = (CompoundShape)collisionObject.CollisionShape; | 79 | CompoundShape compoundShape = (CompoundShape)collisionObject.CollisionShape; |
80 | 80 | ||
81 | int childrenNumber = _childCollisionAlgorithms.Count; | 81 | int childrenNumber = _childCollisionAlgorithms.Count; |
82 | 82 | ||
83 | for (int i = 0; i < childrenNumber; i++) | 83 | for (int i = 0; i < childrenNumber; i++) |
84 | { | 84 | { |
85 | CompoundShape childShape = compoundShape.GetChildShape(i) as CompoundShape; | 85 | CompoundShape childShape = compoundShape.GetChildShape(i) as CompoundShape; |
86 | 86 | ||
87 | Matrix orgTransform = collisionObject.WorldTransform; | 87 | Matrix orgTransform = collisionObject.WorldTransform; |
88 | CollisionShape orgShape = collisionObject.CollisionShape; | 88 | CollisionShape orgShape = collisionObject.CollisionShape; |
89 | 89 | ||
90 | Matrix childTransform = compoundShape.GetChildTransform(i); | 90 | Matrix childTransform = compoundShape.GetChildTransform(i); |
91 | Matrix newChildWorld = orgTransform * childTransform; | 91 | Matrix newChildWorld = orgTransform * childTransform; |
92 | 92 | ||
93 | collisionObject.WorldTransform = newChildWorld; | 93 | collisionObject.WorldTransform = newChildWorld; |
94 | collisionObject.CollisionShape = childShape; | 94 | collisionObject.CollisionShape = childShape; |
95 | _childCollisionAlgorithms[i].ProcessCollision(collisionObject, otherObject, dispatchInfo, resultOut); | 95 | _childCollisionAlgorithms[i].ProcessCollision(collisionObject, otherObject, dispatchInfo, resultOut); |
96 | 96 | ||
97 | collisionObject.CollisionShape = orgShape; | 97 | collisionObject.CollisionShape = orgShape; |
98 | collisionObject.WorldTransform = orgTransform; | 98 | collisionObject.WorldTransform = orgTransform; |
99 | } | 99 | } |
100 | } | 100 | } |
101 | 101 | ||
102 | public override float CalculateTimeOfImpact(CollisionObject bodyA, CollisionObject bodyB, DispatcherInfo dispatchInfo, ManifoldResult resultOut) | 102 | public override float CalculateTimeOfImpact(CollisionObject bodyA, CollisionObject bodyB, DispatcherInfo dispatchInfo, ManifoldResult resultOut) |
103 | { | 103 | { |
104 | CollisionObject collisionObject = _isSwapped ? bodyB : bodyA; | 104 | CollisionObject collisionObject = _isSwapped ? bodyB : bodyA; |
105 | CollisionObject otherObject = _isSwapped ? bodyA : bodyB; | 105 | CollisionObject otherObject = _isSwapped ? bodyA : bodyB; |
106 | 106 | ||
107 | BulletDebug.Assert(collisionObject.CollisionShape.IsCompound); | 107 | BulletDebug.Assert(collisionObject.CollisionShape.IsCompound); |
108 | 108 | ||
109 | CompoundShape compoundShape = (CompoundShape)collisionObject.CollisionShape; | 109 | CompoundShape compoundShape = (CompoundShape)collisionObject.CollisionShape; |
110 | 110 | ||
111 | float hitFraction = 1.0f; | 111 | float hitFraction = 1.0f; |
112 | 112 | ||
113 | for (int i = 0; i < _childCollisionAlgorithms.Count; i++) | 113 | for (int i = 0; i < _childCollisionAlgorithms.Count; i++) |
114 | { | 114 | { |
115 | CollisionShape childShape = compoundShape.GetChildShape(i); | 115 | CollisionShape childShape = compoundShape.GetChildShape(i); |
116 | 116 | ||
117 | Matrix orgTransform = collisionObject.WorldTransform; | 117 | Matrix orgTransform = collisionObject.WorldTransform; |
118 | CollisionShape orgShape = collisionObject.CollisionShape; | 118 | CollisionShape orgShape = collisionObject.CollisionShape; |
119 | 119 | ||
120 | Matrix childTransform = compoundShape.GetChildTransform(i); | 120 | Matrix childTransform = compoundShape.GetChildTransform(i); |
121 | Matrix newChildWorld = orgTransform * childTransform; | 121 | Matrix newChildWorld = orgTransform * childTransform; |
122 | collisionObject.WorldTransform = newChildWorld; | 122 | collisionObject.WorldTransform = newChildWorld; |
123 | 123 | ||
124 | collisionObject.CollisionShape = childShape; | 124 | collisionObject.CollisionShape = childShape; |
125 | float frac = _childCollisionAlgorithms[i].CalculateTimeOfImpact( | 125 | float frac = _childCollisionAlgorithms[i].CalculateTimeOfImpact( |
126 | collisionObject, otherObject, dispatchInfo, resultOut | 126 | collisionObject, otherObject, dispatchInfo, resultOut |
127 | ); | 127 | ); |
128 | 128 | ||
129 | if (frac < hitFraction) | 129 | if (frac < hitFraction) |
130 | { | 130 | { |
131 | hitFraction = frac; | 131 | hitFraction = frac; |
132 | } | 132 | } |
133 | 133 | ||
134 | collisionObject.CollisionShape = orgShape; | 134 | collisionObject.CollisionShape = orgShape; |
135 | collisionObject.WorldTransform = orgTransform; | 135 | collisionObject.WorldTransform = orgTransform; |
136 | } | 136 | } |
137 | 137 | ||
138 | return hitFraction; | 138 | return hitFraction; |
139 | } | 139 | } |
140 | 140 | ||
141 | public class CreateFunc : CollisionAlgorithmCreateFunction | 141 | public class CreateFunc : CollisionAlgorithmCreateFunction |
142 | { | 142 | { |
143 | public override CollisionAlgorithm CreateCollisionAlgorithm(CollisionAlgorithmConstructionInfo collisionAlgorithmConstructionInfo, CollisionObject bodyA, CollisionObject bodyB) | 143 | public override CollisionAlgorithm CreateCollisionAlgorithm(CollisionAlgorithmConstructionInfo collisionAlgorithmConstructionInfo, CollisionObject bodyA, CollisionObject bodyB) |
144 | { | 144 | { |
145 | return new CompoundCollisionAlgorithm(collisionAlgorithmConstructionInfo, bodyA, bodyB, false); | 145 | return new CompoundCollisionAlgorithm(collisionAlgorithmConstructionInfo, bodyA, bodyB, false); |
146 | } | 146 | } |
147 | }; | 147 | }; |
148 | 148 | ||
149 | public class SwappedCreateFunc : CollisionAlgorithmCreateFunction | 149 | public class SwappedCreateFunc : CollisionAlgorithmCreateFunction |
150 | { | 150 | { |
151 | public override CollisionAlgorithm CreateCollisionAlgorithm(CollisionAlgorithmConstructionInfo collisionAlgorithmConstructionInfo, CollisionObject bodyA, CollisionObject bodyB) | 151 | public override CollisionAlgorithm CreateCollisionAlgorithm(CollisionAlgorithmConstructionInfo collisionAlgorithmConstructionInfo, CollisionObject bodyA, CollisionObject bodyB) |
152 | { | 152 | { |
153 | return new CompoundCollisionAlgorithm(collisionAlgorithmConstructionInfo, bodyA, bodyB, true); | 153 | return new CompoundCollisionAlgorithm(collisionAlgorithmConstructionInfo, bodyA, bodyB, true); |
154 | } | 154 | } |
155 | }; | 155 | }; |
156 | } | 156 | } |
157 | } | 157 | } |