diff options
Diffstat (limited to 'libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/CompoundShape.cs')
-rw-r--r-- | libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/CompoundShape.cs | 366 |
1 files changed, 183 insertions, 183 deletions
diff --git a/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/CompoundShape.cs b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/CompoundShape.cs index 08aeec8..bd13ffd 100644 --- a/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/CompoundShape.cs +++ b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/CompoundShape.cs | |||
@@ -1,183 +1,183 @@ | |||
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 MonoXnaCompactMaths; | 25 | using MonoXnaCompactMaths; |
26 | 26 | ||
27 | namespace XnaDevRu.BulletX | 27 | namespace XnaDevRu.BulletX |
28 | { | 28 | { |
29 | /// <summary> | 29 | /// <summary> |
30 | /// CompoundShape allows to store multiple other CollisionShapes | 30 | /// CompoundShape allows to store multiple other CollisionShapes |
31 | /// This allows for concave collision objects. This is more general then the Static Concave TriangleMeshShape. | 31 | /// This allows for concave collision objects. This is more general then the Static Concave TriangleMeshShape. |
32 | /// </summary> | 32 | /// </summary> |
33 | public class CompoundShape : CollisionShape | 33 | public class CompoundShape : CollisionShape |
34 | { | 34 | { |
35 | private List<Matrix> _childTransforms = new List<Matrix>(); | 35 | private List<Matrix> _childTransforms = new List<Matrix>(); |
36 | private List<CollisionShape> _childShapes = new List<CollisionShape>(); | 36 | private List<CollisionShape> _childShapes = new List<CollisionShape>(); |
37 | private Vector3 _localAabbMin; | 37 | private Vector3 _localAabbMin; |
38 | private Vector3 _localAabbMax; | 38 | private Vector3 _localAabbMax; |
39 | 39 | ||
40 | private OptimizedBvh _aabbTree; | 40 | private OptimizedBvh _aabbTree; |
41 | private float _collisionMargin; | 41 | private float _collisionMargin; |
42 | private Vector3 _localScaling; | 42 | private Vector3 _localScaling; |
43 | 43 | ||
44 | public CompoundShape() | 44 | public CompoundShape() |
45 | { | 45 | { |
46 | _localAabbMin = new Vector3(1e30f, 1e30f, 1e30f); | 46 | _localAabbMin = new Vector3(1e30f, 1e30f, 1e30f); |
47 | _localAabbMax = new Vector3(-1e30f, -1e30f, -1e30f); | 47 | _localAabbMax = new Vector3(-1e30f, -1e30f, -1e30f); |
48 | _aabbTree = null; | 48 | _aabbTree = null; |
49 | _collisionMargin = 0f; | 49 | _collisionMargin = 0f; |
50 | _localScaling = new Vector3(1f, 1f, 1f); | 50 | _localScaling = new Vector3(1f, 1f, 1f); |
51 | } | 51 | } |
52 | 52 | ||
53 | public override void GetAabb(Matrix t, out Vector3 aabbMin, out Vector3 aabbMax) | 53 | public override void GetAabb(Matrix t, out Vector3 aabbMin, out Vector3 aabbMax) |
54 | { | 54 | { |
55 | Vector3 localHalfExtents = 0.5f * (_localAabbMax - _localAabbMin); | 55 | Vector3 localHalfExtents = 0.5f * (_localAabbMax - _localAabbMin); |
56 | Vector3 localCenter = 0.5f * (_localAabbMax + _localAabbMin); | 56 | Vector3 localCenter = 0.5f * (_localAabbMax + _localAabbMin); |
57 | 57 | ||
58 | Matrix abs_b = MathHelper.Absolute(t); | 58 | Matrix abs_b = MathHelper.Absolute(t); |
59 | 59 | ||
60 | Vector3 row1 = new Vector3(abs_b.M11, abs_b.M12, abs_b.M13); | 60 | Vector3 row1 = new Vector3(abs_b.M11, abs_b.M12, abs_b.M13); |
61 | Vector3 row2 = new Vector3(abs_b.M21, abs_b.M22, abs_b.M23); | 61 | Vector3 row2 = new Vector3(abs_b.M21, abs_b.M22, abs_b.M23); |
62 | Vector3 row3 = new Vector3(abs_b.M31, abs_b.M32, abs_b.M33); | 62 | Vector3 row3 = new Vector3(abs_b.M31, abs_b.M32, abs_b.M33); |
63 | 63 | ||
64 | Vector3 center = new Vector3(Vector3.Dot(row1, localCenter) + t.Translation.X, | 64 | Vector3 center = new Vector3(Vector3.Dot(row1, localCenter) + t.Translation.X, |
65 | Vector3.Dot(row2, localCenter) + t.Translation.Y, | 65 | Vector3.Dot(row2, localCenter) + t.Translation.Y, |
66 | Vector3.Dot(row3, localCenter) + t.Translation.Z); | 66 | Vector3.Dot(row3, localCenter) + t.Translation.Z); |
67 | 67 | ||
68 | Vector3 extent = new Vector3(Vector3.Dot(row1, localHalfExtents), | 68 | Vector3 extent = new Vector3(Vector3.Dot(row1, localHalfExtents), |
69 | Vector3.Dot(row2, localHalfExtents), | 69 | Vector3.Dot(row2, localHalfExtents), |
70 | Vector3.Dot(row3, localHalfExtents)); | 70 | Vector3.Dot(row3, localHalfExtents)); |
71 | 71 | ||
72 | aabbMin = center - extent; | 72 | aabbMin = center - extent; |
73 | aabbMax = center + extent; | 73 | aabbMax = center + extent; |
74 | } | 74 | } |
75 | 75 | ||
76 | public override BroadphaseNativeTypes ShapeType | 76 | public override BroadphaseNativeTypes ShapeType |
77 | { | 77 | { |
78 | get | 78 | get |
79 | { | 79 | { |
80 | return BroadphaseNativeTypes.Compound; | 80 | return BroadphaseNativeTypes.Compound; |
81 | } | 81 | } |
82 | } | 82 | } |
83 | 83 | ||
84 | public override Vector3 LocalScaling | 84 | public override Vector3 LocalScaling |
85 | { | 85 | { |
86 | get | 86 | get |
87 | { | 87 | { |
88 | return _localScaling; | 88 | return _localScaling; |
89 | } | 89 | } |
90 | set | 90 | set |
91 | { | 91 | { |
92 | _localScaling = value; | 92 | _localScaling = value; |
93 | } | 93 | } |
94 | } | 94 | } |
95 | 95 | ||
96 | public override string Name | 96 | public override string Name |
97 | { | 97 | { |
98 | get | 98 | get |
99 | { | 99 | { |
100 | return "Compound"; | 100 | return "Compound"; |
101 | } | 101 | } |
102 | } | 102 | } |
103 | 103 | ||
104 | public override float Margin | 104 | public override float Margin |
105 | { | 105 | { |
106 | get | 106 | get |
107 | { | 107 | { |
108 | return _collisionMargin; | 108 | return _collisionMargin; |
109 | } | 109 | } |
110 | set | 110 | set |
111 | { | 111 | { |
112 | _collisionMargin = value; | 112 | _collisionMargin = value; |
113 | } | 113 | } |
114 | } | 114 | } |
115 | 115 | ||
116 | public int ChildShapeCount { get { return _childShapes.Count; } } | 116 | public int ChildShapeCount { get { return _childShapes.Count; } } |
117 | //this is optional, but should make collision queries faster, by culling non-overlapping nodes | 117 | //this is optional, but should make collision queries faster, by culling non-overlapping nodes |
118 | public OptimizedBvh AabbTree { get { return _aabbTree; } } | 118 | public OptimizedBvh AabbTree { get { return _aabbTree; } } |
119 | 119 | ||
120 | public CollisionShape GetChildShape(int index) | 120 | public CollisionShape GetChildShape(int index) |
121 | { | 121 | { |
122 | return _childShapes[index]; | 122 | return _childShapes[index]; |
123 | } | 123 | } |
124 | 124 | ||
125 | public Matrix GetChildTransform(int index) | 125 | public Matrix GetChildTransform(int index) |
126 | { | 126 | { |
127 | return _childTransforms[index]; | 127 | return _childTransforms[index]; |
128 | } | 128 | } |
129 | 129 | ||
130 | public override void CalculateLocalInertia(float mass, out Vector3 inertia) | 130 | public override void CalculateLocalInertia(float mass, out Vector3 inertia) |
131 | { | 131 | { |
132 | //approximation: take the inertia from the aabb for now | 132 | //approximation: take the inertia from the aabb for now |
133 | Matrix ident = Matrix.Identity; | 133 | Matrix ident = Matrix.Identity; |
134 | Vector3 aabbMin, aabbMax; | 134 | Vector3 aabbMin, aabbMax; |
135 | GetAabb(ident, out aabbMin, out aabbMax); | 135 | GetAabb(ident, out aabbMin, out aabbMax); |
136 | 136 | ||
137 | Vector3 halfExtents = (aabbMax - aabbMin) * 0.5f; | 137 | Vector3 halfExtents = (aabbMax - aabbMin) * 0.5f; |
138 | 138 | ||
139 | float lx = 2f * (halfExtents.X); | 139 | float lx = 2f * (halfExtents.X); |
140 | float ly = 2f * (halfExtents.Y); | 140 | float ly = 2f * (halfExtents.Y); |
141 | float lz = 2f * (halfExtents.Z); | 141 | float lz = 2f * (halfExtents.Z); |
142 | 142 | ||
143 | inertia = new Vector3(); | 143 | inertia = new Vector3(); |
144 | inertia.X = mass / (12.0f) * (ly * ly + lz * lz); | 144 | inertia.X = mass / (12.0f) * (ly * ly + lz * lz); |
145 | inertia.Y = mass / (12.0f) * (lx * lx + lz * lz); | 145 | inertia.Y = mass / (12.0f) * (lx * lx + lz * lz); |
146 | inertia.Z = mass / (12.0f) * (lx * lx + ly * ly); | 146 | inertia.Z = mass / (12.0f) * (lx * lx + ly * ly); |
147 | } | 147 | } |
148 | 148 | ||
149 | public void AddChildShape(Matrix localTransform, CollisionShape shape) | 149 | public void AddChildShape(Matrix localTransform, CollisionShape shape) |
150 | { | 150 | { |
151 | _childTransforms.Add(localTransform); | 151 | _childTransforms.Add(localTransform); |
152 | _childShapes.Add(shape); | 152 | _childShapes.Add(shape); |
153 | 153 | ||
154 | //extend the local aabbMin/aabbMax | 154 | //extend the local aabbMin/aabbMax |
155 | Vector3 localAabbMin, localAabbMax; | 155 | Vector3 localAabbMin, localAabbMax; |
156 | shape.GetAabb(localTransform, out localAabbMin, out localAabbMax); | 156 | shape.GetAabb(localTransform, out localAabbMin, out localAabbMax); |
157 | if (_localAabbMin.X > localAabbMin.X) | 157 | if (_localAabbMin.X > localAabbMin.X) |
158 | { | 158 | { |
159 | _localAabbMin.X = localAabbMin.X; | 159 | _localAabbMin.X = localAabbMin.X; |
160 | } | 160 | } |
161 | if (_localAabbMax.X < localAabbMax.X) | 161 | if (_localAabbMax.X < localAabbMax.X) |
162 | { | 162 | { |
163 | _localAabbMax.X = localAabbMax.X; | 163 | _localAabbMax.X = localAabbMax.X; |
164 | } | 164 | } |
165 | if (_localAabbMin.Y > localAabbMin.Y) | 165 | if (_localAabbMin.Y > localAabbMin.Y) |
166 | { | 166 | { |
167 | _localAabbMin.Y = localAabbMin.Y; | 167 | _localAabbMin.Y = localAabbMin.Y; |
168 | } | 168 | } |
169 | if (_localAabbMax.Y < localAabbMax.Y) | 169 | if (_localAabbMax.Y < localAabbMax.Y) |
170 | { | 170 | { |
171 | _localAabbMax.Y = localAabbMax.Y; | 171 | _localAabbMax.Y = localAabbMax.Y; |
172 | } | 172 | } |
173 | if (_localAabbMin.Z > localAabbMin.Z) | 173 | if (_localAabbMin.Z > localAabbMin.Z) |
174 | { | 174 | { |
175 | _localAabbMin.Z = localAabbMin.Z; | 175 | _localAabbMin.Z = localAabbMin.Z; |
176 | } | 176 | } |
177 | if (_localAabbMax.Z < localAabbMax.Z) | 177 | if (_localAabbMax.Z < localAabbMax.Z) |
178 | { | 178 | { |
179 | _localAabbMax.Z = localAabbMax.Z; | 179 | _localAabbMax.Z = localAabbMax.Z; |
180 | } | 180 | } |
181 | } | 181 | } |
182 | } | 182 | } |
183 | } | 183 | } |