aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin/BSAPIXNA.cs
diff options
context:
space:
mode:
authorRobert Adams2013-05-08 06:02:12 -0700
committerRobert Adams2013-05-08 06:02:12 -0700
commiteb0687f5af127ad6195b95965ce31346f2bc0a24 (patch)
tree8888b8d4c45874c646da0b475d09bd051b33e1cc /OpenSim/Region/Physics/BulletSPlugin/BSAPIXNA.cs
parentAdd regression test for inventory item give, reject and subsequent trash fold... (diff)
downloadopensim-SC_OLD-eb0687f5af127ad6195b95965ce31346f2bc0a24.zip
opensim-SC_OLD-eb0687f5af127ad6195b95965ce31346f2bc0a24.tar.gz
opensim-SC_OLD-eb0687f5af127ad6195b95965ce31346f2bc0a24.tar.bz2
opensim-SC_OLD-eb0687f5af127ad6195b95965ce31346f2bc0a24.tar.xz
vh: update BulletSim (OpenSim/Region/Physics/BulletSPlugin
and DLL/SO) to ac6dcd35fb77f118fc6c3d72cb029591306c7e99 (Mon May 6 21:10:02 2013 -0400) on top of 0.7.5-postfixes.
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSAPIXNA.cs')
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSAPIXNA.cs1470
1 files changed, 1084 insertions, 386 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSAPIXNA.cs b/OpenSim/Region/Physics/BulletSPlugin/BSAPIXNA.cs
index 30a7bee..59780ae 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSAPIXNA.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSAPIXNA.cs
@@ -27,6 +27,7 @@
27using System; 27using System;
28using System.Collections.Generic; 28using System.Collections.Generic;
29using System.IO; 29using System.IO;
30using System.Runtime.InteropServices;
30using System.Text; 31using System.Text;
31 32
32using OpenSim.Framework; 33using OpenSim.Framework;
@@ -80,11 +81,11 @@ private sealed class BulletBodyXNA : BulletBody
80private sealed class BulletShapeXNA : BulletShape 81private sealed class BulletShapeXNA : BulletShape
81{ 82{
82 public CollisionShape shape; 83 public CollisionShape shape;
83 public BulletShapeXNA(CollisionShape xx, BSPhysicsShapeType typ) 84 public BulletShapeXNA(CollisionShape xx, BSPhysicsShapeType typ)
84 : base() 85 : base()
85 { 86 {
86 shape = xx; 87 shape = xx;
87 type = typ; 88 shapeType = typ;
88 } 89 }
89 public override bool HasPhysicalShape 90 public override bool HasPhysicalShape
90 { 91 {
@@ -96,7 +97,7 @@ private sealed class BulletShapeXNA : BulletShape
96 } 97 }
97 public override BulletShape Clone() 98 public override BulletShape Clone()
98 { 99 {
99 return new BulletShapeXNA(shape, type); 100 return new BulletShapeXNA(shape, shapeType);
100 } 101 }
101 public override bool ReferenceSame(BulletShape other) 102 public override bool ReferenceSame(BulletShape other)
102 { 103 {
@@ -129,6 +130,14 @@ private sealed class BulletConstraintXNA : BulletConstraint
129 get { return "XNAConstraint"; } 130 get { return "XNAConstraint"; }
130 } 131 }
131} 132}
133 internal int m_maxCollisions;
134 internal CollisionDesc[] UpdatedCollisions;
135 internal int LastCollisionDesc = 0;
136 internal int m_maxUpdatesPerFrame;
137 internal int LastEntityProperty = 0;
138
139 internal EntityProperties[] UpdatedObjects;
140 internal Dictionary<uint, GhostObject> specialCollisionObjects;
132 141
133 private static int m_collisionsThisFrame; 142 private static int m_collisionsThisFrame;
134 private BSScene PhysicsScene { get; set; } 143 private BSScene PhysicsScene { get; set; }
@@ -142,111 +151,127 @@ private sealed class BulletConstraintXNA : BulletConstraint
142 } 151 }
143 152
144 /// <summary> 153 /// <summary>
145 /// 154 ///
146 /// </summary> 155 /// </summary>
147 /// <param name="p"></param> 156 /// <param name="p"></param>
148 /// <param name="p_2"></param> 157 /// <param name="p_2"></param>
149 public override bool RemoveObjectFromWorld(BulletWorld pWorld, BulletBody pBody) 158 public override bool RemoveObjectFromWorld(BulletWorld pWorld, BulletBody pBody)
150 { 159 {
151 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 160 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
152 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 161 RigidBody body = ((BulletBodyXNA)pBody).rigidBody;
153 world.RemoveRigidBody(body); 162 CollisionObject collisionObject = ((BulletBodyXNA)pBody).body;
163 if (body != null)
164 world.RemoveRigidBody(body);
165 else if (collisionObject != null)
166 world.RemoveCollisionObject(collisionObject);
167 else
168 return false;
154 return true; 169 return true;
155 } 170 }
156 171
157 public override bool AddConstraintToWorld(BulletWorld world, BulletConstraint constrain, bool disableCollisionsBetweenLinkedObjects) 172 public override bool AddConstraintToWorld(BulletWorld pWorld, BulletConstraint pConstraint, bool pDisableCollisionsBetweenLinkedObjects)
158 { 173 {
159 /* TODO */ 174 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
160 return false; 175 TypedConstraint constraint = (pConstraint as BulletConstraintXNA).constrain;
176 world.AddConstraint(constraint, pDisableCollisionsBetweenLinkedObjects);
177
178 return true;
179
161 } 180 }
162 181
163 public override bool RemoveConstraintFromWorld(BulletWorld world, BulletConstraint constrain) 182 public override bool RemoveConstraintFromWorld(BulletWorld pWorld, BulletConstraint pConstraint)
164 { 183 {
165 /* TODO */ 184 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
166 return false; 185 TypedConstraint constraint = (pConstraint as BulletConstraintXNA).constrain;
186 world.RemoveConstraint(constraint);
187 return true;
167 } 188 }
168 189
169 public override void SetRestitution(BulletBody pBody, float pRestitution) 190 public override void SetRestitution(BulletBody pCollisionObject, float pRestitution)
170 { 191 {
171 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 192 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
172 body.SetRestitution(pRestitution); 193 collisionObject.SetRestitution(pRestitution);
173 } 194 }
174 195
175 public override int GetShapeType(BulletShape pShape) 196 public override int GetShapeType(BulletShape pShape)
176 { 197 {
177 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 198 CollisionShape shape = (pShape as BulletShapeXNA).shape;
178 return (int)shape.GetShapeType(); 199 return (int)shape.GetShapeType();
179 } 200 }
180 public override void SetMargin(BulletShape pShape, float pMargin) 201 public override void SetMargin(BulletShape pShape, float pMargin)
181 { 202 {
182 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 203 CollisionShape shape = (pShape as BulletShapeXNA).shape;
183 shape.SetMargin(pMargin); 204 shape.SetMargin(pMargin);
184 } 205 }
185 206
186 public override float GetMargin(BulletShape pShape) 207 public override float GetMargin(BulletShape pShape)
187 { 208 {
188 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 209 CollisionShape shape = (pShape as BulletShapeXNA).shape;
189 return shape.GetMargin(); 210 return shape.GetMargin();
190 } 211 }
191 212
192 public override void SetLocalScaling(BulletShape pShape, Vector3 pScale) 213 public override void SetLocalScaling(BulletShape pShape, Vector3 pScale)
193 { 214 {
194 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 215 CollisionShape shape = (pShape as BulletShapeXNA).shape;
195 IndexedVector3 vec = new IndexedVector3(pScale.X, pScale.Y, pScale.Z); 216 IndexedVector3 vec = new IndexedVector3(pScale.X, pScale.Y, pScale.Z);
196 shape.SetLocalScaling(ref vec); 217 shape.SetLocalScaling(ref vec);
197 218
198 } 219 }
199 220
200 public override void SetContactProcessingThreshold(BulletBody pBody, float contactprocessingthreshold) 221 public override void SetContactProcessingThreshold(BulletBody pCollisionObject, float contactprocessingthreshold)
201 { 222 {
202 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 223 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
203 body.SetContactProcessingThreshold(contactprocessingthreshold); 224 collisionObject.SetContactProcessingThreshold(contactprocessingthreshold);
204 } 225 }
205 226
206 public override void SetCcdMotionThreshold(BulletBody pBody, float pccdMotionThreashold) 227 public override void SetCcdMotionThreshold(BulletBody pCollisionObject, float pccdMotionThreashold)
207 { 228 {
208 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 229 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
209 body.SetCcdMotionThreshold(pccdMotionThreashold); 230 collisionObject.SetCcdMotionThreshold(pccdMotionThreashold);
210 } 231 }
211 232
212 public override void SetCcdSweptSphereRadius(BulletBody pBody, float pCcdSweptSphereRadius) 233 public override void SetCcdSweptSphereRadius(BulletBody pCollisionObject, float pCcdSweptSphereRadius)
213 { 234 {
214 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 235 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
215 body.SetCcdSweptSphereRadius(pCcdSweptSphereRadius); 236 collisionObject.SetCcdSweptSphereRadius(pCcdSweptSphereRadius);
216 } 237 }
217 238
218 public override void SetAngularFactorV(BulletBody pBody, Vector3 pAngularFactor) 239 public override void SetAngularFactorV(BulletBody pBody, Vector3 pAngularFactor)
219 { 240 {
220 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 241 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
221 body.SetAngularFactor(new IndexedVector3(pAngularFactor.X, pAngularFactor.Y, pAngularFactor.Z)); 242 body.SetAngularFactor(new IndexedVector3(pAngularFactor.X, pAngularFactor.Y, pAngularFactor.Z));
222 } 243 }
223 244
224 public override CollisionFlags AddToCollisionFlags(BulletBody pBody, CollisionFlags pcollisionFlags) 245 public override CollisionFlags AddToCollisionFlags(BulletBody pCollisionObject, CollisionFlags pcollisionFlags)
225 { 246 {
226 CollisionObject body = ((BulletBodyXNA)pBody).body; 247 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
227 CollisionFlags existingcollisionFlags = (CollisionFlags)(uint)body.GetCollisionFlags(); 248 CollisionFlags existingcollisionFlags = (CollisionFlags)(uint)collisionObject.GetCollisionFlags();
228 existingcollisionFlags |= pcollisionFlags; 249 existingcollisionFlags |= pcollisionFlags;
229 body.SetCollisionFlags((BulletXNA.BulletCollision.CollisionFlags)(uint)existingcollisionFlags); 250 collisionObject.SetCollisionFlags((BulletXNA.BulletCollision.CollisionFlags)(uint)existingcollisionFlags);
230 return (CollisionFlags) (uint) existingcollisionFlags; 251 return (CollisionFlags) (uint) existingcollisionFlags;
231 } 252 }
232 253
233 public override bool AddObjectToWorld(BulletWorld pWorld, BulletBody pBody) 254 public override bool AddObjectToWorld(BulletWorld pWorld, BulletBody pBody)
234 { 255 {
256 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
257 CollisionObject cbody = (pBody as BulletBodyXNA).body;
258 RigidBody rbody = cbody as RigidBody;
259
235 // Bullet resets several variables when an object is added to the world. In particular, 260 // Bullet resets several variables when an object is added to the world. In particular,
236 // BulletXNA resets position and rotation. Gravity is also reset depending on the static/dynamic 261 // BulletXNA resets position and rotation. Gravity is also reset depending on the static/dynamic
237 // type. Of course, the collision flags in the broadphase proxy are initialized to default. 262 // type. Of course, the collision flags in the broadphase proxy are initialized to default.
238 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 263 IndexedMatrix origPos = cbody.GetWorldTransform();
239 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 264 if (rbody != null)
240 265 {
241 IndexedMatrix origPos = body.GetWorldTransform(); 266 IndexedVector3 origGrav = rbody.GetGravity();
242 IndexedVector3 origGrav = body.GetGravity(); 267 world.AddRigidBody(rbody);
243 268 rbody.SetGravity(origGrav);
244 //if (!(body.GetCollisionShape().GetShapeType() == BroadphaseNativeTypes.STATIC_PLANE_PROXYTYPE && body.GetCollisionShape().GetShapeType() == BroadphaseNativeTypes.TERRAIN_SHAPE_PROXYTYPE)) 269 }
245 270 else
246 world.AddRigidBody(body); 271 {
247 272 world.AddCollisionObject(cbody);
248 body.SetWorldTransform(origPos); 273 }
249 body.SetGravity(origGrav); 274 cbody.SetWorldTransform(origPos);
250 275
251 pBody.ApplyCollisionMask(pWorld.physicsScene); 276 pBody.ApplyCollisionMask(pWorld.physicsScene);
252 277
@@ -255,99 +280,110 @@ private sealed class BulletConstraintXNA : BulletConstraint
255 return true; 280 return true;
256 } 281 }
257 282
258 public override void ForceActivationState(BulletBody pBody, ActivationState pActivationState) 283 public override void ForceActivationState(BulletBody pCollisionObject, ActivationState pActivationState)
259 { 284 {
260 CollisionObject body = ((BulletBodyXNA)pBody).body; 285 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
261 body.ForceActivationState((BulletXNA.BulletCollision.ActivationState)(uint)pActivationState); 286 collisionObject.ForceActivationState((BulletXNA.BulletCollision.ActivationState)(uint)pActivationState);
262 } 287 }
263 288
264 public override void UpdateSingleAabb(BulletWorld pWorld, BulletBody pBody) 289 public override void UpdateSingleAabb(BulletWorld pWorld, BulletBody pCollisionObject)
265 { 290 {
266 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 291 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
267 CollisionObject body = ((BulletBodyXNA)pBody).body; 292 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
268 world.UpdateSingleAabb(body); 293 world.UpdateSingleAabb(collisionObject);
269 } 294 }
270 295
271 public override void UpdateAabbs(BulletWorld world) { /* TODO */ } 296 public override void UpdateAabbs(BulletWorld pWorld) {
272 public override bool GetForceUpdateAllAabbs(BulletWorld world) { /* TODO */ return false; } 297 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
273 public override void SetForceUpdateAllAabbs(BulletWorld world, bool force) { /* TODO */ } 298 world.UpdateAabbs();
299 }
300 public override bool GetForceUpdateAllAabbs(BulletWorld pWorld) {
301 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
302 return world.GetForceUpdateAllAabbs();
274 303
275 public override bool SetCollisionGroupMask(BulletBody pBody, uint pGroup, uint pMask) 304 }
305 public override void SetForceUpdateAllAabbs(BulletWorld pWorld, bool pForce)
276 { 306 {
277 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 307 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
278 body.GetBroadphaseHandle().m_collisionFilterGroup = (BulletXNA.BulletCollision.CollisionFilterGroups) pGroup; 308 world.SetForceUpdateAllAabbs(pForce);
279 body.GetBroadphaseHandle().m_collisionFilterGroup = (BulletXNA.BulletCollision.CollisionFilterGroups) pGroup; 309 }
280 if ((uint) body.GetBroadphaseHandle().m_collisionFilterGroup == 0) 310
311 public override bool SetCollisionGroupMask(BulletBody pCollisionObject, uint pGroup, uint pMask)
312 {
313 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
314 collisionObject.GetBroadphaseHandle().m_collisionFilterGroup = (BulletXNA.BulletCollision.CollisionFilterGroups) pGroup;
315 collisionObject.GetBroadphaseHandle().m_collisionFilterGroup = (BulletXNA.BulletCollision.CollisionFilterGroups) pGroup;
316 if ((uint) collisionObject.GetBroadphaseHandle().m_collisionFilterGroup == 0)
281 return false; 317 return false;
282 return true; 318 return true;
283 } 319 }
284 320
285 public override void ClearAllForces(BulletBody pBody) 321 public override void ClearAllForces(BulletBody pCollisionObject)
286 { 322 {
287 CollisionObject body = ((BulletBodyXNA)pBody).body; 323 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
288 IndexedVector3 zeroVector = new IndexedVector3(0, 0, 0); 324 IndexedVector3 zeroVector = new IndexedVector3(0, 0, 0);
289 body.SetInterpolationLinearVelocity(ref zeroVector); 325 collisionObject.SetInterpolationLinearVelocity(ref zeroVector);
290 body.SetInterpolationAngularVelocity(ref zeroVector); 326 collisionObject.SetInterpolationAngularVelocity(ref zeroVector);
291 IndexedMatrix bodytransform = body.GetWorldTransform(); 327 IndexedMatrix bodytransform = collisionObject.GetWorldTransform();
292 328
293 body.SetInterpolationWorldTransform(ref bodytransform); 329 collisionObject.SetInterpolationWorldTransform(ref bodytransform);
294 330
295 if (body is RigidBody) 331 if (collisionObject is RigidBody)
296 { 332 {
297 RigidBody rigidbody = body as RigidBody; 333 RigidBody rigidbody = collisionObject as RigidBody;
298 rigidbody.SetLinearVelocity(zeroVector); 334 rigidbody.SetLinearVelocity(zeroVector);
299 rigidbody.SetAngularVelocity(zeroVector); 335 rigidbody.SetAngularVelocity(zeroVector);
300 rigidbody.ClearForces(); 336 rigidbody.ClearForces();
301 } 337 }
302 } 338 }
303 339
304 public override void SetInterpolationAngularVelocity(BulletBody pBody, Vector3 pVector3) 340 public override void SetInterpolationAngularVelocity(BulletBody pCollisionObject, Vector3 pVector3)
305 { 341 {
306 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 342 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
307 IndexedVector3 vec = new IndexedVector3(pVector3.X, pVector3.Y, pVector3.Z); 343 IndexedVector3 vec = new IndexedVector3(pVector3.X, pVector3.Y, pVector3.Z);
308 body.SetInterpolationAngularVelocity(ref vec); 344 collisionObject.SetInterpolationAngularVelocity(ref vec);
309 } 345 }
310 346
311 public override void SetAngularVelocity(BulletBody pBody, Vector3 pVector3) 347 public override void SetAngularVelocity(BulletBody pBody, Vector3 pVector3)
312 { 348 {
313 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 349 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
314 IndexedVector3 vec = new IndexedVector3(pVector3.X, pVector3.Y, pVector3.Z); 350 IndexedVector3 vec = new IndexedVector3(pVector3.X, pVector3.Y, pVector3.Z);
315 body.SetAngularVelocity(ref vec); 351 body.SetAngularVelocity(ref vec);
316 } 352 }
317 public override Vector3 GetTotalForce(BulletBody pBody) 353 public override Vector3 GetTotalForce(BulletBody pBody)
318 { 354 {
319 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 355 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
320 IndexedVector3 iv3 = body.GetTotalForce(); 356 IndexedVector3 iv3 = body.GetTotalForce();
321 return new Vector3(iv3.X, iv3.Y, iv3.Z); 357 return new Vector3(iv3.X, iv3.Y, iv3.Z);
322 } 358 }
323 public override Vector3 GetTotalTorque(BulletBody pBody) 359 public override Vector3 GetTotalTorque(BulletBody pBody)
324 { 360 {
325 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 361 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
326 IndexedVector3 iv3 = body.GetTotalTorque(); 362 IndexedVector3 iv3 = body.GetTotalTorque();
327 return new Vector3(iv3.X, iv3.Y, iv3.Z); 363 return new Vector3(iv3.X, iv3.Y, iv3.Z);
328 } 364 }
329 public override Vector3 GetInvInertiaDiagLocal(BulletBody pBody) 365 public override Vector3 GetInvInertiaDiagLocal(BulletBody pBody)
330 { 366 {
331 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 367 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
332 IndexedVector3 iv3 = body.GetInvInertiaDiagLocal(); 368 IndexedVector3 iv3 = body.GetInvInertiaDiagLocal();
333 return new Vector3(iv3.X, iv3.Y, iv3.Z); 369 return new Vector3(iv3.X, iv3.Y, iv3.Z);
334 } 370 }
335 public override void SetInvInertiaDiagLocal(BulletBody pBody, Vector3 inert) 371 public override void SetInvInertiaDiagLocal(BulletBody pBody, Vector3 inert)
336 { 372 {
337 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 373 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
338 IndexedVector3 iv3 = new IndexedVector3(inert.X, inert.Y, inert.Z); 374 IndexedVector3 iv3 = new IndexedVector3(inert.X, inert.Y, inert.Z);
339 body.SetInvInertiaDiagLocal(ref iv3); 375 body.SetInvInertiaDiagLocal(ref iv3);
340 } 376 }
341 public override void ApplyForce(BulletBody pBody, Vector3 force, Vector3 pos) 377 public override void ApplyForce(BulletBody pBody, Vector3 force, Vector3 pos)
342 { 378 {
343 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 379 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
344 IndexedVector3 forceiv3 = new IndexedVector3(force.X, force.Y, force.Z); 380 IndexedVector3 forceiv3 = new IndexedVector3(force.X, force.Y, force.Z);
345 IndexedVector3 posiv3 = new IndexedVector3(pos.X, pos.Y, pos.Z); 381 IndexedVector3 posiv3 = new IndexedVector3(pos.X, pos.Y, pos.Z);
346 body.ApplyForce(ref forceiv3, ref posiv3); 382 body.ApplyForce(ref forceiv3, ref posiv3);
347 } 383 }
348 public override void ApplyImpulse(BulletBody pBody, Vector3 imp, Vector3 pos) 384 public override void ApplyImpulse(BulletBody pBody, Vector3 imp, Vector3 pos)
349 { 385 {
350 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 386 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
351 IndexedVector3 impiv3 = new IndexedVector3(imp.X, imp.Y, imp.Z); 387 IndexedVector3 impiv3 = new IndexedVector3(imp.X, imp.Y, imp.Z);
352 IndexedVector3 posiv3 = new IndexedVector3(pos.X, pos.Y, pos.Z); 388 IndexedVector3 posiv3 = new IndexedVector3(pos.X, pos.Y, pos.Z);
353 body.ApplyImpulse(ref impiv3, ref posiv3); 389 body.ApplyImpulse(ref impiv3, ref posiv3);
@@ -355,32 +391,32 @@ private sealed class BulletConstraintXNA : BulletConstraint
355 391
356 public override void ClearForces(BulletBody pBody) 392 public override void ClearForces(BulletBody pBody)
357 { 393 {
358 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 394 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
359 body.ClearForces(); 395 body.ClearForces();
360 } 396 }
361 397
362 public override void SetTranslation(BulletBody pBody, Vector3 _position, Quaternion _orientation) 398 public override void SetTranslation(BulletBody pCollisionObject, Vector3 _position, Quaternion _orientation)
363 { 399 {
364 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 400 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
365 IndexedVector3 vposition = new IndexedVector3(_position.X, _position.Y, _position.Z); 401 IndexedVector3 vposition = new IndexedVector3(_position.X, _position.Y, _position.Z);
366 IndexedQuaternion vquaternion = new IndexedQuaternion(_orientation.X, _orientation.Y, _orientation.Z, 402 IndexedQuaternion vquaternion = new IndexedQuaternion(_orientation.X, _orientation.Y, _orientation.Z,
367 _orientation.W); 403 _orientation.W);
368 IndexedMatrix mat = IndexedMatrix.CreateFromQuaternion(vquaternion); 404 IndexedMatrix mat = IndexedMatrix.CreateFromQuaternion(vquaternion);
369 mat._origin = vposition; 405 mat._origin = vposition;
370 body.SetWorldTransform(mat); 406 collisionObject.SetWorldTransform(mat);
371 407
372 } 408 }
373 409
374 public override Vector3 GetPosition(BulletBody pBody) 410 public override Vector3 GetPosition(BulletBody pCollisionObject)
375 { 411 {
376 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 412 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
377 IndexedVector3 pos = body.GetInterpolationWorldTransform()._origin; 413 IndexedVector3 pos = collisionObject.GetInterpolationWorldTransform()._origin;
378 return new Vector3(pos.X, pos.Y, pos.Z); 414 return new Vector3(pos.X, pos.Y, pos.Z);
379 } 415 }
380 416
381 public override Vector3 CalculateLocalInertia(BulletShape pShape, float pphysMass) 417 public override Vector3 CalculateLocalInertia(BulletShape pShape, float pphysMass)
382 { 418 {
383 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 419 CollisionShape shape = (pShape as BulletShapeXNA).shape;
384 IndexedVector3 inertia = IndexedVector3.Zero; 420 IndexedVector3 inertia = IndexedVector3.Zero;
385 shape.CalculateLocalInertia(pphysMass, out inertia); 421 shape.CalculateLocalInertia(pphysMass, out inertia);
386 return new Vector3(inertia.X, inertia.Y, inertia.Z); 422 return new Vector3(inertia.X, inertia.Y, inertia.Z);
@@ -388,81 +424,104 @@ private sealed class BulletConstraintXNA : BulletConstraint
388 424
389 public override void SetMassProps(BulletBody pBody, float pphysMass, Vector3 plocalInertia) 425 public override void SetMassProps(BulletBody pBody, float pphysMass, Vector3 plocalInertia)
390 { 426 {
391 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 427 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
392 IndexedVector3 inertia = new IndexedVector3(plocalInertia.X, plocalInertia.Y, plocalInertia.Z); 428 if (body != null) // Can't set mass props on collision object.
393 body.SetMassProps(pphysMass, inertia); 429 {
430 IndexedVector3 inertia = new IndexedVector3(plocalInertia.X, plocalInertia.Y, plocalInertia.Z);
431 body.SetMassProps(pphysMass, inertia);
432 }
394 } 433 }
395 434
396 435
397 public override void SetObjectForce(BulletBody pBody, Vector3 _force) 436 public override void SetObjectForce(BulletBody pBody, Vector3 _force)
398 { 437 {
399 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 438 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
400 IndexedVector3 force = new IndexedVector3(_force.X, _force.Y, _force.Z); 439 IndexedVector3 force = new IndexedVector3(_force.X, _force.Y, _force.Z);
401 body.SetTotalForce(ref force); 440 body.SetTotalForce(ref force);
402 } 441 }
403 442
404 public override void SetFriction(BulletBody pBody, float _currentFriction) 443 public override void SetFriction(BulletBody pCollisionObject, float _currentFriction)
405 { 444 {
406 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 445 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
407 body.SetFriction(_currentFriction); 446 collisionObject.SetFriction(_currentFriction);
408 } 447 }
409 448
410 public override void SetLinearVelocity(BulletBody pBody, Vector3 _velocity) 449 public override void SetLinearVelocity(BulletBody pBody, Vector3 _velocity)
411 { 450 {
412 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 451 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
413 IndexedVector3 velocity = new IndexedVector3(_velocity.X, _velocity.Y, _velocity.Z); 452 IndexedVector3 velocity = new IndexedVector3(_velocity.X, _velocity.Y, _velocity.Z);
414 body.SetLinearVelocity(velocity); 453 body.SetLinearVelocity(velocity);
415 } 454 }
416 455
417 public override void Activate(BulletBody pBody, bool pforceactivation) 456 public override void Activate(BulletBody pCollisionObject, bool pforceactivation)
418 { 457 {
419 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 458 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
420 body.Activate(pforceactivation); 459 collisionObject.Activate(pforceactivation);
421 460
422 } 461 }
423 462
424 public override Quaternion GetOrientation(BulletBody pBody) 463 public override Quaternion GetOrientation(BulletBody pCollisionObject)
425 { 464 {
426 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 465 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
427 IndexedQuaternion mat = body.GetInterpolationWorldTransform().GetRotation(); 466 IndexedQuaternion mat = collisionObject.GetInterpolationWorldTransform().GetRotation();
428 return new Quaternion(mat.X, mat.Y, mat.Z, mat.W); 467 return new Quaternion(mat.X, mat.Y, mat.Z, mat.W);
429 } 468 }
430 469
431 public override CollisionFlags RemoveFromCollisionFlags(BulletBody pBody, CollisionFlags pcollisionFlags) 470 public override CollisionFlags RemoveFromCollisionFlags(BulletBody pCollisionObject, CollisionFlags pcollisionFlags)
432 { 471 {
433 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 472 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
434 CollisionFlags existingcollisionFlags = (CollisionFlags)(uint)body.GetCollisionFlags(); 473 CollisionFlags existingcollisionFlags = (CollisionFlags)(uint)collisionObject.GetCollisionFlags();
435 existingcollisionFlags &= ~pcollisionFlags; 474 existingcollisionFlags &= ~pcollisionFlags;
436 body.SetCollisionFlags((BulletXNA.BulletCollision.CollisionFlags)(uint)existingcollisionFlags); 475 collisionObject.SetCollisionFlags((BulletXNA.BulletCollision.CollisionFlags)(uint)existingcollisionFlags);
437 return (CollisionFlags)(uint)existingcollisionFlags; 476 return (CollisionFlags)(uint)existingcollisionFlags;
438 } 477 }
439 478
440 public override float GetCcdMotionThreshold(BulletBody obj) { /* TODO */ return 0f; } 479 public override float GetCcdMotionThreshold(BulletBody pCollisionObject)
480 {
481 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
482 return collisionObject.GetCcdSquareMotionThreshold();
483 }
441 484
442 public override float GetCcdSweptSphereRadius(BulletBody obj) { /* TODO */ return 0f; } 485 public override float GetCcdSweptSphereRadius(BulletBody pCollisionObject)
486 {
487 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
488 return collisionObject.GetCcdSweptSphereRadius();
489
490 }
443 491
444 public override IntPtr GetUserPointer(BulletBody obj) { /* TODO */ return IntPtr.Zero; } 492 public override IntPtr GetUserPointer(BulletBody pCollisionObject)
493 {
494 CollisionObject shape = (pCollisionObject as BulletBodyXNA).body;
495 return (IntPtr)shape.GetUserPointer();
496 }
445 497
446 public override void SetUserPointer(BulletBody obj, IntPtr val) { /* TODO */ } 498 public override void SetUserPointer(BulletBody pCollisionObject, IntPtr val)
499 {
500 CollisionObject shape = (pCollisionObject as BulletBodyXNA).body;
501 shape.SetUserPointer(val);
502 }
447 503
448 public override void SetGravity(BulletBody pBody, Vector3 pGravity) 504 public override void SetGravity(BulletBody pBody, Vector3 pGravity)
449 { 505 {
450 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 506 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
451 IndexedVector3 gravity = new IndexedVector3(pGravity.X, pGravity.Y, pGravity.Z); 507 if (body != null) // Can't set collisionobject.set gravity
452 body.SetGravity(gravity); 508 {
509 IndexedVector3 gravity = new IndexedVector3(pGravity.X, pGravity.Y, pGravity.Z);
510 body.SetGravity(gravity);
511 }
453 } 512 }
454 513
455 public override bool DestroyConstraint(BulletWorld pWorld, BulletConstraint pConstraint) 514 public override bool DestroyConstraint(BulletWorld pWorld, BulletConstraint pConstraint)
456 { 515 {
457 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 516 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
458 TypedConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain; 517 TypedConstraint constraint = (pConstraint as BulletConstraintXNA).constrain;
459 world.RemoveConstraint(constraint); 518 world.RemoveConstraint(constraint);
460 return true; 519 return true;
461 } 520 }
462 521
463 public override bool SetLinearLimits(BulletConstraint pConstraint, Vector3 low, Vector3 high) 522 public override bool SetLinearLimits(BulletConstraint pConstraint, Vector3 low, Vector3 high)
464 { 523 {
465 Generic6DofConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint; 524 Generic6DofConstraint constraint = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
466 IndexedVector3 lowlimit = new IndexedVector3(low.X, low.Y, low.Z); 525 IndexedVector3 lowlimit = new IndexedVector3(low.X, low.Y, low.Z);
467 IndexedVector3 highlimit = new IndexedVector3(high.X, high.Y, high.Z); 526 IndexedVector3 highlimit = new IndexedVector3(high.X, high.Y, high.Z);
468 constraint.SetLinearLowerLimit(lowlimit); 527 constraint.SetLinearLowerLimit(lowlimit);
@@ -472,7 +531,7 @@ private sealed class BulletConstraintXNA : BulletConstraint
472 531
473 public override bool SetAngularLimits(BulletConstraint pConstraint, Vector3 low, Vector3 high) 532 public override bool SetAngularLimits(BulletConstraint pConstraint, Vector3 low, Vector3 high)
474 { 533 {
475 Generic6DofConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint; 534 Generic6DofConstraint constraint = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
476 IndexedVector3 lowlimit = new IndexedVector3(low.X, low.Y, low.Z); 535 IndexedVector3 lowlimit = new IndexedVector3(low.X, low.Y, low.Z);
477 IndexedVector3 highlimit = new IndexedVector3(high.X, high.Y, high.Z); 536 IndexedVector3 highlimit = new IndexedVector3(high.X, high.Y, high.Z);
478 constraint.SetAngularLowerLimit(lowlimit); 537 constraint.SetAngularLowerLimit(lowlimit);
@@ -482,31 +541,32 @@ private sealed class BulletConstraintXNA : BulletConstraint
482 541
483 public override void SetConstraintNumSolverIterations(BulletConstraint pConstraint, float cnt) 542 public override void SetConstraintNumSolverIterations(BulletConstraint pConstraint, float cnt)
484 { 543 {
485 Generic6DofConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint; 544 Generic6DofConstraint constraint = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
486 constraint.SetOverrideNumSolverIterations((int)cnt); 545 constraint.SetOverrideNumSolverIterations((int)cnt);
487 } 546 }
488 547
489 public override bool CalculateTransforms(BulletConstraint pConstraint) 548 public override bool CalculateTransforms(BulletConstraint pConstraint)
490 { 549 {
491 Generic6DofConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint; 550 Generic6DofConstraint constraint = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
492 constraint.CalculateTransforms(); 551 constraint.CalculateTransforms();
493 return true; 552 return true;
494 } 553 }
495 554
496 public override void SetConstraintEnable(BulletConstraint pConstraint, float p_2) 555 public override void SetConstraintEnable(BulletConstraint pConstraint, float p_2)
497 { 556 {
498 Generic6DofConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint; 557 Generic6DofConstraint constraint = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
499 constraint.SetEnabled((p_2 == 0) ? false : true); 558 constraint.SetEnabled((p_2 == 0) ? false : true);
500 } 559 }
501 560
502 561
503 //BulletSimAPI.Create6DofConstraint(m_world.ptr, m_body1.ptr, m_body2.ptr,frame1, frame1rot,frame2, frame2rot,useLinearReferenceFrameA, disableCollisionsBetweenLinkedBodies)); 562 public override BulletConstraint Create6DofConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
504 public override BulletConstraint Create6DofConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2, Vector3 pframe1, Quaternion pframe1rot, Vector3 pframe2, Quaternion pframe2rot, bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies) 563 Vector3 pframe1, Quaternion pframe1rot, Vector3 pframe2, Quaternion pframe2rot,
564 bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies)
505 565
506 { 566 {
507 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 567 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
508 RigidBody body1 = ((BulletBodyXNA)pBody1).rigidBody; 568 RigidBody body1 = (pBody1 as BulletBodyXNA).rigidBody;
509 RigidBody body2 = ((BulletBodyXNA)pBody2).rigidBody; 569 RigidBody body2 = (pBody2 as BulletBodyXNA).rigidBody;
510 IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z); 570 IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
511 IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W); 571 IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
512 IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot); 572 IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
@@ -525,9 +585,26 @@ private sealed class BulletConstraintXNA : BulletConstraint
525 return new BulletConstraintXNA(consttr); 585 return new BulletConstraintXNA(consttr);
526 } 586 }
527 587
528 588 public override BulletConstraint Create6DofConstraintFixed(BulletWorld pWorld, BulletBody pBody1,
589 Vector3 pframe1, Quaternion pframe1rot,
590 bool pUseLinearReferenceFrameB, bool pdisableCollisionsBetweenLinkedBodies)
591 {
592 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
593 RigidBody body1 = (pBody1 as BulletBodyXNA).rigidBody;
594 IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
595 IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
596 IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
597 frame1._origin = frame1v;
598
599 Generic6DofConstraint consttr = new Generic6DofConstraint(body1, ref frame1, pUseLinearReferenceFrameB);
600 consttr.CalculateTransforms();
601 world.AddConstraint(consttr,pdisableCollisionsBetweenLinkedBodies);
602
603 return new BulletConstraintXNA(consttr);
604 }
605
529 /// <summary> 606 /// <summary>
530 /// 607 ///
531 /// </summary> 608 /// </summary>
532 /// <param name="pWorld"></param> 609 /// <param name="pWorld"></param>
533 /// <param name="pBody1"></param> 610 /// <param name="pBody1"></param>
@@ -538,9 +615,9 @@ private sealed class BulletConstraintXNA : BulletConstraint
538 /// <returns></returns> 615 /// <returns></returns>
539 public override BulletConstraint Create6DofConstraintToPoint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2, Vector3 pjoinPoint, bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies) 616 public override BulletConstraint Create6DofConstraintToPoint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2, Vector3 pjoinPoint, bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies)
540 { 617 {
541 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 618 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
542 RigidBody body1 = ((BulletBodyXNA)pBody1).rigidBody; 619 RigidBody body1 = (pBody1 as BulletBodyXNA).rigidBody;
543 RigidBody body2 = ((BulletBodyXNA)pBody2).rigidBody; 620 RigidBody body2 = (pBody2 as BulletBodyXNA).rigidBody;
544 IndexedMatrix frame1 = new IndexedMatrix(IndexedBasisMatrix.Identity, new IndexedVector3(0, 0, 0)); 621 IndexedMatrix frame1 = new IndexedMatrix(IndexedBasisMatrix.Identity, new IndexedVector3(0, 0, 0));
545 IndexedMatrix frame2 = new IndexedMatrix(IndexedBasisMatrix.Identity, new IndexedVector3(0, 0, 0)); 622 IndexedMatrix frame2 = new IndexedMatrix(IndexedBasisMatrix.Identity, new IndexedVector3(0, 0, 0));
546 623
@@ -559,7 +636,7 @@ private sealed class BulletConstraintXNA : BulletConstraint
559 //SetFrames(m_constraint.ptr, frameA, frameArot, frameB, frameBrot); 636 //SetFrames(m_constraint.ptr, frameA, frameArot, frameB, frameBrot);
560 public override bool SetFrames(BulletConstraint pConstraint, Vector3 pframe1, Quaternion pframe1rot, Vector3 pframe2, Quaternion pframe2rot) 637 public override bool SetFrames(BulletConstraint pConstraint, Vector3 pframe1, Quaternion pframe1rot, Vector3 pframe2, Quaternion pframe2rot)
561 { 638 {
562 Generic6DofConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint; 639 Generic6DofConstraint constraint = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
563 IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z); 640 IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
564 IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W); 641 IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
565 IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot); 642 IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
@@ -575,109 +652,110 @@ private sealed class BulletConstraintXNA : BulletConstraint
575 652
576 public override Vector3 GetLinearVelocity(BulletBody pBody) 653 public override Vector3 GetLinearVelocity(BulletBody pBody)
577 { 654 {
578 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 655 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
579 IndexedVector3 iv3 = body.GetLinearVelocity(); 656 IndexedVector3 iv3 = body.GetLinearVelocity();
580 return new Vector3(iv3.X, iv3.Y, iv3.Z); 657 return new Vector3(iv3.X, iv3.Y, iv3.Z);
581 } 658 }
582 public override Vector3 GetAngularVelocity(BulletBody pBody) 659 public override Vector3 GetAngularVelocity(BulletBody pBody)
583 { 660 {
584 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 661 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
585 IndexedVector3 iv3 = body.GetAngularVelocity(); 662 IndexedVector3 iv3 = body.GetAngularVelocity();
586 return new Vector3(iv3.X, iv3.Y, iv3.Z); 663 return new Vector3(iv3.X, iv3.Y, iv3.Z);
587 } 664 }
588 public override Vector3 GetVelocityInLocalPoint(BulletBody pBody, Vector3 pos) 665 public override Vector3 GetVelocityInLocalPoint(BulletBody pBody, Vector3 pos)
589 { 666 {
590 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 667 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
591 IndexedVector3 posiv3 = new IndexedVector3(pos.X, pos.Y, pos.Z); 668 IndexedVector3 posiv3 = new IndexedVector3(pos.X, pos.Y, pos.Z);
592 IndexedVector3 iv3 = body.GetVelocityInLocalPoint(ref posiv3); 669 IndexedVector3 iv3 = body.GetVelocityInLocalPoint(ref posiv3);
593 return new Vector3(iv3.X, iv3.Y, iv3.Z); 670 return new Vector3(iv3.X, iv3.Y, iv3.Z);
594 } 671 }
595 public override void Translate(BulletBody pBody, Vector3 trans) 672 public override void Translate(BulletBody pCollisionObject, Vector3 trans)
596 { 673 {
597 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 674 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
675 collisionObject.Translate(new IndexedVector3(trans.X,trans.Y,trans.Z));
598 } 676 }
599 public override void UpdateDeactivation(BulletBody pBody, float timeStep) 677 public override void UpdateDeactivation(BulletBody pBody, float timeStep)
600 { 678 {
601 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 679 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
602 body.UpdateDeactivation(timeStep); 680 body.UpdateDeactivation(timeStep);
603 } 681 }
604 682
605 public override bool WantsSleeping(BulletBody pBody) 683 public override bool WantsSleeping(BulletBody pBody)
606 { 684 {
607 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 685 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
608 return body.WantsSleeping(); 686 return body.WantsSleeping();
609 } 687 }
610 688
611 public override void SetAngularFactor(BulletBody pBody, float factor) 689 public override void SetAngularFactor(BulletBody pBody, float factor)
612 { 690 {
613 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 691 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
614 body.SetAngularFactor(factor); 692 body.SetAngularFactor(factor);
615 } 693 }
616 694
617 public override Vector3 GetAngularFactor(BulletBody pBody) 695 public override Vector3 GetAngularFactor(BulletBody pBody)
618 { 696 {
619 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 697 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
620 IndexedVector3 iv3 = body.GetAngularFactor(); 698 IndexedVector3 iv3 = body.GetAngularFactor();
621 return new Vector3(iv3.X, iv3.Y, iv3.Z); 699 return new Vector3(iv3.X, iv3.Y, iv3.Z);
622 } 700 }
623 701
624 public override bool IsInWorld(BulletWorld pWorld, BulletBody pBody) 702 public override bool IsInWorld(BulletWorld pWorld, BulletBody pCollisionObject)
625 { 703 {
626 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 704 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
627 CollisionObject body = ((BulletBodyXNA)pBody).body; 705 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
628 return world.IsInWorld(body); 706 return world.IsInWorld(collisionObject);
629 } 707 }
630 708
631 public override void AddConstraintRef(BulletBody pBody, BulletConstraint pConstrain) 709 public override void AddConstraintRef(BulletBody pBody, BulletConstraint pConstraint)
632 { 710 {
633 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 711 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
634 TypedConstraint constrain = ((BulletConstraintXNA)pConstrain).constrain; 712 TypedConstraint constrain = (pConstraint as BulletConstraintXNA).constrain;
635 body.AddConstraintRef(constrain); 713 body.AddConstraintRef(constrain);
636 } 714 }
637 715
638 public override void RemoveConstraintRef(BulletBody pBody, BulletConstraint pConstrain) 716 public override void RemoveConstraintRef(BulletBody pBody, BulletConstraint pConstraint)
639 { 717 {
640 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 718 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
641 TypedConstraint constrain = ((BulletConstraintXNA)pConstrain).constrain; 719 TypedConstraint constrain = (pConstraint as BulletConstraintXNA).constrain;
642 body.RemoveConstraintRef(constrain); 720 body.RemoveConstraintRef(constrain);
643 } 721 }
644 722
645 public override BulletConstraint GetConstraintRef(BulletBody pBody, int index) 723 public override BulletConstraint GetConstraintRef(BulletBody pBody, int index)
646 { 724 {
647 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 725 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
648 return new BulletConstraintXNA(body.GetConstraintRef(index)); 726 return new BulletConstraintXNA(body.GetConstraintRef(index));
649 } 727 }
650 728
651 public override int GetNumConstraintRefs(BulletBody pBody) 729 public override int GetNumConstraintRefs(BulletBody pBody)
652 { 730 {
653 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 731 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
654 return body.GetNumConstraintRefs(); 732 return body.GetNumConstraintRefs();
655 } 733 }
656 734
657 public override void SetInterpolationLinearVelocity(BulletBody pBody, Vector3 VehicleVelocity) 735 public override void SetInterpolationLinearVelocity(BulletBody pCollisionObject, Vector3 VehicleVelocity)
658 { 736 {
659 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 737 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
660 IndexedVector3 velocity = new IndexedVector3(VehicleVelocity.X, VehicleVelocity.Y, VehicleVelocity.Z); 738 IndexedVector3 velocity = new IndexedVector3(VehicleVelocity.X, VehicleVelocity.Y, VehicleVelocity.Z);
661 body.SetInterpolationLinearVelocity(ref velocity); 739 collisionObject.SetInterpolationLinearVelocity(ref velocity);
662 } 740 }
663 741
664 public override bool UseFrameOffset(BulletConstraint pConstraint, float onOff) 742 public override bool UseFrameOffset(BulletConstraint pConstraint, float onOff)
665 { 743 {
666 Generic6DofConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint; 744 Generic6DofConstraint constraint = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
667 constraint.SetUseFrameOffset((onOff == 0) ? false : true); 745 constraint.SetUseFrameOffset((onOff == 0) ? false : true);
668 return true; 746 return true;
669 } 747 }
670 //SetBreakingImpulseThreshold(m_constraint.ptr, threshold); 748 //SetBreakingImpulseThreshold(m_constraint.ptr, threshold);
671 public override bool SetBreakingImpulseThreshold(BulletConstraint pConstraint, float threshold) 749 public override bool SetBreakingImpulseThreshold(BulletConstraint pConstraint, float threshold)
672 { 750 {
673 Generic6DofConstraint constraint = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint; 751 Generic6DofConstraint constraint = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
674 constraint.SetBreakingImpulseThreshold(threshold); 752 constraint.SetBreakingImpulseThreshold(threshold);
675 return true; 753 return true;
676 } 754 }
677 //BulletSimAPI.SetAngularDamping(Prim.PhysBody.ptr, angularDamping); 755 //BulletSimAPI.SetAngularDamping(Prim.PhysBody.ptr, angularDamping);
678 public override void SetAngularDamping(BulletBody pBody, float angularDamping) 756 public override void SetAngularDamping(BulletBody pBody, float angularDamping)
679 { 757 {
680 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 758 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
681 float lineardamping = body.GetLinearDamping(); 759 float lineardamping = body.GetLinearDamping();
682 body.SetDamping(lineardamping, angularDamping); 760 body.SetDamping(lineardamping, angularDamping);
683 761
@@ -685,163 +763,241 @@ private sealed class BulletConstraintXNA : BulletConstraint
685 763
686 public override void UpdateInertiaTensor(BulletBody pBody) 764 public override void UpdateInertiaTensor(BulletBody pBody)
687 { 765 {
688 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 766 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
689 body.UpdateInertiaTensor(); 767 if (body != null) // can't update inertia tensor on CollisionObject
768 body.UpdateInertiaTensor();
690 } 769 }
691 770
692 public override void RecalculateCompoundShapeLocalAabb(BulletShape pCompoundShape) 771 public override void RecalculateCompoundShapeLocalAabb(BulletShape pCompoundShape)
693 { 772 {
694 CompoundShape shape = ((BulletShapeXNA)pCompoundShape).shape as CompoundShape; 773 CompoundShape shape = (pCompoundShape as BulletShapeXNA).shape as CompoundShape;
695 shape.RecalculateLocalAabb(); 774 shape.RecalculateLocalAabb();
696 } 775 }
697 776
698 //BulletSimAPI.GetCollisionFlags(PhysBody.ptr) 777 //BulletSimAPI.GetCollisionFlags(PhysBody.ptr)
699 public override CollisionFlags GetCollisionFlags(BulletBody pBody) 778 public override CollisionFlags GetCollisionFlags(BulletBody pCollisionObject)
700 { 779 {
701 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 780 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
702 uint flags = (uint)body.GetCollisionFlags(); 781 uint flags = (uint)collisionObject.GetCollisionFlags();
703 return (CollisionFlags) flags; 782 return (CollisionFlags) flags;
704 } 783 }
705 784
706 public override void SetDamping(BulletBody pBody, float pLinear, float pAngular) 785 public override void SetDamping(BulletBody pBody, float pLinear, float pAngular)
707 { 786 {
708 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 787 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
709 body.SetDamping(pLinear, pAngular); 788 body.SetDamping(pLinear, pAngular);
710 } 789 }
711 //PhysBody.ptr, PhysicsScene.Params.deactivationTime); 790 //PhysBody.ptr, PhysicsScene.Params.deactivationTime);
712 public override void SetDeactivationTime(BulletBody pBody, float pDeactivationTime) 791 public override void SetDeactivationTime(BulletBody pCollisionObject, float pDeactivationTime)
713 { 792 {
714 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 793 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
715 body.SetDeactivationTime(pDeactivationTime); 794 collisionObject.SetDeactivationTime(pDeactivationTime);
716 } 795 }
717 //SetSleepingThresholds(PhysBody.ptr, PhysicsScene.Params.linearSleepingThreshold, PhysicsScene.Params.angularSleepingThreshold); 796 //SetSleepingThresholds(PhysBody.ptr, PhysicsScene.Params.linearSleepingThreshold, PhysicsScene.Params.angularSleepingThreshold);
718 public override void SetSleepingThresholds(BulletBody pBody, float plinearSleepingThreshold, float pangularSleepingThreshold) 797 public override void SetSleepingThresholds(BulletBody pBody, float plinearSleepingThreshold, float pangularSleepingThreshold)
719 { 798 {
720 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 799 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
721 body.SetSleepingThresholds(plinearSleepingThreshold, pangularSleepingThreshold); 800 body.SetSleepingThresholds(plinearSleepingThreshold, pangularSleepingThreshold);
722 } 801 }
723 802
724 public override CollisionObjectTypes GetBodyType(BulletBody pBody) 803 public override CollisionObjectTypes GetBodyType(BulletBody pCollisionObject)
725 { 804 {
726 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 805 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
727 return (CollisionObjectTypes)(int) body.GetInternalType(); 806 return (CollisionObjectTypes)(int) collisionObject.GetInternalType();
728 } 807 }
729 808
730 public override void ApplyGravity(BulletBody obj) { /* TODO */ } 809 public override void ApplyGravity(BulletBody pBody)
810 {
811
812 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
813 body.ApplyGravity();
814 }
731 815
732 public override Vector3 GetGravity(BulletBody obj) { /* TODO */ return Vector3.Zero; } 816 public override Vector3 GetGravity(BulletBody pBody)
817 {
818 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
819 IndexedVector3 gravity = body.GetGravity();
820 return new Vector3(gravity.X, gravity.Y, gravity.Z);
821 }
733 822
734 public override void SetLinearDamping(BulletBody obj, float lin_damping) { /* TODO */ } 823 public override void SetLinearDamping(BulletBody pBody, float lin_damping)
824 {
825 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
826 float angularDamping = body.GetAngularDamping();
827 body.SetDamping(lin_damping, angularDamping);
828 }
735 829
736 public override float GetLinearDamping(BulletBody obj) { /* TODO */ return 0f; } 830 public override float GetLinearDamping(BulletBody pBody)
831 {
832 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
833 return body.GetLinearDamping();
834 }
737 835
738 public override float GetAngularDamping(BulletBody obj) { /* TODO */ return 0f; } 836 public override float GetAngularDamping(BulletBody pBody)
837 {
838 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
839 return body.GetAngularDamping();
840 }
739 841
740 public override float GetLinearSleepingThreshold(BulletBody obj) { /* TODO */ return 0f; } 842 public override float GetLinearSleepingThreshold(BulletBody pBody)
843 {
844 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
845 return body.GetLinearSleepingThreshold();
846 }
741 847
742 public override void ApplyDamping(BulletBody obj, float timeStep) { /* TODO */ } 848 public override void ApplyDamping(BulletBody pBody, float timeStep)
849 {
850 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
851 body.ApplyDamping(timeStep);
852 }
743 853
744 public override Vector3 GetLinearFactor(BulletBody obj) { /* TODO */ return Vector3.Zero; } 854 public override Vector3 GetLinearFactor(BulletBody pBody)
855 {
856 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
857 IndexedVector3 linearFactor = body.GetLinearFactor();
858 return new Vector3(linearFactor.X, linearFactor.Y, linearFactor.Z);
859 }
745 860
746 public override void SetLinearFactor(BulletBody obj, Vector3 factor) { /* TODO */ } 861 public override void SetLinearFactor(BulletBody pBody, Vector3 factor)
862 {
863 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
864 body.SetLinearFactor(new IndexedVector3(factor.X, factor.Y, factor.Z));
865 }
747 866
748 public override void SetCenterOfMassByPosRot(BulletBody obj, Vector3 pos, Quaternion rot) { /* TODO */ } 867 public override void SetCenterOfMassByPosRot(BulletBody pBody, Vector3 pos, Quaternion rot)
868 {
869 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
870 IndexedQuaternion quat = new IndexedQuaternion(rot.X, rot.Y, rot.Z,rot.W);
871 IndexedMatrix mat = IndexedMatrix.CreateFromQuaternion(quat);
872 mat._origin = new IndexedVector3(pos.X, pos.Y, pos.Z);
873 body.SetCenterOfMassTransform( ref mat);
874 /* TODO: double check this */
875 }
749 876
750 //BulletSimAPI.ApplyCentralForce(PhysBody.ptr, fSum); 877 //BulletSimAPI.ApplyCentralForce(PhysBody.ptr, fSum);
751 public override void ApplyCentralForce(BulletBody pBody, Vector3 pfSum) 878 public override void ApplyCentralForce(BulletBody pBody, Vector3 pfSum)
752 { 879 {
753 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 880 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
754 IndexedVector3 fSum = new IndexedVector3(pfSum.X, pfSum.Y, pfSum.Z); 881 IndexedVector3 fSum = new IndexedVector3(pfSum.X, pfSum.Y, pfSum.Z);
755 body.ApplyCentralForce(ref fSum); 882 body.ApplyCentralForce(ref fSum);
756 } 883 }
757 public override void ApplyCentralImpulse(BulletBody pBody, Vector3 pfSum) 884 public override void ApplyCentralImpulse(BulletBody pBody, Vector3 pfSum)
758 { 885 {
759 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 886 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
760 IndexedVector3 fSum = new IndexedVector3(pfSum.X, pfSum.Y, pfSum.Z); 887 IndexedVector3 fSum = new IndexedVector3(pfSum.X, pfSum.Y, pfSum.Z);
761 body.ApplyCentralImpulse(ref fSum); 888 body.ApplyCentralImpulse(ref fSum);
762 } 889 }
763 public override void ApplyTorque(BulletBody pBody, Vector3 pfSum) 890 public override void ApplyTorque(BulletBody pBody, Vector3 pfSum)
764 { 891 {
765 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 892 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
766 IndexedVector3 fSum = new IndexedVector3(pfSum.X, pfSum.Y, pfSum.Z); 893 IndexedVector3 fSum = new IndexedVector3(pfSum.X, pfSum.Y, pfSum.Z);
767 body.ApplyTorque(ref fSum); 894 body.ApplyTorque(ref fSum);
768 } 895 }
769 public override void ApplyTorqueImpulse(BulletBody pBody, Vector3 pfSum) 896 public override void ApplyTorqueImpulse(BulletBody pBody, Vector3 pfSum)
770 { 897 {
771 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 898 RigidBody body = (pBody as BulletBodyXNA).rigidBody;
772 IndexedVector3 fSum = new IndexedVector3(pfSum.X, pfSum.Y, pfSum.Z); 899 IndexedVector3 fSum = new IndexedVector3(pfSum.X, pfSum.Y, pfSum.Z);
773 body.ApplyTorqueImpulse(ref fSum); 900 body.ApplyTorqueImpulse(ref fSum);
774 } 901 }
775 902
776 public override void DumpRigidBody(BulletWorld p, BulletBody p_2) 903 public override void DestroyObject(BulletWorld pWorld, BulletBody pBody)
777 {
778 //TODO:
779 }
780
781 public override void DumpCollisionShape(BulletWorld p, BulletShape p_2)
782 { 904 {
783 //TODO: 905 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
784 } 906 CollisionObject co = (pBody as BulletBodyXNA).rigidBody;
785 public override void DumpConstraint(BulletWorld world, BulletConstraint constrain) 907 RigidBody bo = co as RigidBody;
786 { 908 if (bo == null)
787 //TODO: 909 {
788 }
789
790 public override void DumpActivationInfo(BulletWorld world)
791 {
792 //TODO:
793 }
794 910
795 public override void DumpAllInfo(BulletWorld world) 911 if (world.IsInWorld(co))
796 { 912 {
797 //TODO: 913 world.RemoveCollisionObject(co);
798 } 914 }
915 }
916 else
917 {
799 918
800 public override void DumpPhysicsStatistics(BulletWorld world) 919 if (world.IsInWorld(bo))
801 { 920 {
802 //TODO: 921 world.RemoveRigidBody(bo);
803 } 922 }
923 }
924 if (co != null)
925 {
926 if (co.GetUserPointer() != null)
927 {
928 uint localId = (uint) co.GetUserPointer();
929 if (specialCollisionObjects.ContainsKey(localId))
930 {
931 specialCollisionObjects.Remove(localId);
932 }
933 }
934 }
804 935
805 public override void DestroyObject(BulletWorld p, BulletBody p_2)
806 {
807 //TODO:
808 } 936 }
809 937
810 public override void Shutdown(BulletWorld pWorld) 938 public override void Shutdown(BulletWorld pWorld)
811 { 939 {
812 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 940 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
813 world.Cleanup(); 941 world.Cleanup();
814 } 942 }
815 943
816 public override BulletShape DuplicateCollisionShape(BulletWorld sim, BulletShape srcShape, uint id) 944 public override BulletShape DuplicateCollisionShape(BulletWorld pWorld, BulletShape pShape, uint id)
817 { 945 {
818 return null; 946 CollisionShape shape1 = (pShape as BulletShapeXNA).shape;
947
948 // TODO: Turn this from a reference copy to a Value Copy.
949 BulletShapeXNA shape2 = new BulletShapeXNA(shape1, BSShapeTypeFromBroadPhaseNativeType(shape1.GetShapeType()));
950
951 return shape2;
819 } 952 }
820 953
821 public override bool DeleteCollisionShape(BulletWorld p, BulletShape p_2) 954 public override bool DeleteCollisionShape(BulletWorld pWorld, BulletShape pShape)
822 { 955 {
823 //TODO: 956 //TODO:
824 return false; 957 return false;
825 } 958 }
826 //(sim.ptr, shape.ptr, prim.LocalID, prim.RawPosition, prim.RawOrientation); 959 //(sim.ptr, shape.ptr, prim.LocalID, prim.RawPosition, prim.RawOrientation);
827 960
828 public override BulletBody CreateBodyFromShape(BulletWorld pWorld, BulletShape pShape, uint pLocalID, Vector3 pRawPosition, Quaternion pRawOrientation) 961 public override BulletBody CreateBodyFromShape(BulletWorld pWorld, BulletShape pShape, uint pLocalID, Vector3 pRawPosition, Quaternion pRawOrientation)
829 { 962 {
830 CollisionWorld world = ((BulletWorldXNA)pWorld).world; 963 CollisionWorld world = (pWorld as BulletWorldXNA).world;
831 IndexedMatrix mat = 964 IndexedMatrix mat =
832 IndexedMatrix.CreateFromQuaternion(new IndexedQuaternion(pRawOrientation.X, pRawOrientation.Y, 965 IndexedMatrix.CreateFromQuaternion(new IndexedQuaternion(pRawOrientation.X, pRawOrientation.Y,
833 pRawOrientation.Z, pRawOrientation.W)); 966 pRawOrientation.Z, pRawOrientation.W));
834 mat._origin = new IndexedVector3(pRawPosition.X, pRawPosition.Y, pRawPosition.Z); 967 mat._origin = new IndexedVector3(pRawPosition.X, pRawPosition.Y, pRawPosition.Z);
835 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 968 CollisionShape shape = (pShape as BulletShapeXNA).shape;
836 //UpdateSingleAabb(world, shape); 969 //UpdateSingleAabb(world, shape);
837 // TODO: Feed Update array into null 970 // TODO: Feed Update array into null
838 RigidBody body = new RigidBody(0,new SimMotionState(world,pLocalID,mat,null),shape,IndexedVector3.Zero); 971 SimMotionState motionState = new SimMotionState(this, pLocalID, mat, null);
839 972 RigidBody body = new RigidBody(0,motionState,shape,IndexedVector3.Zero);
973 RigidBodyConstructionInfo constructionInfo = new RigidBodyConstructionInfo(0, motionState, shape, IndexedVector3.Zero)
974 {
975 m_mass = 0
976 };
977 /*
978 m_mass = mass;
979 m_motionState =motionState;
980 m_collisionShape = collisionShape;
981 m_localInertia = localInertia;
982 m_linearDamping = 0f;
983 m_angularDamping = 0f;
984 m_friction = 0.5f;
985 m_restitution = 0f;
986 m_linearSleepingThreshold = 0.8f;
987 m_angularSleepingThreshold = 1f;
988 m_additionalDamping = false;
989 m_additionalDampingFactor = 0.005f;
990 m_additionalLinearDampingThresholdSqr = 0.01f;
991 m_additionalAngularDampingThresholdSqr = 0.01f;
992 m_additionalAngularDampingFactor = 0.01f;
993 m_startWorldTransform = IndexedMatrix.Identity;
994 */
840 body.SetUserPointer(pLocalID); 995 body.SetUserPointer(pLocalID);
996
841 return new BulletBodyXNA(pLocalID, body); 997 return new BulletBodyXNA(pLocalID, body);
842 } 998 }
843 999
844 1000
845 public override BulletBody CreateBodyWithDefaultMotionState( BulletShape pShape, uint pLocalID, Vector3 pRawPosition, Quaternion pRawOrientation) 1001 public override BulletBody CreateBodyWithDefaultMotionState( BulletShape pShape, uint pLocalID, Vector3 pRawPosition, Quaternion pRawOrientation)
846 { 1002 {
847 1003
@@ -850,7 +1006,7 @@ private sealed class BulletConstraintXNA : BulletConstraint
850 pRawOrientation.Z, pRawOrientation.W)); 1006 pRawOrientation.Z, pRawOrientation.W));
851 mat._origin = new IndexedVector3(pRawPosition.X, pRawPosition.Y, pRawPosition.Z); 1007 mat._origin = new IndexedVector3(pRawPosition.X, pRawPosition.Y, pRawPosition.Z);
852 1008
853 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 1009 CollisionShape shape = (pShape as BulletShapeXNA).shape;
854 1010
855 // TODO: Feed Update array into null 1011 // TODO: Feed Update array into null
856 RigidBody body = new RigidBody(0, new DefaultMotionState( mat, IndexedMatrix.Identity), shape, IndexedVector3.Zero); 1012 RigidBody body = new RigidBody(0, new DefaultMotionState( mat, IndexedMatrix.Identity), shape, IndexedVector3.Zero);
@@ -859,21 +1015,43 @@ private sealed class BulletConstraintXNA : BulletConstraint
859 return new BulletBodyXNA(pLocalID, body); 1015 return new BulletBodyXNA(pLocalID, body);
860 } 1016 }
861 //(m_mapInfo.terrainBody.ptr, CollisionFlags.CF_STATIC_OBJECT); 1017 //(m_mapInfo.terrainBody.ptr, CollisionFlags.CF_STATIC_OBJECT);
862 public override CollisionFlags SetCollisionFlags(BulletBody pBody, CollisionFlags collisionFlags) 1018 public override CollisionFlags SetCollisionFlags(BulletBody pCollisionObject, CollisionFlags collisionFlags)
863 { 1019 {
864 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 1020 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
865 body.SetCollisionFlags((BulletXNA.BulletCollision.CollisionFlags) (uint) collisionFlags); 1021 collisionObject.SetCollisionFlags((BulletXNA.BulletCollision.CollisionFlags) (uint) collisionFlags);
866 return (CollisionFlags)body.GetCollisionFlags(); 1022 return (CollisionFlags)collisionObject.GetCollisionFlags();
867 } 1023 }
868 1024
869 public override Vector3 GetAnisotripicFriction(BulletConstraint pconstrain) { /* TODO */ return Vector3.Zero; } 1025 public override Vector3 GetAnisotripicFriction(BulletConstraint pconstrain)
1026 {
1027
1028 /* TODO */
1029 return Vector3.Zero;
1030 }
870 public override Vector3 SetAnisotripicFriction(BulletConstraint pconstrain, Vector3 frict) { /* TODO */ return Vector3.Zero; } 1031 public override Vector3 SetAnisotripicFriction(BulletConstraint pconstrain, Vector3 frict) { /* TODO */ return Vector3.Zero; }
871 public override bool HasAnisotripicFriction(BulletConstraint pconstrain) { /* TODO */ return false; } 1032 public override bool HasAnisotripicFriction(BulletConstraint pconstrain) { /* TODO */ return false; }
872 public override float GetContactProcessingThreshold(BulletBody pBody) { /* TODO */ return 0f; } 1033 public override float GetContactProcessingThreshold(BulletBody pBody) { /* TODO */ return 0f; }
873 public override bool IsStaticObject(BulletBody pBody) { /* TODO */ return false; } 1034 public override bool IsStaticObject(BulletBody pCollisionObject)
874 public override bool IsKinematicObject(BulletBody pBody) { /* TODO */ return false; } 1035 {
875 public override bool IsStaticOrKinematicObject(BulletBody pBody) { /* TODO */ return false; } 1036 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
876 public override bool HasContactResponse(BulletBody pBody) { /* TODO */ return false; } 1037 return collisionObject.IsStaticObject();
1038
1039 }
1040 public override bool IsKinematicObject(BulletBody pCollisionObject)
1041 {
1042 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
1043 return collisionObject.IsKinematicObject();
1044 }
1045 public override bool IsStaticOrKinematicObject(BulletBody pCollisionObject)
1046 {
1047 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
1048 return collisionObject.IsStaticOrKinematicObject();
1049 }
1050 public override bool HasContactResponse(BulletBody pCollisionObject)
1051 {
1052 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
1053 return collisionObject.HasContactResponse();
1054 }
877 public override int GetActivationState(BulletBody pBody) { /* TODO */ return 0; } 1055 public override int GetActivationState(BulletBody pBody) { /* TODO */ return 0; }
878 public override void SetActivationState(BulletBody pBody, int state) { /* TODO */ } 1056 public override void SetActivationState(BulletBody pBody, int state) { /* TODO */ }
879 public override float GetDeactivationTime(BulletBody pBody) { /* TODO */ return 0f; } 1057 public override float GetDeactivationTime(BulletBody pBody) { /* TODO */ return 0f; }
@@ -884,15 +1062,15 @@ private sealed class BulletConstraintXNA : BulletConstraint
884 public override float GetHitFraction(BulletBody pBody) { /* TODO */ return 0f; } 1062 public override float GetHitFraction(BulletBody pBody) { /* TODO */ return 0f; }
885 1063
886 //(m_mapInfo.terrainBody.ptr, PhysicsScene.Params.terrainHitFraction); 1064 //(m_mapInfo.terrainBody.ptr, PhysicsScene.Params.terrainHitFraction);
887 public override void SetHitFraction(BulletBody pBody, float pHitFraction) 1065 public override void SetHitFraction(BulletBody pCollisionObject, float pHitFraction)
888 { 1066 {
889 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 1067 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
890 body.SetHitFraction(pHitFraction); 1068 collisionObject.SetHitFraction(pHitFraction);
891 } 1069 }
892 //BuildCapsuleShape(physicsScene.World.ptr, 1f, 1f, prim.Scale); 1070 //BuildCapsuleShape(physicsScene.World.ptr, 1f, 1f, prim.Scale);
893 public override BulletShape BuildCapsuleShape(BulletWorld pWorld, float pRadius, float pHeight, Vector3 pScale) 1071 public override BulletShape BuildCapsuleShape(BulletWorld pWorld, float pRadius, float pHeight, Vector3 pScale)
894 { 1072 {
895 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 1073 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
896 IndexedVector3 scale = new IndexedVector3(pScale.X, pScale.Y, pScale.Z); 1074 IndexedVector3 scale = new IndexedVector3(pScale.X, pScale.Y, pScale.Z);
897 CapsuleShapeZ capsuleShapeZ = new CapsuleShapeZ(pRadius, pHeight); 1075 CapsuleShapeZ capsuleShapeZ = new CapsuleShapeZ(pRadius, pHeight);
898 capsuleShapeZ.SetMargin(world.WorldSettings.Params.collisionMargin); 1076 capsuleShapeZ.SetMargin(world.WorldSettings.Params.collisionMargin);
@@ -906,19 +1084,29 @@ private sealed class BulletConstraintXNA : BulletConstraint
906 int maxUpdates, ref EntityProperties[] updateArray 1084 int maxUpdates, ref EntityProperties[] updateArray
907 ) 1085 )
908 { 1086 {
1087
1088 UpdatedObjects = updateArray;
1089 UpdatedCollisions = collisionArray;
909 /* TODO */ 1090 /* TODO */
910 return new BulletWorldXNA(1, null, null); 1091 ConfigurationParameters[] configparms = new ConfigurationParameters[1];
1092 configparms[0] = parms;
1093 Vector3 worldExtent = new Vector3(Constants.RegionSize, Constants.RegionSize, Constants.RegionHeight);
1094 m_maxCollisions = maxCollisions;
1095 m_maxUpdatesPerFrame = maxUpdates;
1096 specialCollisionObjects = new Dictionary<uint, GhostObject>();
1097
1098 return new BulletWorldXNA(1, PhysicsScene, BSAPIXNA.Initialize2(worldExtent, configparms, maxCollisions, ref collisionArray, maxUpdates, ref updateArray, null));
911 } 1099 }
912 1100
913 private static object Initialize2(Vector3 worldExtent, 1101 private static DiscreteDynamicsWorld Initialize2(Vector3 worldExtent,
914 ConfigurationParameters[] o, 1102 ConfigurationParameters[] o,
915 int mMaxCollisionsPerFrame, ref List<BulletXNA.CollisionDesc> collisionArray, 1103 int mMaxCollisionsPerFrame, ref CollisionDesc[] collisionArray,
916 int mMaxUpdatesPerFrame, ref List<BulletXNA.EntityProperties> updateArray, 1104 int mMaxUpdatesPerFrame, ref EntityProperties[] updateArray,
917 object mDebugLogCallbackHandle) 1105 object mDebugLogCallbackHandle)
918 { 1106 {
919 CollisionWorld.WorldData.ParamData p = new CollisionWorld.WorldData.ParamData(); 1107 CollisionWorld.WorldData.ParamData p = new CollisionWorld.WorldData.ParamData();
920 1108
921 p.angularDamping = o[0].XangularDamping; 1109 p.angularDamping = BSParam.AngularDamping;
922 p.defaultFriction = o[0].defaultFriction; 1110 p.defaultFriction = o[0].defaultFriction;
923 p.defaultFriction = o[0].defaultFriction; 1111 p.defaultFriction = o[0].defaultFriction;
924 p.defaultDensity = o[0].defaultDensity; 1112 p.defaultDensity = o[0].defaultDensity;
@@ -926,33 +1114,33 @@ private sealed class BulletConstraintXNA : BulletConstraint
926 p.collisionMargin = o[0].collisionMargin; 1114 p.collisionMargin = o[0].collisionMargin;
927 p.gravity = o[0].gravity; 1115 p.gravity = o[0].gravity;
928 1116
929 p.linearDamping = o[0].XlinearDamping; 1117 p.linearDamping = BSParam.LinearDamping;
930 p.angularDamping = o[0].XangularDamping; 1118 p.angularDamping = BSParam.AngularDamping;
931 p.deactivationTime = o[0].XdeactivationTime; 1119 p.deactivationTime = BSParam.DeactivationTime;
932 p.linearSleepingThreshold = o[0].XlinearSleepingThreshold; 1120 p.linearSleepingThreshold = BSParam.LinearSleepingThreshold;
933 p.angularSleepingThreshold = o[0].XangularSleepingThreshold; 1121 p.angularSleepingThreshold = BSParam.AngularSleepingThreshold;
934 p.ccdMotionThreshold = o[0].XccdMotionThreshold; 1122 p.ccdMotionThreshold = BSParam.CcdMotionThreshold;
935 p.ccdSweptSphereRadius = o[0].XccdSweptSphereRadius; 1123 p.ccdSweptSphereRadius = BSParam.CcdSweptSphereRadius;
936 p.contactProcessingThreshold = o[0].XcontactProcessingThreshold; 1124 p.contactProcessingThreshold = BSParam.ContactProcessingThreshold;
937 1125
938 p.terrainImplementation = o[0].XterrainImplementation; 1126 p.terrainImplementation = BSParam.TerrainImplementation;
939 p.terrainFriction = o[0].XterrainFriction; 1127 p.terrainFriction = BSParam.TerrainFriction;
940 1128
941 p.terrainHitFraction = o[0].XterrainHitFraction; 1129 p.terrainHitFraction = BSParam.TerrainHitFraction;
942 p.terrainRestitution = o[0].XterrainRestitution; 1130 p.terrainRestitution = BSParam.TerrainRestitution;
943 p.terrainCollisionMargin = o[0].XterrainCollisionMargin; 1131 p.terrainCollisionMargin = BSParam.TerrainCollisionMargin;
944 1132
945 p.avatarFriction = o[0].XavatarFriction; 1133 p.avatarFriction = BSParam.AvatarFriction;
946 p.avatarStandingFriction = o[0].XavatarStandingFriction; 1134 p.avatarStandingFriction = BSParam.AvatarStandingFriction;
947 p.avatarDensity = o[0].XavatarDensity; 1135 p.avatarDensity = BSParam.AvatarDensity;
948 p.avatarRestitution = o[0].XavatarRestitution; 1136 p.avatarRestitution = BSParam.AvatarRestitution;
949 p.avatarCapsuleWidth = o[0].XavatarCapsuleWidth; 1137 p.avatarCapsuleWidth = BSParam.AvatarCapsuleWidth;
950 p.avatarCapsuleDepth = o[0].XavatarCapsuleDepth; 1138 p.avatarCapsuleDepth = BSParam.AvatarCapsuleDepth;
951 p.avatarCapsuleHeight = o[0].XavatarCapsuleHeight; 1139 p.avatarCapsuleHeight = BSParam.AvatarCapsuleHeight;
952 p.avatarContactProcessingThreshold = o[0].XavatarContactProcessingThreshold; 1140 p.avatarContactProcessingThreshold = BSParam.AvatarContactProcessingThreshold;
953 1141
954 p.vehicleAngularDamping = o[0].XvehicleAngularDamping; 1142 p.vehicleAngularDamping = BSParam.VehicleAngularDamping;
955 1143
956 p.maxPersistantManifoldPoolSize = o[0].maxPersistantManifoldPoolSize; 1144 p.maxPersistantManifoldPoolSize = o[0].maxPersistantManifoldPoolSize;
957 p.maxCollisionAlgorithmPoolSize = o[0].maxCollisionAlgorithmPoolSize; 1145 p.maxCollisionAlgorithmPoolSize = o[0].maxCollisionAlgorithmPoolSize;
958 p.shouldDisableContactPoolDynamicAllocation = o[0].shouldDisableContactPoolDynamicAllocation; 1146 p.shouldDisableContactPoolDynamicAllocation = o[0].shouldDisableContactPoolDynamicAllocation;
@@ -962,17 +1150,17 @@ private sealed class BulletConstraintXNA : BulletConstraint
962 p.shouldEnableFrictionCaching = o[0].shouldEnableFrictionCaching; 1150 p.shouldEnableFrictionCaching = o[0].shouldEnableFrictionCaching;
963 p.numberOfSolverIterations = o[0].numberOfSolverIterations; 1151 p.numberOfSolverIterations = o[0].numberOfSolverIterations;
964 1152
965 p.linksetImplementation = o[0].XlinksetImplementation; 1153 p.linksetImplementation = BSParam.LinksetImplementation;
966 p.linkConstraintUseFrameOffset = o[0].XlinkConstraintUseFrameOffset; 1154 p.linkConstraintUseFrameOffset = BSParam.NumericBool(BSParam.LinkConstraintUseFrameOffset);
967 p.linkConstraintEnableTransMotor = o[0].XlinkConstraintEnableTransMotor; 1155 p.linkConstraintEnableTransMotor = BSParam.NumericBool(BSParam.LinkConstraintEnableTransMotor);
968 p.linkConstraintTransMotorMaxVel = o[0].XlinkConstraintTransMotorMaxVel; 1156 p.linkConstraintTransMotorMaxVel = BSParam.LinkConstraintTransMotorMaxVel;
969 p.linkConstraintTransMotorMaxForce = o[0].XlinkConstraintTransMotorMaxForce; 1157 p.linkConstraintTransMotorMaxForce = BSParam.LinkConstraintTransMotorMaxForce;
970 p.linkConstraintERP = o[0].XlinkConstraintERP; 1158 p.linkConstraintERP = BSParam.LinkConstraintERP;
971 p.linkConstraintCFM = o[0].XlinkConstraintCFM; 1159 p.linkConstraintCFM = BSParam.LinkConstraintCFM;
972 p.linkConstraintSolverIterations = o[0].XlinkConstraintSolverIterations; 1160 p.linkConstraintSolverIterations = BSParam.LinkConstraintSolverIterations;
973 p.physicsLoggingFrames = o[0].XphysicsLoggingFrames; 1161 p.physicsLoggingFrames = o[0].physicsLoggingFrames;
974 DefaultCollisionConstructionInfo ccci = new DefaultCollisionConstructionInfo(); 1162 DefaultCollisionConstructionInfo ccci = new DefaultCollisionConstructionInfo();
975 1163
976 DefaultCollisionConfiguration cci = new DefaultCollisionConfiguration(); 1164 DefaultCollisionConfiguration cci = new DefaultCollisionConfiguration();
977 CollisionDispatcher m_dispatcher = new CollisionDispatcher(cci); 1165 CollisionDispatcher m_dispatcher = new CollisionDispatcher(cci);
978 1166
@@ -993,8 +1181,10 @@ private sealed class BulletConstraintXNA : BulletConstraint
993 SequentialImpulseConstraintSolver m_solver = new SequentialImpulseConstraintSolver(); 1181 SequentialImpulseConstraintSolver m_solver = new SequentialImpulseConstraintSolver();
994 1182
995 DiscreteDynamicsWorld world = new DiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_solver, cci); 1183 DiscreteDynamicsWorld world = new DiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_solver, cci);
996 world.UpdatedObjects = updateArray; 1184
997 world.UpdatedCollisions = collisionArray; 1185 world.LastCollisionDesc = 0;
1186 world.LastEntityProperty = 0;
1187
998 world.WorldSettings.Params = p; 1188 world.WorldSettings.Params = p;
999 world.SetForceUpdateAllAabbs(p.shouldForceUpdateAllAabbs != 0); 1189 world.SetForceUpdateAllAabbs(p.shouldForceUpdateAllAabbs != 0);
1000 world.GetSolverInfo().m_solverMode = SolverMode.SOLVER_USE_WARMSTARTING | SolverMode.SOLVER_SIMD; 1190 world.GetSolverInfo().m_solverMode = SolverMode.SOLVER_USE_WARMSTARTING | SolverMode.SOLVER_SIMD;
@@ -1028,7 +1218,7 @@ private sealed class BulletConstraintXNA : BulletConstraint
1028 world.GetSolverInfo().m_restingContactRestitutionThreshold = 2; 1218 world.GetSolverInfo().m_restingContactRestitutionThreshold = 2;
1029 world.SetForceUpdateAllAabbs(true); 1219 world.SetForceUpdateAllAabbs(true);
1030 1220
1031 1221 //BSParam.TerrainImplementation = 0;
1032 world.SetGravity(new IndexedVector3(0,0,p.gravity)); 1222 world.SetGravity(new IndexedVector3(0,0,p.gravity));
1033 1223
1034 return world; 1224 return world;
@@ -1036,7 +1226,7 @@ private sealed class BulletConstraintXNA : BulletConstraint
1036 //m_constraint.ptr, ConstraintParams.BT_CONSTRAINT_STOP_CFM, cfm, ConstraintParamAxis.AXIS_ALL 1226 //m_constraint.ptr, ConstraintParams.BT_CONSTRAINT_STOP_CFM, cfm, ConstraintParamAxis.AXIS_ALL
1037 public override bool SetConstraintParam(BulletConstraint pConstraint, ConstraintParams paramIndex, float paramvalue, ConstraintParamAxis axis) 1227 public override bool SetConstraintParam(BulletConstraint pConstraint, ConstraintParams paramIndex, float paramvalue, ConstraintParamAxis axis)
1038 { 1228 {
1039 Generic6DofConstraint constrain = ((BulletConstraintXNA)pConstraint).constrain as Generic6DofConstraint; 1229 Generic6DofConstraint constrain = (pConstraint as BulletConstraintXNA).constrain as Generic6DofConstraint;
1040 if (axis == ConstraintParamAxis.AXIS_LINEAR_ALL || axis == ConstraintParamAxis.AXIS_ALL) 1230 if (axis == ConstraintParamAxis.AXIS_LINEAR_ALL || axis == ConstraintParamAxis.AXIS_ALL)
1041 { 1231 {
1042 constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams) (int) paramIndex, paramvalue, 0); 1232 constrain.SetParam((BulletXNA.BulletDynamics.ConstraintParams) (int) paramIndex, paramvalue, 0);
@@ -1059,7 +1249,8 @@ private sealed class BulletConstraintXNA : BulletConstraint
1059 public override bool PushUpdate(BulletBody pCollisionObject) 1249 public override bool PushUpdate(BulletBody pCollisionObject)
1060 { 1250 {
1061 bool ret = false; 1251 bool ret = false;
1062 RigidBody rb = ((BulletBodyXNA)pCollisionObject).rigidBody; 1252 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
1253 RigidBody rb = collisionObject as RigidBody;
1063 if (rb != null) 1254 if (rb != null)
1064 { 1255 {
1065 SimMotionState sms = rb.GetMotionState() as SimMotionState; 1256 SimMotionState sms = rb.GetMotionState() as SimMotionState;
@@ -1072,62 +1263,62 @@ private sealed class BulletConstraintXNA : BulletConstraint
1072 } 1263 }
1073 } 1264 }
1074 return ret; 1265 return ret;
1075 1266
1076 } 1267 }
1077 1268
1078 public override float GetAngularMotionDisc(BulletShape pShape) 1269 public override float GetAngularMotionDisc(BulletShape pShape)
1079 { 1270 {
1080 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 1271 CollisionShape shape = (pShape as BulletShapeXNA).shape;
1081 return shape.GetAngularMotionDisc(); 1272 return shape.GetAngularMotionDisc();
1082 } 1273 }
1083 public override float GetContactBreakingThreshold(BulletShape pShape, float defaultFactor) 1274 public override float GetContactBreakingThreshold(BulletShape pShape, float defaultFactor)
1084 { 1275 {
1085 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 1276 CollisionShape shape = (pShape as BulletShapeXNA).shape;
1086 return shape.GetContactBreakingThreshold(defaultFactor); 1277 return shape.GetContactBreakingThreshold(defaultFactor);
1087 } 1278 }
1088 public override bool IsCompound(BulletShape pShape) 1279 public override bool IsCompound(BulletShape pShape)
1089 { 1280 {
1090 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 1281 CollisionShape shape = (pShape as BulletShapeXNA).shape;
1091 return shape.IsCompound(); 1282 return shape.IsCompound();
1092 } 1283 }
1093 public override bool IsSoftBody(BulletShape pShape) 1284 public override bool IsSoftBody(BulletShape pShape)
1094 { 1285 {
1095 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 1286 CollisionShape shape = (pShape as BulletShapeXNA).shape;
1096 return shape.IsSoftBody(); 1287 return shape.IsSoftBody();
1097 } 1288 }
1098 public override bool IsPolyhedral(BulletShape pShape) 1289 public override bool IsPolyhedral(BulletShape pShape)
1099 { 1290 {
1100 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 1291 CollisionShape shape = (pShape as BulletShapeXNA).shape;
1101 return shape.IsPolyhedral(); 1292 return shape.IsPolyhedral();
1102 } 1293 }
1103 public override bool IsConvex2d(BulletShape pShape) 1294 public override bool IsConvex2d(BulletShape pShape)
1104 { 1295 {
1105 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 1296 CollisionShape shape = (pShape as BulletShapeXNA).shape;
1106 return shape.IsConvex2d(); 1297 return shape.IsConvex2d();
1107 } 1298 }
1108 public override bool IsConvex(BulletShape pShape) 1299 public override bool IsConvex(BulletShape pShape)
1109 { 1300 {
1110 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 1301 CollisionShape shape = (pShape as BulletShapeXNA).shape;
1111 return shape.IsConvex(); 1302 return shape.IsConvex();
1112 } 1303 }
1113 public override bool IsNonMoving(BulletShape pShape) 1304 public override bool IsNonMoving(BulletShape pShape)
1114 { 1305 {
1115 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 1306 CollisionShape shape = (pShape as BulletShapeXNA).shape;
1116 return shape.IsNonMoving(); 1307 return shape.IsNonMoving();
1117 } 1308 }
1118 public override bool IsConcave(BulletShape pShape) 1309 public override bool IsConcave(BulletShape pShape)
1119 { 1310 {
1120 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 1311 CollisionShape shape = (pShape as BulletShapeXNA).shape;
1121 return shape.IsConcave(); 1312 return shape.IsConcave();
1122 } 1313 }
1123 public override bool IsInfinite(BulletShape pShape) 1314 public override bool IsInfinite(BulletShape pShape)
1124 { 1315 {
1125 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 1316 CollisionShape shape = (pShape as BulletShapeXNA).shape;
1126 return shape.IsInfinite(); 1317 return shape.IsInfinite();
1127 } 1318 }
1128 public override bool IsNativeShape(BulletShape pShape) 1319 public override bool IsNativeShape(BulletShape pShape)
1129 { 1320 {
1130 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 1321 CollisionShape shape = (pShape as BulletShapeXNA).shape;
1131 bool ret; 1322 bool ret;
1132 switch (shape.GetShapeType()) 1323 switch (shape.GetShapeType())
1133 { 1324 {
@@ -1144,38 +1335,59 @@ private sealed class BulletConstraintXNA : BulletConstraint
1144 return ret; 1335 return ret;
1145 } 1336 }
1146 1337
1147 public override void SetShapeCollisionMargin(BulletShape shape, float margin) { /* TODO */ } 1338 public override void SetShapeCollisionMargin(BulletShape pShape, float pMargin)
1339 {
1340 CollisionShape shape = (pShape as BulletShapeXNA).shape;
1341 shape.SetMargin(pMargin);
1342 }
1148 1343
1149 //sim.ptr, shape.ptr,prim.LocalID, prim.RawPosition, prim.RawOrientation 1344 //sim.ptr, shape.ptr,prim.LocalID, prim.RawPosition, prim.RawOrientation
1150 public override BulletBody CreateGhostFromShape(BulletWorld pWorld, BulletShape pShape, uint pLocalID, Vector3 pRawPosition, Quaternion pRawOrientation) 1345 public override BulletBody CreateGhostFromShape(BulletWorld pWorld, BulletShape pShape, uint pLocalID, Vector3 pRawPosition, Quaternion pRawOrientation)
1151 { 1346 {
1152 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 1347 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
1153 IndexedMatrix bodyTransform = new IndexedMatrix(); 1348 IndexedMatrix bodyTransform = new IndexedMatrix();
1154 bodyTransform._origin = new IndexedVector3(pRawPosition.X, pRawPosition.Y, pRawPosition.Z); 1349 bodyTransform._origin = new IndexedVector3(pRawPosition.X, pRawPosition.Y, pRawPosition.Z);
1155 bodyTransform.SetRotation(new IndexedQuaternion(pRawOrientation.X,pRawOrientation.Y,pRawOrientation.Z,pRawOrientation.W)); 1350 bodyTransform.SetRotation(new IndexedQuaternion(pRawOrientation.X,pRawOrientation.Y,pRawOrientation.Z,pRawOrientation.W));
1156 GhostObject gObj = new PairCachingGhostObject(); 1351 GhostObject gObj = new PairCachingGhostObject();
1157 gObj.SetWorldTransform(bodyTransform); 1352 gObj.SetWorldTransform(bodyTransform);
1158 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 1353 CollisionShape shape = (pShape as BulletShapeXNA).shape;
1159 gObj.SetCollisionShape(shape); 1354 gObj.SetCollisionShape(shape);
1160 gObj.SetUserPointer(pLocalID); 1355 gObj.SetUserPointer(pLocalID);
1356
1357 if (specialCollisionObjects.ContainsKey(pLocalID))
1358 specialCollisionObjects[pLocalID] = gObj;
1359 else
1360 specialCollisionObjects.Add(pLocalID, gObj);
1361
1161 // TODO: Add to Special CollisionObjects! 1362 // TODO: Add to Special CollisionObjects!
1162 return new BulletBodyXNA(pLocalID, gObj); 1363 return new BulletBodyXNA(pLocalID, gObj);
1163 } 1364 }
1164 1365
1165 public override void SetCollisionShape(BulletWorld pWorld, BulletBody pObj, BulletShape pShape) 1366 public override void SetCollisionShape(BulletWorld pWorld, BulletBody pCollisionObject, BulletShape pShape)
1166 { 1367 {
1167 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 1368 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
1168 CollisionObject obj = ((BulletBodyXNA)pObj).body; 1369 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).body;
1169 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 1370 if (pShape == null)
1170 obj.SetCollisionShape(shape); 1371 {
1171 1372 collisionObject.SetCollisionShape(new EmptyShape());
1373 }
1374 else
1375 {
1376 CollisionShape shape = (pShape as BulletShapeXNA).shape;
1377 collisionObject.SetCollisionShape(shape);
1378 }
1379 }
1380 public override BulletShape GetCollisionShape(BulletBody pCollisionObject)
1381 {
1382 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
1383 CollisionShape shape = collisionObject.GetCollisionShape();
1384 return new BulletShapeXNA(shape, BSShapeTypeFromBroadPhaseNativeType(shape.GetShapeType()));
1172 } 1385 }
1173 public override BulletShape GetCollisionShape(BulletBody obj) { /* TODO */ return null; }
1174 1386
1175 //(PhysicsScene.World.ptr, nativeShapeData) 1387 //(PhysicsScene.World.ptr, nativeShapeData)
1176 public override BulletShape BuildNativeShape(BulletWorld pWorld, ShapeData pShapeData) 1388 public override BulletShape BuildNativeShape(BulletWorld pWorld, ShapeData pShapeData)
1177 { 1389 {
1178 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 1390 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
1179 CollisionShape shape = null; 1391 CollisionShape shape = null;
1180 switch (pShapeData.Type) 1392 switch (pShapeData.Type)
1181 { 1393 {
@@ -1210,15 +1422,15 @@ private sealed class BulletConstraintXNA : BulletConstraint
1210 1422
1211 public override int GetNumberOfCompoundChildren(BulletShape pCompoundShape) 1423 public override int GetNumberOfCompoundChildren(BulletShape pCompoundShape)
1212 { 1424 {
1213 CompoundShape compoundshape = ((BulletShapeXNA)pCompoundShape).shape as CompoundShape; 1425 CompoundShape compoundshape = (pCompoundShape as BulletShapeXNA).shape as CompoundShape;
1214 return compoundshape.GetNumChildShapes(); 1426 return compoundshape.GetNumChildShapes();
1215 } 1427 }
1216 //LinksetRoot.PhysShape.ptr, newShape.ptr, displacementPos, displacementRot 1428 //LinksetRoot.PhysShape.ptr, newShape.ptr, displacementPos, displacementRot
1217 public override void AddChildShapeToCompoundShape(BulletShape pCShape, BulletShape paddShape, Vector3 displacementPos, Quaternion displacementRot) 1429 public override void AddChildShapeToCompoundShape(BulletShape pCShape, BulletShape paddShape, Vector3 displacementPos, Quaternion displacementRot)
1218 { 1430 {
1219 IndexedMatrix relativeTransform = new IndexedMatrix(); 1431 IndexedMatrix relativeTransform = new IndexedMatrix();
1220 CompoundShape compoundshape = ((BulletShapeXNA)pCShape).shape as CompoundShape; 1432 CompoundShape compoundshape = (pCShape as BulletShapeXNA).shape as CompoundShape;
1221 CollisionShape addshape = ((BulletShapeXNA)paddShape).shape; 1433 CollisionShape addshape = (paddShape as BulletShapeXNA).shape;
1222 1434
1223 relativeTransform._origin = new IndexedVector3(displacementPos.X, displacementPos.Y, displacementPos.Z); 1435 relativeTransform._origin = new IndexedVector3(displacementPos.X, displacementPos.Y, displacementPos.Z);
1224 relativeTransform.SetRotation(new IndexedQuaternion(displacementRot.X,displacementRot.Y,displacementRot.Z,displacementRot.W)); 1436 relativeTransform.SetRotation(new IndexedQuaternion(displacementRot.X,displacementRot.Y,displacementRot.Z,displacementRot.W));
@@ -1228,15 +1440,155 @@ private sealed class BulletConstraintXNA : BulletConstraint
1228 1440
1229 public override BulletShape RemoveChildShapeFromCompoundShapeIndex(BulletShape pCShape, int pii) 1441 public override BulletShape RemoveChildShapeFromCompoundShapeIndex(BulletShape pCShape, int pii)
1230 { 1442 {
1231 CompoundShape compoundshape = ((BulletShapeXNA)pCShape).shape as CompoundShape; 1443 CompoundShape compoundshape = (pCShape as BulletShapeXNA).shape as CompoundShape;
1232 CollisionShape ret = null; 1444 CollisionShape ret = null;
1233 ret = compoundshape.GetChildShape(pii); 1445 ret = compoundshape.GetChildShape(pii);
1234 compoundshape.RemoveChildShapeByIndex(pii); 1446 compoundshape.RemoveChildShapeByIndex(pii);
1235 return new BulletShapeXNA(ret, BSPhysicsShapeType.SHAPE_UNKNOWN); 1447 return new BulletShapeXNA(ret, BSShapeTypeFromBroadPhaseNativeType(ret.GetShapeType()));
1448 }
1449
1450 public override BulletShape GetChildShapeFromCompoundShapeIndex(BulletShape cShape, int indx) {
1451
1452 if (cShape == null)
1453 return null;
1454 CompoundShape compoundShape = (cShape as BulletShapeXNA).shape as CompoundShape;
1455 CollisionShape shape = compoundShape.GetChildShape(indx);
1456 BulletShape retShape = new BulletShapeXNA(shape, BSShapeTypeFromBroadPhaseNativeType(shape.GetShapeType()));
1457
1458
1459 return retShape;
1460 }
1461
1462 public BSPhysicsShapeType BSShapeTypeFromBroadPhaseNativeType(BroadphaseNativeTypes pin)
1463 {
1464 BSPhysicsShapeType ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1465 switch (pin)
1466 {
1467 case BroadphaseNativeTypes.BOX_SHAPE_PROXYTYPE:
1468 ret = BSPhysicsShapeType.SHAPE_BOX;
1469 break;
1470 case BroadphaseNativeTypes.TRIANGLE_SHAPE_PROXYTYPE:
1471 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1472 break;
1473
1474 case BroadphaseNativeTypes.TETRAHEDRAL_SHAPE_PROXYTYPE:
1475 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1476 break;
1477 case BroadphaseNativeTypes.CONVEX_TRIANGLEMESH_SHAPE_PROXYTYPE:
1478 ret = BSPhysicsShapeType.SHAPE_MESH;
1479 break;
1480 case BroadphaseNativeTypes.CONVEX_HULL_SHAPE_PROXYTYPE:
1481 ret = BSPhysicsShapeType.SHAPE_HULL;
1482 break;
1483 case BroadphaseNativeTypes.CONVEX_POINT_CLOUD_SHAPE_PROXYTYPE:
1484 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1485 break;
1486 case BroadphaseNativeTypes.CUSTOM_POLYHEDRAL_SHAPE_TYPE:
1487 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1488 break;
1489 //implicit convex shapes
1490 case BroadphaseNativeTypes.IMPLICIT_CONVEX_SHAPES_START_HERE:
1491 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1492 break;
1493 case BroadphaseNativeTypes.SPHERE_SHAPE_PROXYTYPE:
1494 ret = BSPhysicsShapeType.SHAPE_SPHERE;
1495 break;
1496 case BroadphaseNativeTypes.MULTI_SPHERE_SHAPE_PROXYTYPE:
1497 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1498 break;
1499 case BroadphaseNativeTypes.CAPSULE_SHAPE_PROXYTYPE:
1500 ret = BSPhysicsShapeType.SHAPE_CAPSULE;
1501 break;
1502 case BroadphaseNativeTypes.CONE_SHAPE_PROXYTYPE:
1503 ret = BSPhysicsShapeType.SHAPE_CONE;
1504 break;
1505 case BroadphaseNativeTypes.CONVEX_SHAPE_PROXYTYPE:
1506 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1507 break;
1508 case BroadphaseNativeTypes.CYLINDER_SHAPE_PROXYTYPE:
1509 ret = BSPhysicsShapeType.SHAPE_CYLINDER;
1510 break;
1511 case BroadphaseNativeTypes.UNIFORM_SCALING_SHAPE_PROXYTYPE:
1512 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1513 break;
1514 case BroadphaseNativeTypes.MINKOWSKI_SUM_SHAPE_PROXYTYPE:
1515 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1516 break;
1517 case BroadphaseNativeTypes.MINKOWSKI_DIFFERENCE_SHAPE_PROXYTYPE:
1518 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1519 break;
1520 case BroadphaseNativeTypes.BOX_2D_SHAPE_PROXYTYPE:
1521 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1522 break;
1523 case BroadphaseNativeTypes.CONVEX_2D_SHAPE_PROXYTYPE:
1524 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1525 break;
1526 case BroadphaseNativeTypes.CUSTOM_CONVEX_SHAPE_TYPE:
1527 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1528 break;
1529 //concave shape
1530 case BroadphaseNativeTypes.CONCAVE_SHAPES_START_HERE:
1531 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1532 break;
1533 //keep all the convex shapetype below here, for the check IsConvexShape in broadphase proxy!
1534 case BroadphaseNativeTypes.TRIANGLE_MESH_SHAPE_PROXYTYPE:
1535 ret = BSPhysicsShapeType.SHAPE_MESH;
1536 break;
1537 case BroadphaseNativeTypes.SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE:
1538 ret = BSPhysicsShapeType.SHAPE_MESH;
1539 break;
1540 ///used for demo integration FAST/Swift collision library and Bullet
1541 case BroadphaseNativeTypes.FAST_CONCAVE_MESH_PROXYTYPE:
1542 ret = BSPhysicsShapeType.SHAPE_MESH;
1543 break;
1544 //terrain
1545 case BroadphaseNativeTypes.TERRAIN_SHAPE_PROXYTYPE:
1546 ret = BSPhysicsShapeType.SHAPE_HEIGHTMAP;
1547 break;
1548 ///Used for GIMPACT Trimesh integration
1549 case BroadphaseNativeTypes.GIMPACT_SHAPE_PROXYTYPE:
1550 ret = BSPhysicsShapeType.SHAPE_MESH;
1551 break;
1552 ///Multimaterial mesh
1553 case BroadphaseNativeTypes.MULTIMATERIAL_TRIANGLE_MESH_PROXYTYPE:
1554 ret = BSPhysicsShapeType.SHAPE_MESH;
1555 break;
1556
1557 case BroadphaseNativeTypes.EMPTY_SHAPE_PROXYTYPE:
1558 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1559 break;
1560 case BroadphaseNativeTypes.STATIC_PLANE_PROXYTYPE:
1561 ret = BSPhysicsShapeType.SHAPE_GROUNDPLANE;
1562 break;
1563 case BroadphaseNativeTypes.CUSTOM_CONCAVE_SHAPE_TYPE:
1564 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1565 break;
1566 case BroadphaseNativeTypes.CONCAVE_SHAPES_END_HERE:
1567 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1568 break;
1569
1570 case BroadphaseNativeTypes.COMPOUND_SHAPE_PROXYTYPE:
1571 ret = BSPhysicsShapeType.SHAPE_COMPOUND;
1572 break;
1573
1574 case BroadphaseNativeTypes.SOFTBODY_SHAPE_PROXYTYPE:
1575 ret = BSPhysicsShapeType.SHAPE_MESH;
1576 break;
1577 case BroadphaseNativeTypes.HFFLUID_SHAPE_PROXYTYPE:
1578 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1579 break;
1580 case BroadphaseNativeTypes.HFFLUID_BUOYANT_CONVEX_SHAPE_PROXYTYPE:
1581 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1582 break;
1583 case BroadphaseNativeTypes.INVALID_SHAPE_PROXYTYPE:
1584 ret = BSPhysicsShapeType.SHAPE_UNKNOWN;
1585 break;
1586 }
1587 return ret;
1236 } 1588 }
1237 1589
1238 public override BulletShape GetChildShapeFromCompoundShapeIndex(BulletShape cShape, int indx) { /* TODO */ return null; }
1239 public override void RemoveChildShapeFromCompoundShape(BulletShape cShape, BulletShape removeShape) { /* TODO */ } 1590 public override void RemoveChildShapeFromCompoundShape(BulletShape cShape, BulletShape removeShape) { /* TODO */ }
1591 public override void UpdateChildTransform(BulletShape pShape, int childIndex, Vector3 pos, Quaternion rot, bool shouldRecalculateLocalAabb) { /* TODO */ }
1240 1592
1241 public override BulletShape CreateGroundPlaneShape(uint pLocalId, float pheight, float pcollisionMargin) 1593 public override BulletShape CreateGroundPlaneShape(uint pLocalId, float pheight, float pcollisionMargin)
1242 { 1594 {
@@ -1246,18 +1598,144 @@ private sealed class BulletConstraintXNA : BulletConstraint
1246 return new BulletShapeXNA(m_planeshape, BSPhysicsShapeType.SHAPE_GROUNDPLANE); 1598 return new BulletShapeXNA(m_planeshape, BSPhysicsShapeType.SHAPE_GROUNDPLANE);
1247 } 1599 }
1248 1600
1249 public override BulletConstraint CreateHingeConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody ppBody2, Vector3 ppivotInA, Vector3 ppivotInB, Vector3 paxisInA, Vector3 paxisInB, bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies) 1601 public override BulletConstraint Create6DofSpringConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
1602 Vector3 pframe1, Quaternion pframe1rot, Vector3 pframe2, Quaternion pframe2rot,
1603 bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies)
1604
1605 {
1606 Generic6DofSpringConstraint constrain = null;
1607 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
1608 RigidBody body1 = (pBody1 as BulletBodyXNA).rigidBody;
1609 RigidBody body2 = (pBody2 as BulletBodyXNA).rigidBody;
1610 if (body1 != null && body2 != null)
1611 {
1612 IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
1613 IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
1614 IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
1615 frame1._origin = frame1v;
1616
1617 IndexedVector3 frame2v = new IndexedVector3(pframe2.X, pframe2.Y, pframe2.Z);
1618 IndexedQuaternion frame2rot = new IndexedQuaternion(pframe2rot.X, pframe2rot.Y, pframe2rot.Z, pframe2rot.W);
1619 IndexedMatrix frame2 = IndexedMatrix.CreateFromQuaternion(frame2rot);
1620 frame2._origin = frame1v;
1621
1622 constrain = new Generic6DofSpringConstraint(body1, body2, ref frame1, ref frame2, puseLinearReferenceFrameA);
1623 world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
1624
1625 constrain.CalculateTransforms();
1626 }
1627
1628 return new BulletConstraintXNA(constrain);
1629 }
1630
1631 public override BulletConstraint CreateHingeConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
1632 Vector3 ppivotInA, Vector3 ppivotInB, Vector3 paxisInA, Vector3 paxisInB,
1633 bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies)
1250 { 1634 {
1251 HingeConstraint constrain = null; 1635 HingeConstraint constrain = null;
1252 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 1636 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
1253 RigidBody rb1 = ((BulletBodyXNA)pBody1).rigidBody; 1637 RigidBody rb1 = (pBody1 as BulletBodyXNA).rigidBody;
1254 RigidBody rb2 = ((BulletBodyXNA)ppBody2).rigidBody; 1638 RigidBody rb2 = (pBody2 as BulletBodyXNA).rigidBody;
1255 if (rb1 != null && rb2 != null) 1639 if (rb1 != null && rb2 != null)
1256 { 1640 {
1257 IndexedVector3 pivotInA = new IndexedVector3(ppivotInA.X, ppivotInA.Y, ppivotInA.Z); 1641 IndexedVector3 pivotInA = new IndexedVector3(ppivotInA.X, ppivotInA.Y, ppivotInA.Z);
1258 IndexedVector3 pivotInB = new IndexedVector3(ppivotInB.X, ppivotInB.Y, ppivotInB.Z); 1642 IndexedVector3 pivotInB = new IndexedVector3(ppivotInB.X, ppivotInB.Y, ppivotInB.Z);
1259 IndexedVector3 axisInA = new IndexedVector3(paxisInA.X, paxisInA.Y, paxisInA.Z); 1643 IndexedVector3 axisInA = new IndexedVector3(paxisInA.X, paxisInA.Y, paxisInA.Z);
1260 IndexedVector3 axisInB = new IndexedVector3(paxisInB.X, paxisInB.Y, paxisInB.Z); 1644 IndexedVector3 axisInB = new IndexedVector3(paxisInB.X, paxisInB.Y, paxisInB.Z);
1645 constrain = new HingeConstraint(rb1, rb2, ref pivotInA, ref pivotInB, ref axisInA, ref axisInB, puseLinearReferenceFrameA);
1646 world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
1647 }
1648 return new BulletConstraintXNA(constrain);
1649 }
1650
1651 public override BulletConstraint CreateSliderConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
1652 Vector3 pframe1, Quaternion pframe1rot,
1653 Vector3 pframe2, Quaternion pframe2rot,
1654 bool puseLinearReferenceFrameA, bool pdisableCollisionsBetweenLinkedBodies)
1655 {
1656 SliderConstraint constrain = null;
1657 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
1658 RigidBody rb1 = (pBody1 as BulletBodyXNA).rigidBody;
1659 RigidBody rb2 = (pBody2 as BulletBodyXNA).rigidBody;
1660 if (rb1 != null && rb2 != null)
1661 {
1662 IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
1663 IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
1664 IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
1665 frame1._origin = frame1v;
1666
1667 IndexedVector3 frame2v = new IndexedVector3(pframe2.X, pframe2.Y, pframe2.Z);
1668 IndexedQuaternion frame2rot = new IndexedQuaternion(pframe2rot.X, pframe2rot.Y, pframe2rot.Z, pframe2rot.W);
1669 IndexedMatrix frame2 = IndexedMatrix.CreateFromQuaternion(frame2rot);
1670 frame2._origin = frame1v;
1671
1672 constrain = new SliderConstraint(rb1, rb2, ref frame1, ref frame2, puseLinearReferenceFrameA);
1673 world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
1674 }
1675 return new BulletConstraintXNA(constrain);
1676 }
1677
1678 public override BulletConstraint CreateConeTwistConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
1679 Vector3 pframe1, Quaternion pframe1rot,
1680 Vector3 pframe2, Quaternion pframe2rot,
1681 bool pdisableCollisionsBetweenLinkedBodies)
1682 {
1683 ConeTwistConstraint constrain = null;
1684 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
1685 RigidBody rb1 = (pBody1 as BulletBodyXNA).rigidBody;
1686 RigidBody rb2 = (pBody2 as BulletBodyXNA).rigidBody;
1687 if (rb1 != null && rb2 != null)
1688 {
1689 IndexedVector3 frame1v = new IndexedVector3(pframe1.X, pframe1.Y, pframe1.Z);
1690 IndexedQuaternion frame1rot = new IndexedQuaternion(pframe1rot.X, pframe1rot.Y, pframe1rot.Z, pframe1rot.W);
1691 IndexedMatrix frame1 = IndexedMatrix.CreateFromQuaternion(frame1rot);
1692 frame1._origin = frame1v;
1693
1694 IndexedVector3 frame2v = new IndexedVector3(pframe2.X, pframe2.Y, pframe2.Z);
1695 IndexedQuaternion frame2rot = new IndexedQuaternion(pframe2rot.X, pframe2rot.Y, pframe2rot.Z, pframe2rot.W);
1696 IndexedMatrix frame2 = IndexedMatrix.CreateFromQuaternion(frame2rot);
1697 frame2._origin = frame1v;
1698
1699 constrain = new ConeTwistConstraint(rb1, rb2, ref frame1, ref frame2);
1700 world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
1701 }
1702 return new BulletConstraintXNA(constrain);
1703 }
1704
1705 public override BulletConstraint CreateGearConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
1706 Vector3 paxisInA, Vector3 paxisInB,
1707 float pratio, bool pdisableCollisionsBetweenLinkedBodies)
1708 {
1709 Generic6DofConstraint constrain = null;
1710 /* BulletXNA does not have a gear constraint
1711 GearConstraint constrain = null;
1712 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
1713 RigidBody rb1 = (pBody1 as BulletBodyXNA).rigidBody;
1714 RigidBody rb2 = (pBody2 as BulletBodyXNA).rigidBody;
1715 if (rb1 != null && rb2 != null)
1716 {
1717 IndexedVector3 axis1 = new IndexedVector3(paxisInA.X, paxisInA.Y, paxisInA.Z);
1718 IndexedVector3 axis2 = new IndexedVector3(paxisInB.X, paxisInB.Y, paxisInB.Z);
1719 constrain = new GearConstraint(rb1, rb2, ref axis1, ref axis2, pratio);
1720 world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
1721 }
1722 */
1723 return new BulletConstraintXNA(constrain);
1724 }
1725
1726 public override BulletConstraint CreatePoint2PointConstraint(BulletWorld pWorld, BulletBody pBody1, BulletBody pBody2,
1727 Vector3 ppivotInA, Vector3 ppivotInB,
1728 bool pdisableCollisionsBetweenLinkedBodies)
1729 {
1730 Point2PointConstraint constrain = null;
1731 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
1732 RigidBody rb1 = (pBody1 as BulletBodyXNA).rigidBody;
1733 RigidBody rb2 = (pBody2 as BulletBodyXNA).rigidBody;
1734 if (rb1 != null && rb2 != null)
1735 {
1736 IndexedVector3 pivotInA = new IndexedVector3(ppivotInA.X, ppivotInA.Y, ppivotInA.Z);
1737 IndexedVector3 pivotInB = new IndexedVector3(ppivotInB.X, ppivotInB.Y, ppivotInB.Z);
1738 constrain = new Point2PointConstraint(rb1, rb2, ref pivotInA, ref pivotInB);
1261 world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies); 1739 world.AddConstraint(constrain, pdisableCollisionsBetweenLinkedBodies);
1262 } 1740 }
1263 return new BulletConstraintXNA(constrain); 1741 return new BulletConstraintXNA(constrain);
@@ -1265,9 +1743,9 @@ private sealed class BulletConstraintXNA : BulletConstraint
1265 1743
1266 public override BulletShape CreateHullShape(BulletWorld pWorld, int pHullCount, float[] pConvHulls) 1744 public override BulletShape CreateHullShape(BulletWorld pWorld, int pHullCount, float[] pConvHulls)
1267 { 1745 {
1268 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 1746 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
1269 CompoundShape compoundshape = new CompoundShape(false); 1747 CompoundShape compoundshape = new CompoundShape(false);
1270 1748
1271 compoundshape.SetMargin(world.WorldSettings.Params.collisionMargin); 1749 compoundshape.SetMargin(world.WorldSettings.Params.collisionMargin);
1272 int ii = 1; 1750 int ii = 1;
1273 1751
@@ -1283,7 +1761,7 @@ private sealed class BulletConstraintXNA : BulletConstraint
1283 int ender = ((ii + 4) + (vertexCount*3)); 1761 int ender = ((ii + 4) + (vertexCount*3));
1284 for (int iii = ii + 4; iii < ender; iii+=3) 1762 for (int iii = ii + 4; iii < ender; iii+=3)
1285 { 1763 {
1286 1764
1287 virts.Add(new IndexedVector3(pConvHulls[iii], pConvHulls[iii + 1], pConvHulls[iii +2])); 1765 virts.Add(new IndexedVector3(pConvHulls[iii], pConvHulls[iii + 1], pConvHulls[iii +2]));
1288 } 1766 }
1289 ConvexHullShape convexShape = new ConvexHullShape(virts, vertexCount); 1767 ConvexHullShape convexShape = new ConvexHullShape(virts, vertexCount);
@@ -1291,26 +1769,39 @@ private sealed class BulletConstraintXNA : BulletConstraint
1291 compoundshape.AddChildShape(ref childTrans, convexShape); 1769 compoundshape.AddChildShape(ref childTrans, convexShape);
1292 ii += (vertexCount*3 + 4); 1770 ii += (vertexCount*3 + 4);
1293 } 1771 }
1294 1772
1295 return new BulletShapeXNA(compoundshape, BSPhysicsShapeType.SHAPE_HULL); 1773 return new BulletShapeXNA(compoundshape, BSPhysicsShapeType.SHAPE_HULL);
1296 } 1774 }
1297 1775
1298 public override BulletShape BuildHullShapeFromMesh(BulletWorld world, BulletShape meshShape) { /* TODO */ return null; } 1776 public override BulletShape BuildHullShapeFromMesh(BulletWorld world, BulletShape meshShape, HACDParams parms)
1777 {
1778 /* TODO */ return null;
1779 }
1780
1781 public override BulletShape BuildConvexHullShapeFromMesh(BulletWorld world, BulletShape meshShape)
1782 {
1783 /* TODO */ return null;
1784 }
1785
1786 public override BulletShape CreateConvexHullShape(BulletWorld pWorld, int pIndicesCount, int[] indices, int pVerticesCount, float[] verticesAsFloats)
1787 {
1788 /* TODO */ return null;
1789 }
1299 1790
1300 public override BulletShape CreateMeshShape(BulletWorld pWorld, int pIndicesCount, int[] indices, int pVerticesCount, float[] verticesAsFloats) 1791 public override BulletShape CreateMeshShape(BulletWorld pWorld, int pIndicesCount, int[] indices, int pVerticesCount, float[] verticesAsFloats)
1301 { 1792 {
1302 //DumpRaw(indices,verticesAsFloats,pIndicesCount,pVerticesCount); 1793 //DumpRaw(indices,verticesAsFloats,pIndicesCount,pVerticesCount);
1303 1794
1304 for (int iter = 0; iter < pVerticesCount; iter++) 1795 for (int iter = 0; iter < pVerticesCount; iter++)
1305 { 1796 {
1306 if (verticesAsFloats[iter] > 0 && verticesAsFloats[iter] < 0.0001) verticesAsFloats[iter] = 0; 1797 if (verticesAsFloats[iter] > 0 && verticesAsFloats[iter] < 0.0001) verticesAsFloats[iter] = 0;
1307 if (verticesAsFloats[iter] < 0 && verticesAsFloats[iter] > -0.0001) verticesAsFloats[iter] = 0; 1798 if (verticesAsFloats[iter] < 0 && verticesAsFloats[iter] > -0.0001) verticesAsFloats[iter] = 0;
1308 } 1799 }
1309 1800
1310 ObjectArray<int> indicesarr = new ObjectArray<int>(indices); 1801 ObjectArray<int> indicesarr = new ObjectArray<int>(indices);
1311 ObjectArray<float> vertices = new ObjectArray<float>(verticesAsFloats); 1802 ObjectArray<float> vertices = new ObjectArray<float>(verticesAsFloats);
1312 DumpRaw(indicesarr,vertices,pIndicesCount,pVerticesCount); 1803 DumpRaw(indicesarr,vertices,pIndicesCount,pVerticesCount);
1313 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 1804 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
1314 IndexedMesh mesh = new IndexedMesh(); 1805 IndexedMesh mesh = new IndexedMesh();
1315 mesh.m_indexType = PHY_ScalarType.PHY_INTEGER; 1806 mesh.m_indexType = PHY_ScalarType.PHY_INTEGER;
1316 mesh.m_numTriangles = pIndicesCount/3; 1807 mesh.m_numTriangles = pIndicesCount/3;
@@ -1320,7 +1811,7 @@ private sealed class BulletConstraintXNA : BulletConstraint
1320 mesh.m_vertexStride = 3; 1811 mesh.m_vertexStride = 3;
1321 mesh.m_vertexType = PHY_ScalarType.PHY_FLOAT; 1812 mesh.m_vertexType = PHY_ScalarType.PHY_FLOAT;
1322 mesh.m_triangleIndexStride = 3; 1813 mesh.m_triangleIndexStride = 3;
1323 1814
1324 TriangleIndexVertexArray tribuilder = new TriangleIndexVertexArray(); 1815 TriangleIndexVertexArray tribuilder = new TriangleIndexVertexArray();
1325 tribuilder.AddIndexedMesh(mesh, PHY_ScalarType.PHY_INTEGER); 1816 tribuilder.AddIndexedMesh(mesh, PHY_ScalarType.PHY_INTEGER);
1326 BvhTriangleMeshShape meshShape = new BvhTriangleMeshShape(tribuilder, true,true); 1817 BvhTriangleMeshShape meshShape = new BvhTriangleMeshShape(tribuilder, true,true);
@@ -1331,7 +1822,7 @@ private sealed class BulletConstraintXNA : BulletConstraint
1331 } 1822 }
1332 public static void DumpRaw(ObjectArray<int>indices, ObjectArray<float> vertices, int pIndicesCount,int pVerticesCount ) 1823 public static void DumpRaw(ObjectArray<int>indices, ObjectArray<float> vertices, int pIndicesCount,int pVerticesCount )
1333 { 1824 {
1334 1825
1335 String fileName = "objTest3.raw"; 1826 String fileName = "objTest3.raw";
1336 String completePath = System.IO.Path.Combine(Util.configDir(), fileName); 1827 String completePath = System.IO.Path.Combine(Util.configDir(), fileName);
1337 StreamWriter sw = new StreamWriter(completePath); 1828 StreamWriter sw = new StreamWriter(completePath);
@@ -1357,7 +1848,7 @@ private sealed class BulletConstraintXNA : BulletConstraint
1357 string s = vertices[indices[i * 3]].ToString("0.0000"); 1848 string s = vertices[indices[i * 3]].ToString("0.0000");
1358 s += " " + vertices[indices[i * 3 + 1]].ToString("0.0000"); 1849 s += " " + vertices[indices[i * 3 + 1]].ToString("0.0000");
1359 s += " " + vertices[indices[i * 3 + 2]].ToString("0.0000"); 1850 s += " " + vertices[indices[i * 3 + 2]].ToString("0.0000");
1360 1851
1361 sw.Write(s + "\n"); 1852 sw.Write(s + "\n");
1362 } 1853 }
1363 1854
@@ -1379,7 +1870,7 @@ private sealed class BulletConstraintXNA : BulletConstraint
1379 mesh.m_vertexStride = 3; 1870 mesh.m_vertexStride = 3;
1380 mesh.m_vertexType = PHY_ScalarType.PHY_FLOAT; 1871 mesh.m_vertexType = PHY_ScalarType.PHY_FLOAT;
1381 mesh.m_triangleIndexStride = 3; 1872 mesh.m_triangleIndexStride = 3;
1382 1873
1383 TriangleIndexVertexArray tribuilder = new TriangleIndexVertexArray(); 1874 TriangleIndexVertexArray tribuilder = new TriangleIndexVertexArray();
1384 tribuilder.AddIndexedMesh(mesh, PHY_ScalarType.PHY_INTEGER); 1875 tribuilder.AddIndexedMesh(mesh, PHY_ScalarType.PHY_INTEGER);
1385 1876
@@ -1410,7 +1901,7 @@ private sealed class BulletConstraintXNA : BulletConstraint
1410 sw.Close(); 1901 sw.Close();
1411 } 1902 }
1412 1903
1413 public override BulletShape CreateTerrainShape(uint id, Vector3 size, float minHeight, float maxHeight, float[] heightMap, 1904 public override BulletShape CreateTerrainShape(uint id, Vector3 size, float minHeight, float maxHeight, float[] heightMap,
1414 float scaleFactor, float collisionMargin) 1905 float scaleFactor, float collisionMargin)
1415 { 1906 {
1416 const int upAxis = 2; 1907 const int upAxis = 2;
@@ -1426,7 +1917,7 @@ private sealed class BulletConstraintXNA : BulletConstraint
1426 1917
1427 public override bool TranslationalLimitMotor(BulletConstraint pConstraint, float ponOff, float targetVelocity, float maxMotorForce) 1918 public override bool TranslationalLimitMotor(BulletConstraint pConstraint, float ponOff, float targetVelocity, float maxMotorForce)
1428 { 1919 {
1429 TypedConstraint tconstrain = ((BulletConstraintXNA)pConstraint).constrain; 1920 TypedConstraint tconstrain = (pConstraint as BulletConstraintXNA).constrain;
1430 bool onOff = ponOff != 0; 1921 bool onOff = ponOff != 0;
1431 bool ret = false; 1922 bool ret = false;
1432 1923
@@ -1452,64 +1943,73 @@ private sealed class BulletConstraintXNA : BulletConstraint
1452 /* TODO */ 1943 /* TODO */
1453 updatedEntityCount = 0; 1944 updatedEntityCount = 0;
1454 collidersCount = 0; 1945 collidersCount = 0;
1455 return 1; 1946
1947
1948 int ret = PhysicsStep2(world,timeStep,maxSubSteps,fixedTimeStep,out updatedEntityCount,out world.physicsScene.m_updateArray, out collidersCount, out world.physicsScene.m_collisionArray);
1949
1950 return ret;
1456 } 1951 }
1457 1952
1458 private int PhysicsStep2(BulletWorld pWorld, float timeStep, int m_maxSubSteps, float m_fixedTimeStep, 1953 private int PhysicsStep2(BulletWorld pWorld, float timeStep, int m_maxSubSteps, float m_fixedTimeStep,
1459 out int updatedEntityCount, out List<BulletXNA.EntityProperties> updatedEntities, 1954 out int updatedEntityCount, out EntityProperties[] updatedEntities,
1460 out int collidersCount, out List<BulletXNA.CollisionDesc>colliders) 1955 out int collidersCount, out CollisionDesc[] colliders)
1461 { 1956 {
1462 int epic = PhysicsStepint(pWorld, timeStep, m_maxSubSteps, m_fixedTimeStep, out updatedEntityCount, out updatedEntities, 1957 int epic = PhysicsStepint(pWorld, timeStep, m_maxSubSteps, m_fixedTimeStep, out updatedEntityCount, out updatedEntities,
1463 out collidersCount, out colliders); 1958 out collidersCount, out colliders, m_maxCollisions, m_maxUpdatesPerFrame);
1464 return epic; 1959 return epic;
1465 } 1960 }
1466 1961
1467 private static int PhysicsStepint(BulletWorld pWorld,float timeStep, int m_maxSubSteps, float m_fixedTimeStep, out int updatedEntityCount, out List<BulletXNA.EntityProperties> updatedEntities, out int collidersCount, out List<BulletXNA.CollisionDesc> colliders) 1962 private int PhysicsStepint(BulletWorld pWorld,float timeStep, int m_maxSubSteps, float m_fixedTimeStep, out int updatedEntityCount,
1963 out EntityProperties[] updatedEntities, out int collidersCount, out CollisionDesc[] colliders, int maxCollisions, int maxUpdates)
1468 { 1964 {
1469 int numSimSteps = 0; 1965 int numSimSteps = 0;
1470 1966 Array.Clear(UpdatedObjects, 0, UpdatedObjects.Length);
1967 Array.Clear(UpdatedCollisions, 0, UpdatedCollisions.Length);
1968 LastEntityProperty=0;
1969
1970
1471 1971
1472 //if (updatedEntities is null)
1473 // updatedEntities = new List<BulletXNA.EntityProperties>();
1474 1972
1475 //if (colliders is null) 1973
1476 // colliders = new List<BulletXNA.CollisionDesc>(); 1974
1477 1975 LastCollisionDesc=0;
1976
1977 updatedEntityCount = 0;
1978 collidersCount = 0;
1979
1478 1980
1479 if (pWorld is BulletWorldXNA) 1981 if (pWorld is BulletWorldXNA)
1480 { 1982 {
1481 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 1983 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
1482 1984
1985 world.LastCollisionDesc = 0;
1986 world.LastEntityProperty = 0;
1483 numSimSteps = world.StepSimulation(timeStep, m_maxSubSteps, m_fixedTimeStep); 1987 numSimSteps = world.StepSimulation(timeStep, m_maxSubSteps, m_fixedTimeStep);
1484 int updates = 0; 1988 int updates = 0;
1485 1989
1486 updatedEntityCount = world.UpdatedObjects.Count; 1990 PersistentManifold contactManifold;
1487 updatedEntities = new List<BulletXNA.EntityProperties>(world.UpdatedObjects); 1991 CollisionObject objA;
1488 updatedEntityCount = updatedEntities.Count; 1992 CollisionObject objB;
1489 world.UpdatedObjects.Clear(); 1993 ManifoldPoint manifoldPoint;
1490 1994 PairCachingGhostObject pairCachingGhostObject;
1491 1995
1492 collidersCount = world.UpdatedCollisions.Count;
1493 colliders = new List<BulletXNA.CollisionDesc>(world.UpdatedCollisions);
1494
1495 world.UpdatedCollisions.Clear();
1496 m_collisionsThisFrame = 0; 1996 m_collisionsThisFrame = 0;
1497 int numManifolds = world.GetDispatcher().GetNumManifolds(); 1997 int numManifolds = world.GetDispatcher().GetNumManifolds();
1498 for (int j = 0; j < numManifolds; j++) 1998 for (int j = 0; j < numManifolds; j++)
1499 { 1999 {
1500 PersistentManifold contactManifold = world.GetDispatcher().GetManifoldByIndexInternal(j); 2000 contactManifold = world.GetDispatcher().GetManifoldByIndexInternal(j);
1501 int numContacts = contactManifold.GetNumContacts(); 2001 int numContacts = contactManifold.GetNumContacts();
1502 if (numContacts == 0) 2002 if (numContacts == 0)
1503 continue; 2003 continue;
1504 2004
1505 CollisionObject objA = contactManifold.GetBody0() as CollisionObject; 2005 objA = contactManifold.GetBody0() as CollisionObject;
1506 CollisionObject objB = contactManifold.GetBody1() as CollisionObject; 2006 objB = contactManifold.GetBody1() as CollisionObject;
1507 2007
1508 ManifoldPoint manifoldPoint = contactManifold.GetContactPoint(0); 2008 manifoldPoint = contactManifold.GetContactPoint(0);
1509 IndexedVector3 contactPoint = manifoldPoint.GetPositionWorldOnB(); 2009 //IndexedVector3 contactPoint = manifoldPoint.GetPositionWorldOnB();
1510 IndexedVector3 contactNormal = -manifoldPoint.m_normalWorldOnB; // make relative to A 2010 // IndexedVector3 contactNormal = -manifoldPoint.m_normalWorldOnB; // make relative to A
1511 2011
1512 RecordCollision(world, objA, objB, contactPoint, contactNormal); 2012 RecordCollision(this, objA, objB, manifoldPoint.GetPositionWorldOnB(), -manifoldPoint.m_normalWorldOnB, manifoldPoint.GetDistance());
1513 m_collisionsThisFrame ++; 2013 m_collisionsThisFrame ++;
1514 if (m_collisionsThisFrame >= 9999999) 2014 if (m_collisionsThisFrame >= 9999999)
1515 break; 2015 break;
@@ -1517,23 +2017,91 @@ private sealed class BulletConstraintXNA : BulletConstraint
1517 2017
1518 } 2018 }
1519 2019
2020 foreach (GhostObject ghostObject in specialCollisionObjects.Values)
2021 {
2022 pairCachingGhostObject = ghostObject as PairCachingGhostObject;
2023 if (pairCachingGhostObject != null)
2024 {
2025 RecordGhostCollisions(pairCachingGhostObject);
2026 }
2027
2028 }
2029
2030
2031 updatedEntityCount = LastEntityProperty;
2032 updatedEntities = UpdatedObjects;
2033
2034 collidersCount = LastCollisionDesc;
2035 colliders = UpdatedCollisions;
2036
1520 2037
1521 } 2038 }
1522 else 2039 else
1523 { 2040 {
1524 //if (updatedEntities is null) 2041 //if (updatedEntities is null)
1525 updatedEntities = new List<BulletXNA.EntityProperties>(); 2042 //updatedEntities = new List<BulletXNA.EntityProperties>();
1526 updatedEntityCount = 0; 2043 //updatedEntityCount = 0;
1527 //if (colliders is null) 2044
1528 colliders = new List<BulletXNA.CollisionDesc>(); 2045
1529 collidersCount = 0; 2046 //collidersCount = 0;
2047
2048 updatedEntities = new EntityProperties[0];
2049
2050
2051 colliders = new CollisionDesc[0];
2052
1530 } 2053 }
1531 return numSimSteps; 2054 return numSimSteps;
1532 } 2055 }
2056 public void RecordGhostCollisions(PairCachingGhostObject obj)
2057 {
2058 IOverlappingPairCache cache = obj.GetOverlappingPairCache();
2059 ObjectArray<BroadphasePair> pairs = cache.GetOverlappingPairArray();
2060
2061 DiscreteDynamicsWorld world = (PhysicsScene.World as BulletWorldXNA).world;
2062 PersistentManifoldArray manifoldArray = new PersistentManifoldArray();
2063 BroadphasePair collisionPair;
2064 PersistentManifold contactManifold;
2065
2066 CollisionObject objA;
2067 CollisionObject objB;
2068
2069 ManifoldPoint pt;
1533 2070
1534 private static void RecordCollision(CollisionWorld world, CollisionObject objA, CollisionObject objB, IndexedVector3 contact, IndexedVector3 norm) 2071 int numPairs = pairs.Count;
2072
2073 for (int i = 0; i < numPairs; i++)
2074 {
2075 manifoldArray.Clear();
2076 if (LastCollisionDesc < UpdatedCollisions.Length)
2077 break;
2078 collisionPair = world.GetPairCache().FindPair(pairs[i].m_pProxy0, pairs[i].m_pProxy1);
2079 if (collisionPair == null)
2080 continue;
2081
2082 collisionPair.m_algorithm.GetAllContactManifolds(manifoldArray);
2083 for (int j = 0; j < manifoldArray.Count; j++)
2084 {
2085 contactManifold = manifoldArray[j];
2086 int numContacts = contactManifold.GetNumContacts();
2087 objA = contactManifold.GetBody0() as CollisionObject;
2088 objB = contactManifold.GetBody1() as CollisionObject;
2089 for (int p = 0; p < numContacts; p++)
2090 {
2091 pt = contactManifold.GetContactPoint(p);
2092 if (pt.GetDistance() < 0.0f)
2093 {
2094 RecordCollision(this, objA, objB, pt.GetPositionWorldOnA(), -pt.m_normalWorldOnB,pt.GetDistance());
2095 break;
2096 }
2097 }
2098 }
2099 }
2100
2101 }
2102 private static void RecordCollision(BSAPIXNA world, CollisionObject objA, CollisionObject objB, IndexedVector3 contact, IndexedVector3 norm, float penetration)
1535 { 2103 {
1536 2104
1537 IndexedVector3 contactNormal = norm; 2105 IndexedVector3 contactNormal = norm;
1538 if ((objA.GetCollisionFlags() & BulletXNA.BulletCollision.CollisionFlags.BS_WANTS_COLLISIONS) == 0 && 2106 if ((objA.GetCollisionFlags() & BulletXNA.BulletCollision.CollisionFlags.BS_WANTS_COLLISIONS) == 0 &&
1539 (objB.GetCollisionFlags() & BulletXNA.BulletCollision.CollisionFlags.BS_WANTS_COLLISIONS) == 0) 2107 (objB.GetCollisionFlags() & BulletXNA.BulletCollision.CollisionFlags.BS_WANTS_COLLISIONS) == 0)
@@ -1550,31 +2118,34 @@ private sealed class BulletConstraintXNA : BulletConstraint
1550 contactNormal = -contactNormal; 2118 contactNormal = -contactNormal;
1551 } 2119 }
1552 2120
1553 ulong collisionID = ((ulong) idA << 32) | idB; 2121 //ulong collisionID = ((ulong) idA << 32) | idB;
1554 2122
1555 BulletXNA.CollisionDesc cDesc = new BulletXNA.CollisionDesc() 2123 CollisionDesc cDesc = new CollisionDesc()
1556 { 2124 {
1557 aID = idA, 2125 aID = idA,
1558 bID = idB, 2126 bID = idB,
1559 point = contact, 2127 point = new Vector3(contact.X,contact.Y,contact.Z),
1560 normal = contactNormal 2128 normal = new Vector3(contactNormal.X,contactNormal.Y,contactNormal.Z),
2129 penetration = penetration
2130
1561 }; 2131 };
1562 world.UpdatedCollisions.Add(cDesc); 2132 if (world.LastCollisionDesc < world.UpdatedCollisions.Length)
2133 world.UpdatedCollisions[world.LastCollisionDesc++] = (cDesc);
1563 m_collisionsThisFrame++; 2134 m_collisionsThisFrame++;
1564 2135
1565 2136
1566 } 2137 }
1567 private static EntityProperties GetDebugProperties(BulletWorld pWorld, BulletBody pBody) 2138 private static EntityProperties GetDebugProperties(BulletWorld pWorld, BulletBody pCollisionObject)
1568 { 2139 {
1569 EntityProperties ent = new EntityProperties(); 2140 EntityProperties ent = new EntityProperties();
1570 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 2141 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
1571 RigidBody body = ((BulletBodyXNA)pBody).rigidBody; 2142 CollisionObject collisionObject = (pCollisionObject as BulletBodyXNA).rigidBody;
1572 IndexedMatrix transform = body.GetWorldTransform(); 2143 IndexedMatrix transform = collisionObject.GetWorldTransform();
1573 IndexedVector3 LinearVelocity = body.GetInterpolationLinearVelocity(); 2144 IndexedVector3 LinearVelocity = collisionObject.GetInterpolationLinearVelocity();
1574 IndexedVector3 AngularVelocity = body.GetInterpolationAngularVelocity(); 2145 IndexedVector3 AngularVelocity = collisionObject.GetInterpolationAngularVelocity();
1575 IndexedQuaternion rotation = transform.GetRotation(); 2146 IndexedQuaternion rotation = transform.GetRotation();
1576 ent.Acceleration = Vector3.Zero; 2147 ent.Acceleration = Vector3.Zero;
1577 ent.ID = (uint)body.GetUserPointer(); 2148 ent.ID = (uint)collisionObject.GetUserPointer();
1578 ent.Position = new Vector3(transform._origin.X,transform._origin.Y,transform._origin.Z); 2149 ent.Position = new Vector3(transform._origin.X,transform._origin.Y,transform._origin.Z);
1579 ent.Rotation = new Quaternion(rotation.X,rotation.Y,rotation.Z,rotation.W); 2150 ent.Rotation = new Quaternion(rotation.X,rotation.Y,rotation.Z,rotation.W);
1580 ent.Velocity = new Vector3(LinearVelocity.X, LinearVelocity.Y, LinearVelocity.Z); 2151 ent.Velocity = new Vector3(LinearVelocity.X, LinearVelocity.Y, LinearVelocity.Z);
@@ -1582,28 +2153,29 @@ private sealed class BulletConstraintXNA : BulletConstraint
1582 return ent; 2153 return ent;
1583 } 2154 }
1584 2155
1585 public override bool UpdateParameter(BulletWorld world, uint localID, String parm, float value) { /* TODO */ return false; } 2156 public override bool UpdateParameter(BulletWorld world, uint localID, String parm, float value) { /* TODO */
2157 return false; }
1586 2158
1587 public override Vector3 GetLocalScaling(BulletShape pShape) 2159 public override Vector3 GetLocalScaling(BulletShape pShape)
1588 { 2160 {
1589 CollisionShape shape = ((BulletShapeXNA)pShape).shape; 2161 CollisionShape shape = (pShape as BulletShapeXNA).shape;
1590 IndexedVector3 scale = shape.GetLocalScaling(); 2162 IndexedVector3 scale = shape.GetLocalScaling();
1591 return new Vector3(scale.X,scale.Y,scale.Z); 2163 return new Vector3(scale.X,scale.Y,scale.Z);
1592 } 2164 }
1593 2165
1594 public bool RayCastGround(BulletWorld pWorld, Vector3 _RayOrigin, float pRayHeight, BulletBody NotMe) 2166 public bool RayCastGround(BulletWorld pWorld, Vector3 _RayOrigin, float pRayHeight, BulletBody NotMe)
1595 { 2167 {
1596 DiscreteDynamicsWorld world = ((BulletWorldXNA)pWorld).world; 2168 DiscreteDynamicsWorld world = (pWorld as BulletWorldXNA).world;
1597 if (world != null) 2169 if (world != null)
1598 { 2170 {
1599 if (NotMe is BulletBodyXNA && NotMe.HasPhysicalBody) 2171 if (NotMe is BulletBodyXNA && NotMe.HasPhysicalBody)
1600 { 2172 {
1601 CollisionObject AvoidBody = ((BulletBodyXNA)NotMe).body; 2173 CollisionObject AvoidBody = (NotMe as BulletBodyXNA).body;
1602 2174
1603 IndexedVector3 rOrigin = new IndexedVector3(_RayOrigin.X, _RayOrigin.Y, _RayOrigin.Z); 2175 IndexedVector3 rOrigin = new IndexedVector3(_RayOrigin.X, _RayOrigin.Y, _RayOrigin.Z);
1604 IndexedVector3 rEnd = new IndexedVector3(_RayOrigin.X, _RayOrigin.Y, _RayOrigin.Z - pRayHeight); 2176 IndexedVector3 rEnd = new IndexedVector3(_RayOrigin.X, _RayOrigin.Y, _RayOrigin.Z - pRayHeight);
1605 using ( 2177 using (
1606 ClosestNotMeRayResultCallback rayCallback = 2178 ClosestNotMeRayResultCallback rayCallback =
1607 new ClosestNotMeRayResultCallback(rOrigin, rEnd, AvoidBody) 2179 new ClosestNotMeRayResultCallback(rOrigin, rEnd, AvoidBody)
1608 ) 2180 )
1609 { 2181 {
@@ -1619,4 +2191,130 @@ private sealed class BulletConstraintXNA : BulletConstraint
1619 return false; 2191 return false;
1620 } 2192 }
1621} 2193}
2194
2195
2196
2197
2198 public class SimMotionState : DefaultMotionState
2199 {
2200 public RigidBody Rigidbody;
2201 public Vector3 ZeroVect;
2202
2203 private IndexedMatrix m_xform;
2204
2205 private EntityProperties m_properties;
2206 private EntityProperties m_lastProperties;
2207 private BSAPIXNA m_world;
2208
2209 const float POSITION_TOLERANCE = 0.05f;
2210 const float VELOCITY_TOLERANCE = 0.001f;
2211 const float ROTATION_TOLERANCE = 0.01f;
2212 const float ANGULARVELOCITY_TOLERANCE = 0.01f;
2213
2214 public SimMotionState(BSAPIXNA pWorld, uint id, IndexedMatrix starTransform, object frameUpdates)
2215 {
2216 IndexedQuaternion OrientationQuaterion = starTransform.GetRotation();
2217 m_properties = new EntityProperties()
2218 {
2219 ID = id,
2220 Position = new Vector3(starTransform._origin.X, starTransform._origin.Y,starTransform._origin.Z),
2221 Rotation = new Quaternion(OrientationQuaterion.X,OrientationQuaterion.Y,OrientationQuaterion.Z,OrientationQuaterion.W)
2222 };
2223 m_lastProperties = new EntityProperties()
2224 {
2225 ID = id,
2226 Position = new Vector3(starTransform._origin.X, starTransform._origin.Y, starTransform._origin.Z),
2227 Rotation = new Quaternion(OrientationQuaterion.X, OrientationQuaterion.Y, OrientationQuaterion.Z, OrientationQuaterion.W)
2228 };
2229 m_world = pWorld;
2230 m_xform = starTransform;
2231 }
2232
2233 public override void GetWorldTransform(out IndexedMatrix worldTrans)
2234 {
2235 worldTrans = m_xform;
2236 }
2237
2238 public override void SetWorldTransform(IndexedMatrix worldTrans)
2239 {
2240 SetWorldTransform(ref worldTrans);
2241 }
2242
2243 public override void SetWorldTransform(ref IndexedMatrix worldTrans)
2244 {
2245 SetWorldTransform(ref worldTrans, false);
2246 }
2247 public void SetWorldTransform(ref IndexedMatrix worldTrans, bool force)
2248 {
2249 m_xform = worldTrans;
2250 // Put the new transform into m_properties
2251 IndexedQuaternion OrientationQuaternion = m_xform.GetRotation();
2252 IndexedVector3 LinearVelocityVector = Rigidbody.GetLinearVelocity();
2253 IndexedVector3 AngularVelocityVector = Rigidbody.GetAngularVelocity();
2254 m_properties.Position = new Vector3(m_xform._origin.X, m_xform._origin.Y, m_xform._origin.Z);
2255 m_properties.Rotation = new Quaternion(OrientationQuaternion.X, OrientationQuaternion.Y,
2256 OrientationQuaternion.Z, OrientationQuaternion.W);
2257 // A problem with stock Bullet is that we don't get an event when an object is deactivated.
2258 // This means that the last non-zero values for linear and angular velocity
2259 // are left in the viewer who does dead reconning and the objects look like
2260 // they float off.
2261 // BulletSim ships with a patch to Bullet which creates such an event.
2262 m_properties.Velocity = new Vector3(LinearVelocityVector.X, LinearVelocityVector.Y, LinearVelocityVector.Z);
2263 m_properties.RotationalVelocity = new Vector3(AngularVelocityVector.X, AngularVelocityVector.Y, AngularVelocityVector.Z);
2264
2265 if (force
2266
2267 || !AlmostEqual(ref m_lastProperties.Position, ref m_properties.Position, POSITION_TOLERANCE)
2268 || !AlmostEqual(ref m_properties.Rotation, ref m_lastProperties.Rotation, ROTATION_TOLERANCE)
2269 // If the Velocity and AngularVelocity are zero, most likely the object has
2270 // been deactivated. If they both are zero and they have become zero recently,
2271 // make sure a property update is sent so the zeros make it to the viewer.
2272 || ((m_properties.Velocity == ZeroVect && m_properties.RotationalVelocity == ZeroVect)
2273 &&
2274 (m_properties.Velocity != m_lastProperties.Velocity ||
2275 m_properties.RotationalVelocity != m_lastProperties.RotationalVelocity))
2276 // If Velocity and AngularVelocity are non-zero but have changed, send an update.
2277 || !AlmostEqual(ref m_properties.Velocity, ref m_lastProperties.Velocity, VELOCITY_TOLERANCE)
2278 ||
2279 !AlmostEqual(ref m_properties.RotationalVelocity, ref m_lastProperties.RotationalVelocity,
2280 ANGULARVELOCITY_TOLERANCE)
2281 )
2282
2283
2284 {
2285 // Add this update to the list of updates for this frame.
2286 m_lastProperties = m_properties;
2287 if (m_world.LastEntityProperty < m_world.UpdatedObjects.Length)
2288 m_world.UpdatedObjects[m_world.LastEntityProperty++]=(m_properties);
2289
2290 //(*m_updatesThisFrame)[m_properties.ID] = &m_properties;
2291 }
2292
2293
2294
2295
2296 }
2297 public override void SetRigidBody(RigidBody body)
2298 {
2299 Rigidbody = body;
2300 }
2301 internal static bool AlmostEqual(ref Vector3 v1, ref Vector3 v2, float nEpsilon)
2302 {
2303 return
2304 (((v1.X - nEpsilon) < v2.X) && (v2.X < (v1.X + nEpsilon))) &&
2305 (((v1.Y - nEpsilon) < v2.Y) && (v2.Y < (v1.Y + nEpsilon))) &&
2306 (((v1.Z - nEpsilon) < v2.Z) && (v2.Z < (v1.Z + nEpsilon)));
2307 }
2308
2309 internal static bool AlmostEqual(ref Quaternion v1, ref Quaternion v2, float nEpsilon)
2310 {
2311 return
2312 (((v1.X - nEpsilon) < v2.X) && (v2.X < (v1.X + nEpsilon))) &&
2313 (((v1.Y - nEpsilon) < v2.Y) && (v2.Y < (v1.Y + nEpsilon))) &&
2314 (((v1.Z - nEpsilon) < v2.Z) && (v2.Z < (v1.Z + nEpsilon))) &&
2315 (((v1.W - nEpsilon) < v2.W) && (v2.W < (v1.W + nEpsilon)));
2316 }
2317
2318 }
1622} 2319}
2320