diff options
Diffstat (limited to 'libraries/ModifiedBulletX/ModifiedBulletX/Dynamics/SimpleDynamicsWorld.cs')
-rw-r--r-- | libraries/ModifiedBulletX/ModifiedBulletX/Dynamics/SimpleDynamicsWorld.cs | 211 |
1 files changed, 211 insertions, 0 deletions
diff --git a/libraries/ModifiedBulletX/ModifiedBulletX/Dynamics/SimpleDynamicsWorld.cs b/libraries/ModifiedBulletX/ModifiedBulletX/Dynamics/SimpleDynamicsWorld.cs new file mode 100644 index 0000000..aa91a03 --- /dev/null +++ b/libraries/ModifiedBulletX/ModifiedBulletX/Dynamics/SimpleDynamicsWorld.cs | |||
@@ -0,0 +1,211 @@ | |||
1 | /* | ||
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 | ||
4 | |||
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 | ||
7 | arising from the use of this software. | ||
8 | |||
9 | Permission is granted to anyone to use this software for any purpose, | ||
10 | including commercial applications, and to alter it and redistribute it | ||
11 | freely, subject to the following restrictions: | ||
12 | |||
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 | ||
15 | in a product, an acknowledgment in the product documentation would be | ||
16 | appreciated but is not required. | ||
17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
18 | misrepresented as being the original software. | ||
19 | 3. This notice may not be removed or altered from any source distribution. | ||
20 | */ | ||
21 | |||
22 | using System; | ||
23 | using System.Collections.Generic; | ||
24 | using System.Text; | ||
25 | using MonoXnaCompactMaths; | ||
26 | |||
27 | namespace XnaDevRu.BulletX.Dynamics | ||
28 | { | ||
29 | public class SimpleDynamicsWorld : DynamicsWorld | ||
30 | { | ||
31 | private IConstraintSolver _constraintSolver; | ||
32 | private bool _ownsConstraintSolver; | ||
33 | private Vector3 _gravity; | ||
34 | private IDebugDraw _debugDrawer; | ||
35 | |||
36 | /// <summary> | ||
37 | /// this btSimpleDynamicsWorld constructor creates dispatcher, broadphase pairCache and constraintSolver | ||
38 | /// </summary> | ||
39 | /// <param name="dispatcher"></param> | ||
40 | /// <param name="pairCache"></param> | ||
41 | /// <param name="constraintSolver"></param> | ||
42 | public SimpleDynamicsWorld(IDispatcher dispatcher, OverlappingPairCache pairCache, IConstraintSolver constraintSolver) | ||
43 | : base(dispatcher, pairCache) | ||
44 | { | ||
45 | _constraintSolver = constraintSolver; | ||
46 | _ownsConstraintSolver = false; | ||
47 | _gravity = new Vector3(0, 0, -10); | ||
48 | } | ||
49 | |||
50 | public override Vector3 Gravity | ||
51 | { | ||
52 | set | ||
53 | { | ||
54 | _gravity = value; | ||
55 | for (int i = 0; i < CollisionObjects.Count; i++) | ||
56 | { | ||
57 | CollisionObject colObj = CollisionObjects[i]; | ||
58 | RigidBody body = RigidBody.Upcast(colObj); | ||
59 | if (body != null) | ||
60 | { | ||
61 | body.Gravity = value; | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | } | ||
66 | |||
67 | public override IConstraintSolver ConstraintSolver | ||
68 | { | ||
69 | set | ||
70 | { | ||
71 | _ownsConstraintSolver = false; | ||
72 | _constraintSolver = value; | ||
73 | } | ||
74 | } | ||
75 | |||
76 | public override IDebugDraw DebugDrawer | ||
77 | { | ||
78 | get | ||
79 | { | ||
80 | return _debugDrawer; | ||
81 | } | ||
82 | set | ||
83 | { | ||
84 | _debugDrawer = value; | ||
85 | } | ||
86 | } | ||
87 | |||
88 | public override void StepSimulation(float timeStep, int numSubsteps, float fixedTimeStep) | ||
89 | { | ||
90 | //apply gravity, predict motion | ||
91 | PredictUnconstraintMotion(timeStep); | ||
92 | |||
93 | DispatcherInfo dispatchInfo = new DispatcherInfo(); | ||
94 | dispatchInfo.TimeStep = timeStep; | ||
95 | dispatchInfo.StepCount = 0; | ||
96 | dispatchInfo.DebugDraw = DebugDrawer; | ||
97 | //perform collision detection | ||
98 | PerformDiscreteCollisionDetection(); | ||
99 | |||
100 | //solve contact constraints | ||
101 | int numManifolds = Dispatcher.ManifoldCount; | ||
102 | if (numManifolds != 0) | ||
103 | { | ||
104 | |||
105 | List<PersistentManifold> manifolds = (Dispatcher as CollisionDispatcher).Manifolds; | ||
106 | //int numManifolds = m_dispatcher1.GetNumManifolds(); | ||
107 | ContactSolverInfo infoGlobal = new ContactSolverInfo(); | ||
108 | infoGlobal.TimeStep = timeStep; | ||
109 | |||
110 | _constraintSolver.SolveGroup(new List<CollisionObject>(), manifolds, manifolds.Count, new List<TypedConstraint>(), infoGlobal, _debugDrawer); | ||
111 | } | ||
112 | //integrate transforms | ||
113 | IntegrateTransforms(timeStep); | ||
114 | |||
115 | UpdateAabbs(); | ||
116 | |||
117 | SynchronizeMotionStates(); | ||
118 | } | ||
119 | |||
120 | public override void UpdateAabbs() | ||
121 | { | ||
122 | for (int i = 0; i < CollisionObjects.Count; i++) | ||
123 | { | ||
124 | CollisionObject colObj = CollisionObjects[i]; | ||
125 | RigidBody body = RigidBody.Upcast(colObj); | ||
126 | if (body != null) | ||
127 | { | ||
128 | if (body.IsActive && (!body.IsStaticObject)) | ||
129 | { | ||
130 | Vector3 minAabb, maxAabb; | ||
131 | colObj.CollisionShape.GetAabb(colObj.WorldTransform, out minAabb, out maxAabb); | ||
132 | IBroadphase bp = Broadphase; | ||
133 | bp.SetAabb(body.Broadphase, minAabb, maxAabb); | ||
134 | } | ||
135 | } | ||
136 | } | ||
137 | } | ||
138 | |||
139 | public override void AddRigidBody(RigidBody body) | ||
140 | { | ||
141 | body.Gravity = _gravity; | ||
142 | |||
143 | if (body.CollisionShape != null) | ||
144 | { | ||
145 | AddCollisionObject(body); | ||
146 | } | ||
147 | } | ||
148 | |||
149 | public override void RemoveRigidBody(RigidBody body) | ||
150 | { | ||
151 | RemoveCollisionObject(body); | ||
152 | } | ||
153 | |||
154 | public void SynchronizeMotionStates() | ||
155 | { | ||
156 | for (int i = 0; i < CollisionObjects.Count; i++) | ||
157 | { | ||
158 | CollisionObject colObj = CollisionObjects[i]; | ||
159 | RigidBody body = RigidBody.Upcast(colObj); | ||
160 | if (body != null && body.MotionState != null) | ||
161 | { | ||
162 | if (body.ActivationState != ActivationState.IslandSleeping) | ||
163 | { | ||
164 | body.MotionState.SetWorldTransform(body.WorldTransform); | ||
165 | } | ||
166 | } | ||
167 | } | ||
168 | } | ||
169 | |||
170 | protected void PredictUnconstraintMotion(float timeStep) | ||
171 | { | ||
172 | for (int i = 0; i < CollisionObjects.Count; i++) | ||
173 | { | ||
174 | CollisionObject colObj = CollisionObjects[i]; | ||
175 | RigidBody body = RigidBody.Upcast(colObj); | ||
176 | if (body != null) | ||
177 | { | ||
178 | if (!body.IsStaticObject) | ||
179 | { | ||
180 | if (body.IsActive) | ||
181 | { | ||
182 | body.ApplyForces(timeStep); | ||
183 | body.IntegrateVelocities(timeStep); | ||
184 | Matrix temp = body.InterpolationWorldTransform; | ||
185 | body.PredictIntegratedTransform(timeStep, ref temp); | ||
186 | body.InterpolationWorldTransform = temp; | ||
187 | } | ||
188 | } | ||
189 | } | ||
190 | } | ||
191 | } | ||
192 | |||
193 | protected void IntegrateTransforms(float timeStep) | ||
194 | { | ||
195 | Matrix predictedTrans = Matrix.Identity; | ||
196 | for (int i = 0; i < CollisionObjects.Count; i++) | ||
197 | { | ||
198 | CollisionObject colObj = CollisionObjects[i]; | ||
199 | RigidBody body = RigidBody.Upcast(colObj); | ||
200 | if (body != null) | ||
201 | { | ||
202 | if (body.IsActive && (!body.IsStaticObject)) | ||
203 | { | ||
204 | body.PredictIntegratedTransform(timeStep, ref predictedTrans); | ||
205 | body.ProceedToTransform(predictedTrans); | ||
206 | } | ||
207 | } | ||
208 | } | ||
209 | } | ||
210 | } | ||
211 | } | ||