aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ModifiedBulletX/ModifiedBulletX/Dynamics/DiscreteDynamicsWorld.cs
blob: 3702c2a27e47e27da5ebe0047409404532930bd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
/*
  Bullet for XNA Copyright (c) 2003-2007 Vsevolod Klementjev http://www.codeplex.com/xnadevru
  Bullet original C++ version Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/

using System;
using System.Collections.Generic;
using System.Text;
using MonoXnaCompactMaths;

namespace XnaDevRu.BulletX.Dynamics
{
	/// <summary>
	/// DiscreteDynamicsWorld provides discrete rigid body simulation
	/// those classes replace the obsolete CcdPhysicsEnvironment/CcdPhysicsController
	/// </summary>
	public class DiscreteDynamicsWorld : DynamicsWorld
	{
		private static bool _reportMe = true;

		private IConstraintSolver _constraintSolver;
		private SimulationIslandManager _islandManager;
		private List<TypedConstraint> _constraints = new List<TypedConstraint>();
		private IDebugDraw _debugDrawer;
		private ContactSolverInfo _solverInfo = new ContactSolverInfo();
		private Vector3 _gravity;
		//for variable timesteps
		private float _localTime;
		//for variable timesteps
		private bool _ownsIslandManager;
		private bool _ownsConstraintSolver;
		private List<RaycastVehicle> _vehicles = new List<RaycastVehicle>();
		private int _profileTimings;

		public DiscreteDynamicsWorld(IDispatcher dispatcher, OverlappingPairCache pairCache)
			: this(dispatcher, pairCache, null) { }

		//this btDiscreteDynamicsWorld constructor gets created objects from the user, and will not delete those
		public DiscreteDynamicsWorld(IDispatcher dispatcher, OverlappingPairCache pairCache, IConstraintSolver constraintSolver)
			: base(dispatcher, pairCache)
		{
			_constraintSolver = constraintSolver != null ? constraintSolver : new SequentialImpulseConstraintSolver();
			_debugDrawer = null;
			_gravity = new Vector3(0, -10, 0);
			_localTime = 1f / 60f;
			_profileTimings = 0;
			_islandManager = new SimulationIslandManager();
			_ownsIslandManager = true;
			_ownsConstraintSolver = constraintSolver == null;
		}

		public ContactSolverInfo SolverInfo { get { return _solverInfo; } }
		public SimulationIslandManager SimulationIslandManager { get { return _islandManager; } }
		public CollisionWorld CollisionWorld { get { return this; } }

		public override IDebugDraw DebugDrawer
		{
			get
			{
				return _debugDrawer;
			}
			set
			{
				_debugDrawer = value;
			}
		}

		public override Vector3 Gravity
		{
			set
			{
				_gravity = value;
				for (int i = 0; i < CollisionObjects.Count; i++)
				{
					CollisionObject colObj = CollisionObjects[i];
					RigidBody body = RigidBody.Upcast(colObj);
					if (body != null)
					{
						body.Gravity = value;
					}
				}
			}
		}

		public override IConstraintSolver ConstraintSolver
		{
			set
			{
				_ownsConstraintSolver = false;
				_constraintSolver = value;
			}
		}

		public override int ConstraintsCount
		{
			get
			{
				return _constraints.Count;
			}
		}

		//if maxSubSteps > 0, it will interpolate motion between fixedTimeStep's
		public override void StepSimulation(float timeStep, int maxSubSteps, float fixedTimeStep)
		{
			int numSimulationSubSteps = 0;

			if (maxSubSteps != 0)
			{
				//fixed timestep with interpolation
				_localTime += timeStep;
				if (_localTime >= fixedTimeStep)
				{
					numSimulationSubSteps = (int)(_localTime / fixedTimeStep);
					_localTime -= numSimulationSubSteps * fixedTimeStep;
				}
			}
			else
			{
				//variable timestep
				fixedTimeStep = timeStep;
				_localTime = timeStep;
				if (Math.Abs(timeStep) < float.Epsilon)
				{
					numSimulationSubSteps = 0;
					maxSubSteps = 0;
				}
				else
				{
					numSimulationSubSteps = 1;
					maxSubSteps = 1;
				}
			}

			//process some debugging flags
			if (DebugDrawer != null)
			{
				RigidBody.DisableDeactivation = (DebugDrawer.DebugMode & DebugDrawModes.NoDeactivation) != 0;
			}
			if (numSimulationSubSteps != 0)
			{

				SaveKinematicState(fixedTimeStep);

				//clamp the number of substeps, to prevent simulation grinding spiralling down to a halt
				int clampedSimulationSteps = (numSimulationSubSteps > maxSubSteps) ? maxSubSteps : numSimulationSubSteps;

				for (int i = 0; i < clampedSimulationSteps; i++)
				{
					InternalSingleStepSimulation(fixedTimeStep);
					SynchronizeMotionStates();
				}

			}

			SynchronizeMotionStates();

			//return numSimulationSubSteps;
		}

		public void StepSimulation(float timeStep, int maxSubSteps)
		{
			StepSimulation(timeStep, maxSubSteps, 1f / 60f);
		}

		public override void UpdateAabbs()
		{
			Vector3 colorvec = new Vector3(1, 0, 0);
			for (int i = 0; i < CollisionObjects.Count; i++)
			{
				CollisionObject colObj = CollisionObjects[i];
				RigidBody body = RigidBody.Upcast(colObj);

				if (body != null)
				{
					//	if (body->IsActive() && (!body->IsStatic()))
					{
						Vector3 minAabb, maxAabb;
						colObj.CollisionShape.GetAabb(colObj.WorldTransform, out minAabb, out maxAabb);
						OverlappingPairCache bp = BroadphasePairCache;

						//moving objects should be moderately sized, probably something wrong if not
						if (colObj.IsStaticObject || ((maxAabb - minAabb).LengthSquared() < 1e12f))
						{
							bp.SetAabb(body.Broadphase, minAabb, maxAabb);
						}
						else
						{
							//something went wrong, investigate
							//this assert is unwanted in 3D modelers (danger of loosing work)
							BulletDebug.Assert(false);
							body.ActivationState = ActivationState.DisableSimulation;

							if (_reportMe)
							{
								_reportMe = false;
								Console.WriteLine("Overflow in AABB, object removed from simulation \n");
								Console.WriteLine("If you can reproduce this, please email bugs@continuousphysics.com\n");
								Console.WriteLine("Please include above information, your Platform, version of OS.\n");
								Console.WriteLine("Thanks.\n");
							}
						}
						if (_debugDrawer != null && (_debugDrawer.DebugMode & DebugDrawModes.DrawAabb) != 0)
							DrawAabb(_debugDrawer, minAabb, maxAabb, colorvec);
					}
				}
			}
		}

		public override void AddConstraint(TypedConstraint constraint)
		{
			_constraints.Add(constraint);
		}

		public override void RemoveConstraint(TypedConstraint constraint)
		{
			_constraints.Remove(constraint);
		}

		public void AddVehicle(RaycastVehicle vehicle)
		{
			_vehicles.Add(vehicle);
		}

		public void RemoveVehicle(RaycastVehicle vehicle)
		{
			_vehicles.Remove(vehicle);
		}

		public override void AddRigidBody(RigidBody body)
		{
			if (!body.IsStaticOrKinematicObject)
			{
				body.Gravity = _gravity;
			}

			if (body.CollisionShape != null)
			{
				bool isDynamic = !(body.IsStaticObject || body.IsKinematicObject);
				BroadphaseProxy.CollisionFilterGroups collisionFilterGroup = isDynamic ? BroadphaseProxy.CollisionFilterGroups.Default : BroadphaseProxy.CollisionFilterGroups.Static;
				BroadphaseProxy.CollisionFilterGroups collisionFilterMask = isDynamic ? BroadphaseProxy.CollisionFilterGroups.All : (BroadphaseProxy.CollisionFilterGroups.All ^ BroadphaseProxy.CollisionFilterGroups.Static);

				AddCollisionObject(body, collisionFilterGroup, collisionFilterMask);
			}
		}

		public override void RemoveRigidBody(RigidBody body)
		{
			RemoveCollisionObject(body);
		}

		public void DebugDrawObject(Matrix worldTransform, CollisionShape shape, Vector3 color)
		{
			if (shape.ShapeType == BroadphaseNativeTypes.Compound)
			{
				CompoundShape compoundShape = shape as CompoundShape;
				for (int i = compoundShape.ChildShapeCount - 1; i >= 0; i--)
				{
					Matrix childTrans = compoundShape.GetChildTransform(i);
					CollisionShape colShape = compoundShape.GetChildShape(i);
					DebugDrawObject(worldTransform * childTrans, colShape, color);
				}

			}
			else
			{
				switch (shape.ShapeType)
				{

					case BroadphaseNativeTypes.Sphere:
						{
							SphereShape sphereShape = shape as SphereShape;
							float radius = sphereShape.Margin;//radius doesn't include the margin, so draw with margin
							Vector3 start = worldTransform.Translation;
							DebugDrawer.DrawLine(start, start + Vector3.TransformNormal(new Vector3(radius, 0, 0), worldTransform), color);
							DebugDrawer.DrawLine(start, start + Vector3.TransformNormal(new Vector3(0, radius, 0), worldTransform), color);
							DebugDrawer.DrawLine(start, start + Vector3.TransformNormal(new Vector3(0, 0, radius), worldTransform), color);
							//drawSphere					
							break;
						}
					case BroadphaseNativeTypes.MultiSphere:
					case BroadphaseNativeTypes.Cone:
						{
							ConeShape coneShape = shape as ConeShape;
							float radius = coneShape.Radius;//+coneShape->getMargin();
							float height = coneShape.Height;//+coneShape->getMargin();
							Vector3 start = worldTransform.Translation;
							DebugDrawer.DrawLine(start + Vector3.TransformNormal(new Vector3(0f, 0f, 0.5f * height), worldTransform), start + Vector3.TransformNormal(new Vector3(radius, 0f, -0.5f * height), worldTransform), color);
							DebugDrawer.DrawLine(start + Vector3.TransformNormal(new Vector3(0f, 0f, 0.5f * height), worldTransform), start + Vector3.TransformNormal(new Vector3(-radius, 0f, -0.5f * height), worldTransform), color);
							DebugDrawer.DrawLine(start + Vector3.TransformNormal(new Vector3(0f, 0f, 0.5f * height), worldTransform), start + Vector3.TransformNormal(new Vector3(0f, radius, -0.5f * height), worldTransform), color);
							DebugDrawer.DrawLine(start + Vector3.TransformNormal(new Vector3(0f, 0f, 0.5f * height), worldTransform), start + Vector3.TransformNormal(new Vector3(0f, -radius, -0.5f * height), worldTransform), color);
							break;
						}
					case BroadphaseNativeTypes.Cylinder:
						{
							CylinderShape cylinder = shape as CylinderShape;
							int upAxis = cylinder.UpAxis;
							float radius = cylinder.Radius;
							float halfHeight = MathHelper.GetElement(cylinder.HalfExtents, upAxis);
							Vector3 start = worldTransform.Translation;
							Vector3 offsetHeight = new Vector3();
							MathHelper.SetElement(ref offsetHeight, upAxis, halfHeight);
							Vector3 offsetRadius = new Vector3();
							MathHelper.SetElement(ref offsetRadius, (upAxis + 1) % 3,  radius);
							DebugDrawer.DrawLine(start + Vector3.TransformNormal(offsetHeight + offsetRadius, worldTransform), start + Vector3.TransformNormal(-offsetHeight + offsetRadius, worldTransform), color);
							DebugDrawer.DrawLine(start + Vector3.TransformNormal(offsetHeight - offsetRadius, worldTransform), start + Vector3.TransformNormal(-offsetHeight - offsetRadius, worldTransform), color);
							break;
						} 
					default:
						{
							if (shape.ShapeType == BroadphaseNativeTypes.TriangleMesh)
							{
								TriangleMeshShape concaveMesh = shape as TriangleMeshShape;
								//btVector3 aabbMax(1e30f,1e30f,1e30f);
								//btVector3 aabbMax(100,100,100);//1e30f,1e30f,1e30f);

								//todo pass camera, for some culling
								Vector3 aabbMax = new Vector3(1e30f, 1e30f, 1e30f);
								Vector3 aabbMin = new Vector3(-1e30f, -1e30f, -1e30f);

								DebugDrawCallback drawCallback = new DebugDrawCallback(DebugDrawer, worldTransform, color);
								concaveMesh.ProcessAllTriangles(drawCallback, aabbMin, aabbMax);
							}

							if (shape.ShapeType == BroadphaseNativeTypes.ConvexTriangleMesh)
							{
								ConvexTriangleMeshShape convexMesh = shape as ConvexTriangleMeshShape;
								//todo: pass camera for some culling			
								Vector3 aabbMax = new Vector3(1e30f, 1e30f, 1e30f);
								Vector3 aabbMin = new Vector3(-1e30f, -1e30f, -1e30f);
								//DebugDrawcallback drawCallback;
								DebugDrawCallback drawCallback = new DebugDrawCallback(DebugDrawer, worldTransform, color);
								convexMesh.getStridingMesh().InternalProcessAllTriangles(drawCallback, aabbMin, aabbMax);
							}

							// for polyhedral shapes
							if (shape.IsPolyhedral)
							{
								PolyhedralConvexShape polyshape = shape as PolyhedralConvexShape;

								for (int i = 0; i < polyshape.EdgeCount; i++)
								{
									Vector3 a, b;
									polyshape.GetEdge(i, out a, out b);
									a = Vector3.TransformNormal(a, worldTransform);
									b = Vector3.TransformNormal(b, worldTransform);
									DebugDrawer.DrawLine(a, b, color);
								}
							}
							break;
						}
				}
			}
		}

		public override TypedConstraint GetConstraint(int index)
		{
			return _constraints[index];
		}

		public static void DrawAabb(IDebugDraw debugDrawer, Vector3 from, Vector3 to, Vector3 color)
		{
			Vector3 halfExtents = (to - from) * 0.5f;
			Vector3 center = (to + from) * 0.5f;

			Vector3 edgecoord = new Vector3(1f, 1f, 1f), pa, pb;
			for (int i = 0; i < 4; i++)
			{
				for (int j = 0; j < 3; j++)
				{
					pa = new Vector3(edgecoord.X * halfExtents.X, edgecoord.Y * halfExtents.Y,
						edgecoord.Z * halfExtents.Z);
					pa += center;

					int othercoord = j % 3;
					MathHelper.SetElement(ref edgecoord, othercoord, MathHelper.GetElement(edgecoord, othercoord) * -1f);
					pb = new Vector3(edgecoord.X * halfExtents.X, edgecoord.Y * halfExtents.Y,
						edgecoord.Z * halfExtents.Z);
					pb += center;

					debugDrawer.DrawLine(pa, pb, color);
				}
				edgecoord = new Vector3(-1f, -1f, -1f);
				if (i < 3)
					MathHelper.SetElement(ref edgecoord, i, MathHelper.GetElement(edgecoord, i) * -1f);
			}
		}

		protected void PredictUnconstraintMotion(float timeStep)
		{
			for (int i = 0; i < CollisionObjects.Count; i++)
			{
				CollisionObject colObj = CollisionObjects[i];
				RigidBody body = RigidBody.Upcast(colObj);
				if (body != null)
				{
					if (!body.IsStaticOrKinematicObject)
					{
						if (body.IsActive)
						{
							body.ApplyForces(timeStep);
							body.IntegrateVelocities(timeStep);
							Matrix temp = body.InterpolationWorldTransform;
							body.PredictIntegratedTransform(timeStep, ref temp);
							body.InterpolationWorldTransform = temp;
						}
					}
				}
			}
		}

		protected void IntegrateTransforms(float timeStep)
		{
			Matrix predictedTrans = new Matrix();
			for (int i = 0; i < CollisionObjects.Count; i++)
			{
				CollisionObject colObj = CollisionObjects[i];
				RigidBody body = RigidBody.Upcast(colObj);
				if (body != null)
				{
					if (body.IsActive && (!body.IsStaticOrKinematicObject))
					{
						body.PredictIntegratedTransform(timeStep, ref predictedTrans);
						body.ProceedToTransform(predictedTrans);
					}
				}
			}
		}

		protected void CalculateSimulationIslands()
		{
			SimulationIslandManager.UpdateActivationState(this, Dispatcher);

			for (int i = 0; i < _constraints.Count; i++)
			{
				TypedConstraint constraint = _constraints[i];

				RigidBody colObj0 = constraint.RigidBodyA;
				RigidBody colObj1 = constraint.RigidBodyB;

				if (((colObj0 != null) && (colObj0.MergesSimulationIslands)) &&
					((colObj1 != null) && (colObj1.MergesSimulationIslands)))
				{
					if (colObj0.IsActive || colObj1.IsActive)
					{

						SimulationIslandManager.UnionFind.Unite((colObj0).IslandTag,
							(colObj1).IslandTag);
					}
				}
			}

			//Store the island id in each body
			SimulationIslandManager.StoreIslandActivationState(this);
		}

		//protected void SolveNonContactConstraints(ContactSolverInfo solverInfo)
		//{
		//    //constraint preparation: building jacobians
		//    for (int i = 0; i < _constraints.Count; i++)
		//    {
		//        TypedConstraint constraint = _constraints[i];
		//        constraint.BuildJacobian();
		//    }

		//    //solve the regular non-contact constraints (point 2 point, hinge, generic d6)
		//    for (int g = 0; g < solverInfo.IterationsCount; g++)
		//    {
		//        for (int i = 0; i < _constraints.Count; i++)
		//        {
		//            TypedConstraint constraint = _constraints[i];
		//            constraint.SolveConstraint(solverInfo.TimeStep);
		//        }
		//    }
		//}

		//protected void SolveContactConstraints(ContactSolverInfo solverInfo)
		//{
		//    InplaceSolverIslandCallback solverCallback = new InplaceSolverIslandCallback(solverInfo, _constraintSolver, _debugDrawer);

		//    // solve all the contact points and contact friction
		//    _islandManager.BuildAndProcessIslands(Dispatcher, CollisionObjects, solverCallback);
		//}

		protected void SolveConstraints(ContactSolverInfo solverInfo)
		{
			//sorted version of all btTypedConstraint, based on islandId
			List<TypedConstraint> sortedConstraints = new List<TypedConstraint>(ConstraintsCount);

			for (int i = 0; i < ConstraintsCount; i++)
			{
				sortedConstraints.Add(_constraints[i]);
			}

			sortedConstraints.Sort(new Comparison<TypedConstraint>(TypedConstraint.SortConstraintOnIslandPredicate));
			List<TypedConstraint> constraintsPtr = ConstraintsCount != 0 ? sortedConstraints : new List<TypedConstraint>();

			InplaceSolverIslandCallback solverCallback = new InplaceSolverIslandCallback(solverInfo, _constraintSolver, constraintsPtr,  _debugDrawer);

			// solve all the constraints for this island
			_islandManager.BuildAndProcessIslands(CollisionWorld.Dispatcher, CollisionWorld.CollisionObjects, solverCallback);
		}

		protected void UpdateActivationState(float timeStep)
		{
			for (int i = 0; i < CollisionObjects.Count; i++)
			{
				CollisionObject colObj = CollisionObjects[i];
				RigidBody body = RigidBody.Upcast(colObj);
				if (body != null)
				{
					body.UpdateDeactivation(timeStep);

					if (body.WantsSleeping())
					{
						if (body.IsStaticOrKinematicObject)
						{
							body.ActivationState = ActivationState.IslandSleeping;
						}
						else
						{
							if (body.ActivationState == ActivationState.Active)
								body.ActivationState = ActivationState.WantsDeactivation;
						}
					}
					else
					{
						if (body.ActivationState != ActivationState.DisableDeactivation)
							body.ActivationState = ActivationState.Active;
					}
				}
			}
		}

		protected void UpdateVehicles(float timeStep)
		{
			for (int i = 0; i < _vehicles.Count; i++)
			{
				RaycastVehicle vehicle = _vehicles[i];
				vehicle.updateVehicle(timeStep);
			}
		}

		protected void StartProfiling(float timeStep) { }

		protected virtual void InternalSingleStepSimulation(float timeStep)
		{
			StartProfiling(timeStep);

			//update aabbs information
			UpdateAabbs();

			//apply gravity, predict motion
			PredictUnconstraintMotion(timeStep);

			DispatcherInfo dispatchInfo = DispatchInfo;
			dispatchInfo.TimeStep = timeStep;
			dispatchInfo.StepCount = 0;
			dispatchInfo.DebugDraw = DebugDrawer;

			//perform collision detection
			PerformDiscreteCollisionDetection();

			CalculateSimulationIslands();

			SolverInfo.TimeStep = timeStep;

			//solve contact and other joint constraints
			SolveConstraints(SolverInfo);

			//CallbackTriggers();

			//integrate transforms
			IntegrateTransforms(timeStep);

			//update vehicle simulation
			UpdateVehicles(timeStep);

			UpdateActivationState(timeStep);
		}

		protected void SynchronizeMotionStates()
		{
			//debug vehicle wheels
			{
				//todo: iterate over awake simulation islands!
				for (int i = 0; i < CollisionObjects.Count; i++)
				{
					CollisionObject colObj = CollisionObjects[i];
					if (DebugDrawer != null && (DebugDrawer.DebugMode & DebugDrawModes.DrawWireframe) != 0)
					{
						Vector3 color = new Vector3(255f, 255f, 255f);
						switch (colObj.ActivationState)
						{
							case ActivationState.Active:
								color = new Vector3(255f, 255f, 255f); break;
							case ActivationState.IslandSleeping:
								color = new Vector3(0f, 255f, 0f); break;
							case ActivationState.WantsDeactivation:
								color = new Vector3(0f, 255f, 255f); break;
							case ActivationState.DisableDeactivation:
								color = new Vector3(255f, 0f, 0f); break;
							case ActivationState.DisableSimulation:
								color = new Vector3(255f, 255f, 0f); break;
							default:
								color = new Vector3(255f, 0f, 0f); break;
						}

						DebugDrawObject(colObj.WorldTransform, colObj.CollisionShape, color);
					}
					RigidBody body = RigidBody.Upcast(colObj);
					if (body != null && body.MotionState != null && !body.IsStaticOrKinematicObject)
					{
						//if (body.ActivationState != ActivationState.IslandSleeping)
						{
							Matrix interpolatedTransform = new Matrix();
							TransformUtil.IntegrateTransform(body.InterpolationWorldTransform,
								body.InterpolationLinearVelocity, body.InterpolationAngularVelocity, _localTime, ref interpolatedTransform);
							body.MotionState.SetWorldTransform(interpolatedTransform);
						}
					}
				}
			}

			if (DebugDrawer != null && (DebugDrawer.DebugMode & DebugDrawModes.DrawWireframe) != 0)
			{
				for (int i = 0; i < _vehicles.Count; i++)
				{
					for (int v = 0; v < _vehicles[i].getNumWheels(); v++)
					{
						Vector3 wheelColor = new Vector3(0, 255, 255);
						if (_vehicles[i].getWheelInfo(v).RaycastInfo.IsInContact)
						{
							wheelColor = new Vector3(0, 0, 255);
						}
						else
						{
							wheelColor = new Vector3(255, 0, 255);
						}

						//synchronize the wheels with the (interpolated) chassis worldtransform
						_vehicles[i].updateWheelTransform(v, true);

						Vector3 wheelPosWS = _vehicles[i].getWheelInfo(v).WorldTransform.Translation;

						Vector3 axle = new Vector3(
							MathHelper.GetElement(_vehicles[i].getWheelInfo(v).WorldTransform, 0, _vehicles[i].getRightAxis()),
							MathHelper.GetElement(_vehicles[i].getWheelInfo(v).WorldTransform, 1, _vehicles[i].getRightAxis()),
							MathHelper.GetElement(_vehicles[i].getWheelInfo(v).WorldTransform, 2, _vehicles[i].getRightAxis()));


						//m_vehicles[i]->getWheelInfo(v).m_raycastInfo.m_wheelAxleWS
						//debug wheels (cylinders)
						_debugDrawer.DrawLine(wheelPosWS, wheelPosWS + axle, wheelColor);
						_debugDrawer.DrawLine(wheelPosWS, _vehicles[i].getWheelInfo(v).RaycastInfo.ContactPointWS, wheelColor);
					}
				}
			}
		}

		protected void SaveKinematicState(float timeStep)
		{
			for (int i = 0; i < CollisionObjects.Count; i++)
			{
				CollisionObject colObj = CollisionObjects[i];
				RigidBody body = RigidBody.Upcast(colObj);
				if (body != null)
				{
					if (body.ActivationState != ActivationState.IslandSleeping)
					{
						if (body.IsKinematicObject)
						{
							//to calculate velocities next frame
							body.SaveKinematicState(timeStep);
						}
					}
				}
			}
		}

		internal class InplaceSolverIslandCallback : SimulationIslandManager.IIslandCallback
		{
			private ContactSolverInfo _solverInfo;
			private IConstraintSolver _solver;
			private IDebugDraw _debugDrawer;
			private List<TypedConstraint> _sortedConstraints;

			public InplaceSolverIslandCallback(
				ContactSolverInfo solverInfo,
				IConstraintSolver solver,
				List<TypedConstraint> sortedConstraints,
				IDebugDraw debugDrawer)
			{
				_solverInfo = solverInfo;
				_solver = solver;
				_sortedConstraints = sortedConstraints;
				_debugDrawer = debugDrawer;
			}

			public ContactSolverInfo SolverInfo { get { return _solverInfo; } set { _solverInfo = value; } }
			public IConstraintSolver Solver { get { return _solver; } set { _solver = value; } }
			public List<TypedConstraint> Constraints { get { return _sortedConstraints; } set { _sortedConstraints = value; } }
			public IDebugDraw DebugDrawer { get { return _debugDrawer; } set { _debugDrawer = value; } }

			public void ProcessIsland(List<CollisionObject> bodies, List<PersistentManifold> manifolds, int numManifolds, int islandID)
			{
				//also add all non-contact constraints/joints for this island
				List<TypedConstraint> startConstraint = new List<TypedConstraint>();
				int numCurConstraints = 0;
				int startIndex = 0;
				int i;

				//find the first constraint for this island
				for (i = 0; i < _sortedConstraints.Count; i++)
				{
					if (TypedConstraint.GetConstraintIslandId(_sortedConstraints[i]) == islandID)
					{
						//startConstraint = &m_sortedConstraints[i];
						startIndex = i;
						break;
					}
				}
				//count the number of constraints in this island
				for (; i < _sortedConstraints.Count; i++)
				{
					if (TypedConstraint.GetConstraintIslandId(_sortedConstraints[i]) == islandID)
					{
						numCurConstraints++;
					}
				}

				for (i = startIndex; i < startIndex + numCurConstraints; i++)
				{
					startConstraint.Add(_sortedConstraints[i]);
				}

				_solver.SolveGroup(bodies, manifolds, numManifolds, startConstraint, _solverInfo, _debugDrawer);
			}
		}
	}

	internal class DebugDrawCallback : ITriangleIndexCallback, ITriangleCallback
	{
		private IDebugDraw _debugDrawer;
		private Vector3 _color;
		private Matrix _worldTrans;

		public DebugDrawCallback(IDebugDraw debugDrawer, Matrix worldTrans, Vector3 color)
		{
			_debugDrawer = debugDrawer;
			_worldTrans = worldTrans;
			_color = color;
		}

		public void ProcessTriangleIndex(Vector3[] triangle, int partId, int triangleIndex)
		{
			ProcessTriangle(triangle, partId, triangleIndex);
		}

		#region ITriangleCallback Members

		public void ProcessTriangle(Vector3[] triangle, int partID, int triangleIndex)
		{
			Vector3 wv0, wv1, wv2;
			wv0 = Vector3.TransformNormal(triangle[0], _worldTrans);
			wv1 = Vector3.TransformNormal(triangle[1], _worldTrans);
			wv2 = Vector3.TransformNormal(triangle[2], _worldTrans);
			_debugDrawer.DrawLine(wv0, wv1, _color);
			_debugDrawer.DrawLine(wv1, wv2, _color);
			_debugDrawer.DrawLine(wv2, wv0, _color);
		}

		#endregion
	}
}