Esempio n. 1
0
        public void Dispose()
        {
            #region Dispose Rigid bodies
            Rock1Body.Dispose();
            Rock2Body.Dispose();
            Rock3Body.Dispose();
            Rock4Body.Dispose();
            Rock5Body.Dispose();
            BandicootRigidBody.Dispose();
            DynamicPlatform1Body.Dispose();
            DynamicPlatform2Body.Dispose();
            DynamicPlatform3Body.Dispose();
            #endregion

            #region Dispose Meshes
            Rock1Mesh.Dispose();
            Rock2Mesh.Dispose();
            Rock3Mesh.Dispose();
            Rock4Mesh.Dispose();
            Rock5Mesh.Dispose();
            DynamicPlatform1Mesh.Dispose();
            DynamicPlatform2Mesh.Dispose();
            DynamicPlatform3Mesh.Dispose();
            #endregion

            #region Dispose Physics world
            dynamicsWorld.Dispose();
            dispatcher.Dispose();
            collisionConfiguration.Dispose();
            constraintSolver.Dispose();
            overlappingPairCache.Dispose();
            #endregion
        }
Esempio n. 2
0
 public void TearDown()
 {
     world.Dispose();
     dispatcher.Dispose();
     broadphase.Dispose();
     conf.Dispose();
 }
Esempio n. 3
0
        public void Dispose()
        {
            if (disposed)
            {
                return;
            }
            disposed = true;
            //Delete all RigidBody objects
            for (int i = btWorld.NumCollisionObjects - 1; i >= 0; i--)
            {
                CollisionObject obj  = btWorld.CollisionObjectArray[i];
                RigidBody       body = obj as RigidBody;
                if (body != null && body.MotionState != null)
                {
                    body.MotionState.Dispose();
                }
                btWorld.RemoveCollisionObject(obj);
                obj.Dispose();
            }

            btWorld.Dispose();
            broadphase.Dispose();
            btDispatcher.Dispose();
            collisionConf.Dispose();
        }
 public void TearDown()
 {
     _world.Dispose();
     _solver.Dispose();
     _broadphase.Dispose();
     _dispatcher.Dispose();
     _conf.Dispose();
 }
Esempio n. 5
0
 public void Dispose()
 {
     dynamicsWorld.Dispose();
     dispatcher.Dispose();
     collisionConfiguration.Dispose();
     constraintSolver.Dispose();
     overlappingPairCache.Dispose();
 }
Esempio n. 6
0
        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 void Dispose()
 {
     //Se hace dispose del modelo fisico.
     dynamicsWorld.Dispose();
     dispatcher.Dispose();
     collisionConfiguration.Dispose();
     constraintSolver.Dispose();
     overlappingPairCache.Dispose();
     piso.Dispose();
 }
Esempio n. 8
0
 /// <summary>
 /// リソースを開放する
 /// </summary>
 public void Dispose()
 {
     if (isDisposed)
     {
         return;
     }
     constraintFactory.Dispose();
     rigidBodyFactory.Dispose();
     dynamicsWorld.Dispose();
     dynamicsWorldFactory.Dispose();
     isDisposed = true;
 }
Esempio n. 9
0
        public void Dispose()
        {
            #region dispose
            dynamicsWorld.Dispose();
            dispatcher.Dispose();
            collisionConfiguration.Dispose();
            constraintSolver.Dispose();
            overlappingPairCache.Dispose();

            bulletObjects.ForEach(b => b.Dispose());
            //floorMesh.Dispose();
            #endregion
        }
        public void TearDown()
        {
            _world.Dispose();
            _dispatcher.Dispose();
            _broadphase.Dispose();
            _conf.Dispose();

            _callback.Dispose();

            _shape.Dispose();
            _sphere1.Dispose();
            _sphere2.Dispose();
        }
        public void Dispose()
        {
            foreach (TGCBox stair in stairsMesh)
            {
                stair.Dispose();
            }

            sphereMesh.Dispose();
            dynamicsWorld.Dispose();
            dispatcher.Dispose();
            collisionConfiguration.Dispose();
            constraintSolver.Dispose();
            overlappingPairCache.Dispose();
        }
Esempio n. 12
0
        public void Dispose()
        {
            dynamicsWorld.Dispose();
            dispatcher.Dispose();
            collisionConfiguration.Dispose();
            constraintSolver.Dispose();
            overlappingPairCache.Dispose();
            boxBody.Dispose();
            ballBody.Dispose();
            floorBody.Dispose();

            boxMesh.Dispose();
            sphereMesh.Dispose();

            floorMesh.Dispose();
        }
Esempio n. 13
0
        public void TearDown()
        {
            world.RemoveRigidBody(body1);
            world.RemoveRigidBody(body2);
            boxShape.Dispose();
            body1.MotionState.Dispose();
            body2.MotionState.Dispose();
            body1.Dispose();
            body2.Dispose();
            ghostObject.Dispose();

            world.Dispose();
            dispatcher.Dispose();
            broadphase.Dispose();
            conf.Dispose();
        }
        //Dispose of components and clean up
        public override void Dispose()
        {
            if (_disposing)
            {
                return;
            }

            //Unregister all registered rigidbodies
            foreach (var rb in _bulletRigidBodies)
            {
                _dynamicsWorld.RemoveRigidBody(rb);
            }

            //Find all constraints and tell them to dispose
            var constraints = (BulletConstraint[])Object.FindObjectsOfType(typeof(BulletConstraint));

            if (constraints.Length == 0)
            {
                return;
            }
            foreach (var tc in constraints)
            {
                tc.Dispose();
            }

            //Find all Bullet rigidbody instances and tell them to dispose
            var rigidBodies = (BulletRigidBody[])Object.FindObjectsOfType(typeof(BulletRigidBody));

            if (rigidBodies.Length == 0)
            {
                return;
            }
            foreach (var rb in rigidBodies)
            {
                rb.Dispose();
            }

            //Dispose of the physics world components in reverse order
            _disposing = true;
            _dynamicsWorld.Dispose();
            _constraintSolver?.Dispose();
            _broadphaseInterface.Dispose();
            _collisionDispatcher.Dispose();
            _collisionConfig.Dispose();
            _bulletBehaviors.Clear();
            _threadedSolver?.Dispose();
        }
Esempio n. 15
0
        public void TearDown()
        {
            world.RemoveRigidBody(compound);
            compound.MotionState.Dispose();
            ground.Dispose();
            groundShape.Dispose();
            compound.Dispose();
            boxShape.Dispose();
            boxShape2.Dispose();
            boxShape3.Dispose();
            compoundShape.Dispose();
            compoundShape2.Dispose();

            world.Dispose();
            dispatcher.Dispose();
            broadphase.Dispose();
            conf.Dispose();
        }
Esempio n. 16
0
        public void TearDown()
        {
            world.RemoveRigidBody(gImpactMesh);
            world.RemoveRigidBody(triangleMesh);
            gImpactMesh.MotionState.Dispose();
            triangleMesh.MotionState.Dispose();
            gImpactMesh.Dispose();
            triangleMesh.Dispose();
            gImpactMeshShape.Dispose();
            triangleMeshShape.Dispose();
            indexVertexArray.Dispose();
            indexVertexArray2.Dispose();

            world.Dispose();
            dispatcher.Dispose();
            broadphase.Dispose();
            conf.Dispose();
        }
Esempio n. 17
0
        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();
        }
Esempio n. 18
0
        public void Destroy()
        {
            this.dynamicsWorld.DeleteAndDisposeAllConstraints();

            this.dynamicsWorld.DeleteAndDisposeAllRigidBodies();

            dynamicsWorld.Dispose();
            solver.Dispose();
            overlappingPairCache.Dispose();
            dispatcher.Dispose();
            collisionConfiguration.Dispose();

            this.bodyContainer.Clear();
            this.constraintContainer.Clear();



            this.created = false;
        }
Esempio n. 19
0
        public void TearDown()
        {
            world.RemoveRigidBody(body1);
            world.RemoveRigidBody(body2);
            body1.CollisionShape.Dispose();
            body2.CollisionShape.Dispose();
            body1.MotionState.Dispose();
            body2.MotionState.Dispose();
            body1.Dispose();
            body2.Dispose();

            broadphase.OverlappingPairUserCallback.Dispose();
            broadphase.OverlappingPairUserCallback = null;

            world.Dispose();
            dispatcher.Dispose();
            broadphase.Dispose();
            conf.Dispose();
        }
Esempio n. 20
0
        public void Dispose()
        {
            //Se hace dispose del modelo fisico.
            dynamicsWorld.Dispose();
            dispatcher.Dispose();
            collisionConfiguration.Dispose();
            constraintSolver.Dispose();
            overlappingPairCache.Dispose();
            hummerBody.Dispose();
            floorBody.Dispose();

            //Dispose de Meshes
            foreach (TgcMesh mesh in meshes)
            {
                mesh.Dispose();
            }

            hummer.Dispose();
        }
        public override void Dispose()
        {
            dynamicsWorld.Dispose();
            foreach (Jugador jugador in jugadores)
            {
                jugador.Dispose();
            }

            pelota.Dispose();

            escena.DisposeAll();

            foreach (Turbo turbo in turbos)
            {
                turbo.Dispose();
            }

            sonido.closeFile();
        }
Esempio n. 22
0
        public void Dispose()
        {
            //Dispose de los meshes.
            sphereMesh.Dispose();
            personaje.Dispose();
            boxMesh.Dispose();
            boxMeshB.Dispose();
            plataforma.Dispose();
            foreach (RigidBody peldanio in escalonesRigidBodies)
            {
                escalon.Dispose();
            }

            //Se hace dispose del modelo fisico.
            dynamicsWorld.Dispose();
            dispatcher.Dispose();
            collisionConfiguration.Dispose();
            constraintSolver.Dispose();
            overlappingPairCache.Dispose();
        }
Esempio n. 23
0
        public void Dispose(bool disposing)
        {
            if (_isDisposed)
            {
                return;
            }

            if (disposing)
            {
                // Dispose managed resources.
            }

            // All bullet rosources are unmanaged.
            if (World != null)
            {
                World.Dispose();
                World = null;
            }
            if (Broadphase != null)
            {
                Broadphase.Dispose();
                Broadphase = null;
            }
            if (Dispatcher != null)
            {
                Dispatcher.Dispose();
                Dispatcher = null;
            }
            if (CollisionConf != null)
            {
                CollisionConf.Dispose();
                CollisionConf = null;
            }
            if (Solver != null)
            {
                Solver.Dispose();
                Solver = null;
            }

            _isDisposed = true;
        }
Esempio n. 24
0
        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();
        }
Esempio n. 25
0
        public void ExitPhysics()
        {
            //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 shape in collisionShapes)
            {
                shape.Dispose();
            }
            collisionShapes.Clear();

            World.Dispose();
            broadphase.Dispose();
            if (dispatcher != null)
            {
                dispatcher.Dispose();
            }
            collisionConf.Dispose();
        }
Esempio n. 26
0
        public void Drop()
        {
            if (simThread != null)
            {
                simThread.Join();
            }

            for (int i = bulletWorld.NumConstraints - 1; i >= 0; i--)
            {
                TypedConstraint c = bulletWorld.GetConstraint(i);
                bulletWorld.RemoveConstraint(c);
                c.Dispose();
            }

            for (int i = bulletWorld.NumCollisionObjects - 1; i >= 0; i--)
            {
                CollisionObject o = bulletWorld.CollisionObjectArray[i];

                RigidBody b = o as RigidBody;
                if (b != null && b.MotionState != null)
                {
                    b.MotionState.Dispose();
                }

                bulletWorld.RemoveCollisionObject(o);
                o.Dispose();
            }

            foreach (CollisionShape s in bulletShapes)
            {
                s.Dispose();
            }
            bulletShapes.Clear();

            bulletWorld.Dispose();
            bulletBroadphase.Dispose();
            bulletCollisionDispatcher.Dispose();
            bulletCollisionConfiguration.Dispose();
        }
Esempio n. 27
0
        /*This Funcion is called at:
         * public static void Main()
         * {
         *     var app = new BulletTest();
         *      app.Run();
         *      _physic.World.Dispose();
         * }
         * definetly the wrong place!!!!!!!!
         * TODO: call it at the right place
         */
        public void Dispose()
        {
            if (BtWorld != null)
            {
                /* for (int d = 0; d < BtWorld.Dispatcher.NumManifolds; d++)
                 * {
                 *  var m = BtWorld.Dispatcher.GetManifoldByIndexInternal(d);
                 *  BtWorld.Dispatcher.ReleaseManifold(m);
                 *  ;
                 * }*/


                //remove/dispose constraints
                int i;
                for (i = BtWorld.NumConstraints - 1; i >= 0; i--)
                {
                    TypedConstraint constraint = BtWorld.GetConstraint(i);
                    BtWorld.RemoveConstraint(constraint);
                    constraint.Dispose();
                }


                //remove the rigidbodies from the dynamics world and delete them
                for (i = BtWorld.NumCollisionObjects - 1; i >= 0; i--)
                {
                    CollisionObject obj  = BtWorld.CollisionObjectArray[i];
                    RigidBody       body = obj as RigidBody;
                    if (body != null && body.MotionState != null)
                    {
                        body.MotionState.Dispose();
                    }
                    BtWorld.RemoveCollisionObject(obj);
                    obj.Dispose();
                }

                //delete collision shapes
                foreach (CollisionShape shape in BtCollisionShapes)
                {
                    shape.Dispose();
                }
                BtCollisionShapes.Clear();



                BtWorld.Dispose();
                BtBroadphase.Dispose();
                BtDispatcher.Dispose();
                BtCollisionConf.Dispose();
            }

            if (BtBroadphase != null)
            {
                BtBroadphase.Dispose();
            }
            if (BtDispatcher != null)
            {
                BtDispatcher.Dispose();
            }
            if (BtCollisionConf != null)
            {
                BtCollisionConf.Dispose();
            }
        }
Esempio n. 28
0
        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();
        }
Esempio n. 29
0
    private 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)
        {
            Gravity = new BulletSharp.Math.Vector3(0, -9.80665f, 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);

        Matrix trans;

        //Step the simulation 300 steps
        for (int i = 0; i < 300; i++)
        {
            World.StepSimulation(1f / 60f, 10);

            fallRigidBody.GetWorldTransform(out trans);

            Debug.Log("box height: " + trans.Origin);
        }

        //Clean up.
        World.RemoveRigidBody(fallRigidBody);
        fallRigidBody.Dispose();

        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();
        }
    }
Esempio n. 30
0
    //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;
        ConstraintSolver       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);
    }