public override void Run() { var conf = new DefaultCollisionConfiguration(); var dispatcher = new CollisionDispatcher(conf); var broadphase = new AxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000)); world = new DiscreteDynamicsWorld(dispatcher, broadphase, null, conf); var indexVertexArray = new TriangleIndexVertexArray(TorusMesh.Indices, TorusMesh.Vertices); foreach (var indexedMesh in indexVertexArray.IndexedMeshArray) { indexedMesh.ToString(); } AddToDisposeQueue(indexVertexArray); var gImpactMesh = new GImpactMeshShape(indexVertexArray); Vector3 aabbMin, aabbMax; gImpactMesh.GetAabb(Matrix.Identity, out aabbMin, out aabbMax); CreateBody(1.0f, gImpactMesh, Vector3.Zero); AddToDisposeQueue(gImpactMesh); gImpactMesh = null; var triangleMesh = new BvhTriangleMeshShape(indexVertexArray, true); triangleMesh.CalculateLocalInertia(1.0f); triangleMesh.GetAabb(Matrix.Identity, out aabbMin, out aabbMax); CreateBody(1.0f, triangleMesh, Vector3.Zero); AddToDisposeQueue(triangleMesh); triangleMesh = null; indexVertexArray = null; AddToDisposeQueue(conf); AddToDisposeQueue(dispatcher); AddToDisposeQueue(broadphase); AddToDisposeQueue(world); //conf.Dispose(); conf = null; //dispatcher.Dispose(); dispatcher = null; //broadphase.Dispose(); broadphase = null; for (int i = 0; i < 600; i++) { world.StepSimulation(1.0f / 60.0f); } world.Dispose(); world = null; ForceGC(); TestWeakRefs(); ClearRefs(); }
public static void TestGCCollection() { var conf = new DefaultCollisionConfiguration(); var dispatcher = new CollisionDispatcher(conf); var broadphase = new DbvtBroadphase(); var world = new DiscreteDynamicsWorld(dispatcher, broadphase, null, conf); world.Gravity = new Vector3(0, -10, 0); var conf_wr = new WeakReference(conf); var dispatcher_wr = new WeakReference(dispatcher); var broadphase_wr = new WeakReference(broadphase); var world_wr = new WeakReference(broadphase); //conf.Dispose(); conf = null; dispatcher.OnDisposing += onDisposing; dispatcher.OnDisposed += onDisposed; //dispatcher.Dispose(); dispatcher = null; broadphase.OnDisposing += onDisposing; broadphase.OnDisposed += onDisposed; //broadphase.Dispose(); broadphase = null; world.OnDisposing += onDisposing; world.OnDisposed += onDisposed; world.SetInternalTickCallback(WorldPreTickCallback); world.StepSimulation(1.0f/60.0f); //world.SetInternalTickCallback(null); //world.Dispose(); world = null; GC.Collect(); GC.WaitForPendingFinalizers(); TestWeakRef("CollisionConfiguration", conf_wr); TestWeakRef("CollisionDispatcher", dispatcher_wr); TestWeakRef("DbvtBroadphase", broadphase_wr); TestWeakRef("DiscreteDynamicsWorld", world_wr); }
static void TestAxisSweepOverlapCallback() { var conf = new DefaultCollisionConfiguration(); var dispatcher = new CollisionDispatcher(conf); var broadphase = new AxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000)); world = new DiscreteDynamicsWorld(dispatcher, broadphase, null, conf); //broadphase.OverlappingPairUserCallback = new AxisSweepUserCallback(); //AddToDisposeQueue(broadphase.OverlappingPairUserCallback); CreateBody(10.0f, new SphereShape(1.0f), new Vector3(2, 2, 0)); CreateBody(1.0f, new SphereShape(1.0f), new Vector3(0, 2, 0)); CustomBroadphaseAabbCallback aabbCallback = new CustomBroadphaseAabbCallback(); broadphase.AabbTest(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000), aabbCallback); AddToDisposeQueue(aabbCallback); aabbCallback = null; // FIXME: RayTest crashes for DbvtBroadphase CustomBroadphaseRayTestCallback rayCallback = new CustomBroadphaseRayTestCallback(); //broadphase.RayTest(new Vector3(0, 2, 0), new Vector3(2, 2, 0), rayCallback); AddToDisposeQueue(rayCallback); rayCallback = null; broadphase = null; world.StepSimulation(1.0f / 60.0f); world.Dispose(); world = null; GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); TestWeakRefs(); disposeQueue.Clear(); }
static void TestGCCollection() { var conf = new DefaultCollisionConfiguration(); var dispatcher = new CollisionDispatcher(conf); var broadphase = new DbvtBroadphase(); //var broadphase = new AxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000)); world = new DiscreteDynamicsWorld(dispatcher, broadphase, null, conf); world.Gravity = new Vector3(0, -10, 0); dispatcher.NearCallback = DispatcherNearCallback; CreateBody(0.0f, new BoxShape(50, 1, 50), Vector3.Zero); var dynamicObject = CreateBody(10.0f, new SphereShape(1.0f), new Vector3(2, 2, 0)); var dynamicObject2 = CreateBody(1.0f, new SphereShape(1.0f), new Vector3(0, 2, 0)); var ghostPairCallback = new GhostPairCallback(); broadphase.OverlappingPairCache.SetInternalGhostPairCallback(ghostPairCallback); AddToDisposeQueue(ghostPairCallback); ghostPairCallback = null; var ghostObject = new PairCachingGhostObject(); ghostObject.CollisionShape = new BoxShape(2); ghostObject.WorldTransform = Matrix.Translation(2,2,0); world.AddCollisionObject(ghostObject); var trimesh = new TriangleMesh(); Vector3 v0 = new Vector3(0, 0, 0); Vector3 v1 = new Vector3(1, 0, 0); Vector3 v2 = new Vector3(0, 1, 0); Vector3 v3 = new Vector3(1, 1, 0); trimesh.AddTriangle(v0, v1, v2); trimesh.AddTriangle(v1, v3, v2); var triangleMeshShape = new BvhTriangleMeshShape(trimesh, false); var triMeshObject = CreateBody(0, triangleMeshShape, new Vector3(20,0,20)); AddToDisposeQueue(triangleMeshShape); AddToDisposeQueue(trimesh); AddToDisposeQueue(triMeshObject); triangleMeshShape = null; trimesh = null; AddToDisposeQueue(conf); AddToDisposeQueue(dispatcher); AddToDisposeQueue(broadphase); AddToDisposeQueue(world); //conf.Dispose(); conf = null; //dispatcher.Dispose(); dispatcher = null; //broadphase.Dispose(); broadphase = null; world.DebugDrawer = new DebugDrawTest(); AddToDisposeQueue(world.DebugDrawer); world.SetInternalTickCallback(WorldPreTickCallback); for (int i = 0; i < 600; i++) { world.StepSimulation(1.0f / 60.0f); } world.DispatchInfo.DebugDraw = new DebugDrawTest2(); AddToDisposeQueue(world.DispatchInfo.DebugDraw); world.DispatchInfo.DebugDraw = world.DispatchInfo.DebugDraw; AddToDisposeQueue(world.DispatchInfo.DebugDraw); world.DispatchInfo.DebugDraw = null; world.DebugDrawer = null; world.DebugDrawer = new DebugDrawTest2(); world.StepSimulation(1.0f / 60.0f); world.DebugDrawWorld(); AddToDisposeQueue(world.DispatchInfo.DebugDraw); world.DebugDrawer = new DebugDrawTest(); world.DebugDrawWorld(); AddToDisposeQueue(world.DebugDrawer); world.DebugDrawer = null; TestContactTest(dynamicObject, dynamicObject2); TestGhostObjectPairs(ghostObject); TestRayCast(dynamicObject); TestTriangleMeshRayCast(triMeshObject); dynamicObject = null; dynamicObject2 = null; triMeshObject = null; //world.SetInternalTickCallback(null); world.Dispose(); world = null; GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); TestWeakRefs(); disposeQueue.Clear(); }
void Start() { //Create a World Debug.Log("Initialize physics"); List<CollisionShape> CollisionShapes = new List<CollisionShape>(); DefaultCollisionConfiguration CollisionConf = new DefaultCollisionConfiguration(); CollisionDispatcher Dispatcher = new CollisionDispatcher(CollisionConf); DbvtBroadphase Broadphase = new DbvtBroadphase(); DiscreteDynamicsWorld World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConf); World.Gravity = new BulletSharp.Math.Vector3(0, -10, 0); // create a few dynamic rigidbodies const float mass = 1.0f; //Add a single cube RigidBody fallRigidBody; BoxShape shape = new BoxShape(1f, 1f, 1f); BulletSharp.Math.Vector3 localInertia = BulletSharp.Math.Vector3.Zero; shape.CalculateLocalInertia(mass, out localInertia); RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, null, shape, localInertia); fallRigidBody = new RigidBody(rbInfo); rbInfo.Dispose(); Matrix st = Matrix.Translation(new BulletSharp.Math.Vector3(0f, 10f, 0f)); fallRigidBody.WorldTransform = st; World.AddRigidBody(fallRigidBody); //Step the simulation 300 steps for (int i = 0; i < 300; i++) { World.StepSimulation(1f / 60f, 10); Matrix trans; fallRigidBody.GetWorldTransform(out trans); Debug.Log("box height: " + trans.Origin); } //Clean up. World.RemoveRigidBody(fallRigidBody); fallRigidBody.Dispose(); UnityEngine.Debug.Log("ExitPhysics"); if (World != null) { //remove/dispose constraints int i; for (i = World.NumConstraints - 1; i >= 0; i--) { TypedConstraint constraint = World.GetConstraint(i); World.RemoveConstraint(constraint); constraint.Dispose(); } //remove the rigidbodies from the dynamics world and delete them for (i = World.NumCollisionObjects - 1; i >= 0; i--) { CollisionObject obj = World.CollisionObjectArray[i]; RigidBody body = obj as RigidBody; if (body != null && body.MotionState != null) { body.MotionState.Dispose(); } World.RemoveCollisionObject(obj); obj.Dispose(); } //delete collision shapes foreach (CollisionShape ss in CollisionShapes) ss.Dispose(); CollisionShapes.Clear(); World.Dispose(); Broadphase.Dispose(); Dispatcher.Dispose(); CollisionConf.Dispose(); } if (Broadphase != null) { Broadphase.Dispose(); } if (Dispatcher != null) { Dispatcher.Dispose(); } if (CollisionConf != null) { CollisionConf.Dispose(); } }
//IMPORTANT Time.fixedTime must match the timestep being used here. public static List<UnityEngine.Vector3> SimulateBall(BRigidBody ballRb, UnityEngine.Vector3 ballThrowForce, int numberOfSimulationSteps, bool reverseOrder) { var ballPositions = new List<UnityEngine.Vector3>(numberOfSimulationSteps); //Create a World Debug.Log("Initialize physics"); CollisionConfiguration CollisionConf; CollisionDispatcher Dispatcher; BroadphaseInterface Broadphase; CollisionWorld cw; SequentialImpulseConstraintSolver Solver; BulletSharp.SoftBody.SoftBodyWorldInfo softBodyWorldInfo; //This should create a copy of the BPhysicsWorld with the same settings BPhysicsWorld bw = BPhysicsWorld.Get(); bw.CreatePhysicsWorld(out cw, out CollisionConf, out Dispatcher, out Broadphase, out Solver, out softBodyWorldInfo); World = (DiscreteDynamicsWorld) cw; //Copy all existing rigidbodies in scene // IMPORTANT rigidbodies must be added to the offline world in the same order that they are in the source world // this is because collisions must be resolved in the same order for the sim to be deterministic DiscreteDynamicsWorld sourceWorld = (DiscreteDynamicsWorld) bw.world; BulletSharp.RigidBody bulletBallRb = null; BulletSharp.Math.Matrix mm = BulletSharp.Math.Matrix.Identity; for(int i = 0; i < sourceWorld.NumCollisionObjects; i++) { CollisionObject co = sourceWorld.CollisionObjectArray[i]; if (co != null && co.UserObject is BRigidBody) { BRigidBody rb = (BRigidBody) co.UserObject; float mass = rb.isDynamic() ? rb.mass : 0f; BCollisionShape existingShape = rb.GetComponent<BCollisionShape>(); CollisionShape shape = null; if (existingShape is BSphereShape) { shape = ((BSphereShape)existingShape).CopyCollisionShape(); } else if (existingShape is BBoxShape) { shape = ((BBoxShape)existingShape).CopyCollisionShape(); } RigidBody bulletRB = null; BulletSharp.Math.Vector3 localInertia = new BulletSharp.Math.Vector3(); rb.CreateOrConfigureRigidBody(ref bulletRB, ref localInertia, shape, null); BulletSharp.Math.Vector3 pos = rb.GetCollisionObject().WorldTransform.Origin; BulletSharp.Math.Quaternion rot = rb.GetCollisionObject().WorldTransform.GetOrientation(); BulletSharp.Math.Matrix.AffineTransformation(1f, ref rot, ref pos, out mm); bulletRB.WorldTransform = mm; World.AddRigidBody(bulletRB, rb.groupsIBelongTo, rb.collisionMask); if (rb == ballRb) { bulletBallRb = bulletRB; bulletRB.ApplyCentralImpulse(ballThrowForce.ToBullet()); } } } //Step the simulation numberOfSimulationSteps times for (int i = 0; i < numberOfSimulationSteps; i++) { int numSteps = World.StepSimulation(1f / 60f, 10, 1f / 60f); ballPositions.Add(bulletBallRb.WorldTransform.Origin.ToUnity()); } UnityEngine.Debug.Log("ExitPhysics"); if (World != null) { //remove/dispose constraints int i; for (i = World.NumConstraints - 1; i >= 0; i--) { TypedConstraint constraint = World.GetConstraint(i); World.RemoveConstraint(constraint); constraint.Dispose(); } //remove the rigidbodies from the dynamics world and delete them for (i = World.NumCollisionObjects - 1; i >= 0; i--) { CollisionObject obj = World.CollisionObjectArray[i]; RigidBody body = obj as RigidBody; if (body != null && body.MotionState != null) { body.MotionState.Dispose(); } World.RemoveCollisionObject(obj); obj.Dispose(); } World.Dispose(); Broadphase.Dispose(); Dispatcher.Dispose(); CollisionConf.Dispose(); } if (Broadphase != null) { Broadphase.Dispose(); } if (Dispatcher != null) { Dispatcher.Dispose(); } if (CollisionConf != null) { CollisionConf.Dispose(); } return ballPositions; }
void TestAxisSweepOverlapCallback() { var conf = new DefaultCollisionConfiguration(); var dispatcher = new CollisionDispatcher(conf); var broadphase = new AxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000)); world = new DiscreteDynamicsWorld(dispatcher, broadphase, null, conf); broadphase.OverlappingPairUserCallback = new AxisSweepUserCallback(); AddToDisposeQueue(broadphase.OverlappingPairUserCallback); CreateBody(10.0f, new SphereShape(1.0f), new Vector3(2, 2, 0)); CreateBody(1.0f, new SphereShape(1.0f), new Vector3(0, 2, 0)); CustomBroadphaseAabbCallback aabbCallback = new CustomBroadphaseAabbCallback(); broadphase.AabbTest(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000), aabbCallback); AddToDisposeQueue(aabbCallback); aabbCallback = null; var rayFromWorld = new Vector3(-2, 2, 0); var rayToWorld = new Vector3(4, 2, 0); CustomBroadphaseRayTestCallback rayCallback = new CustomBroadphaseRayTestCallback(ref rayFromWorld, ref rayToWorld); broadphase.RayTest(rayFromWorld, rayToWorld, rayCallback, Vector3.Zero, Vector3.Zero); if (!rayCallback.HasHit) { Console.WriteLine("Broadphase ray test FAILED!"); } AddToDisposeQueue(rayCallback); rayCallback = null; broadphase = null; world.StepSimulation(1.0f / 60.0f); world.Dispose(); world = null; ForceGC(); TestWeakRefs(); ClearRefs(); }