Exemple #1
0
        public void Dispose()
        {
            for (int i = this._DynamicsWorld.NumCollisionObjects - 1; i >= 0; --i)
            {
                CollisionObject obj = this._DynamicsWorld.CollisionObjectArray[i];

                var body = RigidBody.Upcast(obj);
                if (body != null && body.MotionState != null)
                {
                    body.MotionState.Dispose();
                }

                this._DynamicsWorld.RemoveCollisionObject(obj);
                obj.Dispose();
            }

            for (int i = 0; i < this._CollisionShapes.Count(); ++i)
            {
                CollisionShape collisionShape = this._CollisionShapes[i];
                this._CollisionShapes[i] = null;
                collisionShape.Dispose();
            }

            this._CollisionShapes.Clear();
        }
Exemple #2
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();
        }
Exemple #3
0
        public void Dispose()
        {
            if (_isDisposed)
            {
                return;
            }

            if (World != null)
            {
                AlignedCollisionObjectArray objectArray = World.CollisionObjectArray;
                while (objectArray.Count != 0)
                {
                    int             objectIndex     = objectArray.Count - 1;
                    CollisionObject collisionObject = objectArray[objectIndex];
                    objectArray.RemoveAt(objectIndex);
                    collisionObject.Dispose();
                }

                World.Dispose();
                _dispatcher.Dispose();
                Broadphase.Dispose();
                _conf.Dispose();
            }

            _isDisposed = true;
        }
        public void Dispose()
        {
            RenderCallback.Dispose();
            _staticObject.Dispose();
            _staticShape.Dispose();

            this.StandardCleanup();
        }
    void OnDestroy()
    {
        for (int i = 0; i < createdObjs.Count; i++)
        {
            Destroy(createdObjs[i]);
        }
        createdObjs.Clear();
        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 shape in CollisionShapes)
            {
                shape.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();
        }
    }
Exemple #6
0
        public virtual void ExitPhysics()
        {
            BulletExampleRunner.Get().ExitPhysics();
            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 shape in CollisionShapes)
                {
                    shape.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();
            }
        }
Exemple #7
0
    // Removes the given rigid body from the world and cleans up after it.
    protected void DestroyRigidBody(CollisionObject obj)
    {
        RigidBody body = obj as RigidBody;

        if (body != null && body.MotionState != null)
        {
            body.MotionState.Dispose();
        }
        m_world.RemoveCollisionObject(obj);
        obj.Dispose();
    }
Exemple #8
0
        public bool StepSimulation(float timeStep)
        {
            simTimeStep += timeStep;

            if (simThread != null)
            {
                return(false);
            }

            simThread = new Thread(new ParameterizedThreadStart(delegate(object t)
            {
                float s = (float)t;
                if (s > 1.0f / 60)
                {
                    s = 1.0f / 60;
                }

                bulletWorld.StepSimulation(s);

                IrrlichtLime.Core.Matrix m = new IrrlichtLime.Core.Matrix();
                AlignedCollisionObjectArray collObjects = bulletWorld.CollisionObjectArray;
                for (int i = collObjects.Count - 1; i >= 0; i--)
                {
                    CollisionObject collObject = collObjects[i];
                    if (collObject.IsStaticObject || !collObject.IsActive)
                    {
                        continue;
                    }

                    m.SetElementArray(collObject.WorldTransform.ToArray());

                    SceneNode n = collObject.UserObject as SceneNode;
                    n.Position  = m.Translation;
                    n.Rotation  = m.Rotation;

                    if (m.Translation.Y < -40000)
                    {
                        n.SceneManager.AddToDeletionQueue(n);
                        bulletWorld.RemoveCollisionObject(collObject);
                        collObject.Dispose();
                    }
                }

                simThread = null;
            }));

            simThread.Start(simTimeStep);
            simTimeStep = 0;

            return(true);
        }
 protected virtual void Dispose(bool isdisposing)
 {
     if (isInWorld && isdisposing && m_collisionObject != null)
     {
         BPhysicsWorld pw = BPhysicsWorld.Get();
         if (pw != null && pw.world != null)
         {
             ((DiscreteDynamicsWorld)pw.world).RemoveCollisionObject(m_collisionObject);
         }
     }
     if (m_collisionObject != null)
     {
         m_collisionObject.Dispose();
         m_collisionObject = null;
     }
 }
Exemple #10
0
        public static void Dispose()  // TODO: can crash; examine!
        {
            // remove/dispose of constraints
            for (int i = World.NumConstraints - 1; i >= 0; i--)
            {
                TypedConstraint constraint = World.GetConstraint(i);

                lock (_worldSyncRoot)
                    World.RemoveConstraint(constraint);

                constraint.Dispose();
            }

            // remove the rigidbodies from the dynamics world and delete them
            for (int 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();
                }

                lock (_worldSyncRoot)
                    World.RemoveCollisionObject(obj);

                obj.Dispose();
            }

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

            lock (_worldSyncRoot)
                World.Dispose();

            _broadphase.Dispose();
            if (_dispatcher != null)
            {
                _dispatcher.Dispose();
            }
            _collisionConf.Dispose();
        }
Exemple #11
0
        public void unload()
        {
            int i;

            // Remove constraints
            for (i = _physics_world.world.NumConstraints - 1; i >= 0; i--)
            {
                TypedConstraint constraint = _physics_world.world.GetConstraint(i);
                _physics_world.world.RemoveConstraint(constraint);
                constraint.Dispose();
            }

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

            // Delete collision shapes
            foreach (CollisionShape shape in _physics_world.collision_shapes)
            {
                shape.Dispose();
            }
            _physics_world.collision_shapes.Clear();


            // Delete the world
            _physics_world.world.Dispose();
            _broadphase.Dispose();
            if (_dispatcher != null)
            {
                _dispatcher.Dispose();
            }
            _collision_config.Dispose();
        }
Exemple #12
0
 /// <summary>
 /// リソースを開放する
 /// </summary>
 public void Dispose()
 {
     for (int i = dynamicsWorld.NumCollisionObjects - 1; i >= 0; --i)
     {
         CollisionObject obj  = dynamicsWorld.CollisionObjectArray[i];
         RigidBody       body = RigidBody.Upcast(obj);
         if (body != null && body.MotionState != null)
         {
             body.MotionState.Dispose();
         }
         dynamicsWorld.RemoveCollisionObject(obj);
         obj.Dispose();
     }
     for (int i = 0; i < collisionShapes.Count; ++i)
     {
         CollisionShape collisionShape = collisionShapes[i];
         collisionShapes[i] = null;
         collisionShape.Dispose();
     }
     collisionShapes.Dispose();
 }
Exemple #13
0
        public void ExitPhysics()
        {
            //remove/dispose constraints
            int i;

            for (i = physics_world.NumConstraints - 1; i >= 0; i--)
            {
                TypedConstraint constraint = physics_world.GetConstraint(i);
                physics_world.RemoveConstraint(constraint);
                constraint.Dispose();
            }

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

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

            physics_world.Dispose();
            broadphase.Dispose();
            if (dispatcher != null)
            {
                dispatcher.Dispose();
            }
            collisionConf.Dispose();
        }
Exemple #14
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();
        }
Exemple #15
0
        void ResetScene()
        {
            while (World.NumConstraints > 0)
            {
                TypedConstraint pc = World.GetConstraint(0);
                World.RemoveConstraint(pc);
                pc.Dispose();
            }

            // Don't foreach here, it'll fail.
            for (int i = World.CollisionObjectArray.Count - 1; i >= 0; i--)
            {
                CollisionObject obj = World.CollisionObjectArray[i];

                RigidBody body = RigidBody.Upcast(obj);
                if (body != null && body.MotionState != null)
                {
                    body.MotionState.Dispose();
                }

                if (obj is SoftBody)
                {
                    SoftWorld.RemoveSoftBody(obj as SoftBody);
                }
                else
                {
                    if (body != null)
                    {
                        World.RemoveRigidBody(body);
                    }
                    else
                    {
                        World.RemoveCollisionObject(obj);
                    }
                }
                obj.Dispose();
            }
        }
        private static void CleanupBodiesAndShapes(DynamicsWorld world)
        {
            var shapes = new HashSet <CollisionShape>();

            for (int i = world.NumCollisionObjects - 1; i >= 0; i--)
            {
                CollisionObject obj  = world.CollisionObjectArray[i];
                var             body = obj as RigidBody;
                if (body != null && body.MotionState != null)
                {
                    body.MotionState.Dispose();
                }
                world.RemoveCollisionObject(obj);
                GetShapeWithChildShapes(obj.CollisionShape, shapes);

                obj.Dispose();
            }

            foreach (var shape in shapes)
            {
                shape.Dispose();
            }
        }
Exemple #17
0
        /// <summary>
        /// Disposes all of the objects and the world, essentially a destructor
        /// </summary>
        public void ExitPhysics()
        {
            int i;

            for (i = World.NumConstraints - 1; i >= 0; i--)
            {
                TypedConstraint constraint = World.GetConstraint(i);
                World.RemoveConstraint(constraint);
                constraint.Dispose();
            }

            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();
            }

            foreach (CollisionShape shape in collisionShapes)
            {
                shape.Dispose();
            }
            collisionShapes.Clear();

            World.Dispose();
            broadphase.Dispose();
            if (dispatcher != null)
            {
                dispatcher.Dispose();
            }
            collisionConf.Dispose();
        }
Exemple #18
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();
        }
    }
        protected void Dispose(bool disposing)
        {
            if (debugType >= BDebug.DebugType.Debug)
            {
                Debug.Log("BDynamicsWorld Disposing physics.");
            }

            if (PhysicsWorldHelper != null)
            {
                PhysicsWorldHelper.m_ddWorld = null;
                PhysicsWorldHelper.m_world   = null;
            }
            if (m_world != null)
            {
                //remove/dispose constraints
                int i;
                if (_ddWorld != null)
                {
                    if (debugType >= BDebug.DebugType.Debug)
                    {
                        Debug.LogFormat("Removing Constraints {0}", _ddWorld.NumConstraints);
                    }
                    for (i = _ddWorld.NumConstraints - 1; i >= 0; i--)
                    {
                        TypedConstraint constraint = _ddWorld.GetConstraint(i);
                        _ddWorld.RemoveConstraint(constraint);
                        if (constraint.Userobject is BTypedConstraint)
                        {
                            ((BTypedConstraint)constraint.Userobject).m_isInWorld = false;
                        }
                        if (debugType >= BDebug.DebugType.Debug)
                        {
                            Debug.LogFormat("Removed Constaint {0}", constraint.Userobject);
                        }
                        constraint.Dispose();
                    }
                }

                if (debugType >= BDebug.DebugType.Debug)
                {
                    Debug.LogFormat("Removing Collision Objects {0}", m_world.NumCollisionObjects);
                }
                //remove the rigidbodies from the dynamics world and delete them
                for (i = m_world.NumCollisionObjects - 1; i >= 0; i--)
                {
                    CollisionObject obj  = m_world.CollisionObjectArray[i];
                    RigidBody       body = obj as RigidBody;
                    if (body != null && body.MotionState != null)
                    {
                        Debug.Assert(body.NumConstraintRefs == 0, "Rigid body still had constraints");
                        body.MotionState.Dispose();
                    }
                    m_world.RemoveCollisionObject(obj);
                    if (obj.UserObject is BCollisionObject)
                    {
                        ((BCollisionObject)obj.UserObject).isInWorld = false;
                    }
                    if (debugType >= BDebug.DebugType.Debug)
                    {
                        Debug.LogFormat("Removed CollisionObject {0}", obj.UserObject);
                    }
                    obj.Dispose();
                }

                MultiBodyDynamicsWorld _mbdWorld = m_world as MultiBodyDynamicsWorld;
                if (_mbdWorld != null)
                {
                    if (debugType >= BDebug.DebugType.Debug)
                    {
                        Debug.LogFormat("Removing MultiBody Constraints {0}", _mbdWorld.NumMultiBodyConstraints);
                    }
                    for (i = _mbdWorld.NumMultiBodyConstraints - 1; i >= 0; i--)
                    {
                        MultiBodyConstraint constraint = _mbdWorld.GetMultiBodyConstraint(i);
                        _mbdWorld.RemoveMultiBodyConstraint(constraint);

                        /* if (constraint.Userobject is BTypedConstraint) ((BTypedConstraint)constraint.Userobject).m_isInWorld = false;
                         * if (debugType >= BDebug.DebugType.Debug) Debug.LogFormat("Removed Constaint {0}", constraint.Userobject);*/
                        constraint.Dispose();
                    }

                    if (debugType >= BDebug.DebugType.Debug)
                    {
                        Debug.LogFormat("Removing Multibodies {0}", _mbdWorld.NumMultibodies);
                    }
                    //remove the rigidbodies from the dynamics world and delete them
                    for (i = _mbdWorld.NumMultibodies - 1; i >= 0; i--)
                    {
                        MultiBody mb = _mbdWorld.GetMultiBody(i);

                        _mbdWorld.RemoveMultiBody(mb);
                        if (mb.UserObject is BMultiBody bmb)
                        {
                            bmb.isInWorld = false;
                        }
                        if (debugType >= BDebug.DebugType.Debug)
                        {
                            Debug.LogFormat("Removed CollisionObject {0}", mb.UserObject);
                        }
                        mb.Dispose();
                    }
                }

                if (m_world.DebugDrawer != null)
                {
                    if (m_world.DebugDrawer is IDisposable)
                    {
                        IDisposable dis = (IDisposable)m_world.DebugDrawer;
                        dis.Dispose();
                    }
                }

                m_world.Dispose();
                Broadphase.Dispose();
                Dispatcher.Dispose();
                CollisionConf.Dispose();
                _ddWorld = null;
                m_world  = null;
            }

            if (Broadphase != null)
            {
                Broadphase.Dispose();
                Broadphase = null;
            }
            if (Dispatcher != null)
            {
                Dispatcher.Dispose();
                Dispatcher = null;
            }
            if (CollisionConf != null)
            {
                CollisionConf.Dispose();
                CollisionConf = null;
            }
            if (constraintSolver != null)
            {
                constraintSolver.Dispose();
                constraintSolver = null;
            }
            if (softBodyWorldInfo != null)
            {
                softBodyWorldInfo.Dispose();
                softBodyWorldInfo = null;
            }
            _isDisposed = true;
            singleton   = null;
        }
Exemple #20
0
        protected void Dispose(bool disposing)
        {
            if (lateUpdateHelper != null)
            {
                lateUpdateHelper.m_ddWorld = null;
                lateUpdateHelper.m_world   = null;
            }
            if (m_world != null)
            {
                //remove/dispose constraints
                int i;
                if (_ddWorld != null)
                {
                    for (i = _ddWorld.NumConstraints - 1; i >= 0; i--)
                    {
                        TypedConstraint constraint = _ddWorld.GetConstraint(i);
                        _ddWorld.RemoveConstraint(constraint);
                        if (constraint.Userobject is BTypedConstraint)
                        {
                            ((BTypedConstraint)constraint.Userobject).m_isInWorld = false;
                        }
                        constraint.Dispose();
                    }
                }

                //remove the rigidbodies from the dynamics world and delete them
                for (i = m_world.NumCollisionObjects - 1; i >= 0; i--)
                {
                    CollisionObject obj  = m_world.CollisionObjectArray[i];
                    RigidBody       body = obj as RigidBody;
                    if (body != null && body.MotionState != null)
                    {
                        Debug.Assert(body.NumConstraintRefs == 0, "Rigid body still had constraints");
                        body.MotionState.Dispose();
                    }
                    m_world.RemoveCollisionObject(obj);
                    if (obj.UserObject is BCollisionObject)
                    {
                        ((BCollisionObject)obj.UserObject).isInWorld = false;
                    }
                    obj.Dispose();
                }

                if (m_world.DebugDrawer != null)
                {
                    if (m_world.DebugDrawer is IDisposable)
                    {
                        IDisposable dis = (IDisposable)m_world.DebugDrawer;
                        dis.Dispose();
                    }
                }

                m_world.Dispose();
                Broadphase.Dispose();
                Dispatcher.Dispose();
                CollisionConf.Dispose();
                _ddWorld = null;
                m_world  = null;
            }

            if (Broadphase != null)
            {
                Broadphase.Dispose();
                Broadphase = null;
            }
            if (Dispatcher != null)
            {
                Dispatcher.Dispose();
                Dispatcher = null;
            }
            if (CollisionConf != null)
            {
                CollisionConf.Dispose();
                CollisionConf = null;
            }
            if (constraintSolver != null)
            {
                constraintSolver.Dispose();
                constraintSolver = null;
            }
            if (softBodyWorldInfo != null)
            {
                softBodyWorldInfo.Dispose();
                softBodyWorldInfo = null;
            }
            _isDisposed = true;
            singleton   = null;
        }
Exemple #21
0
    private void Dispose(bool disposing)
    {
        if (physicWorldParameters.debugType >= BDebug.DebugType.Debug)
        {
            Debug.Log("BDynamicsWorld Disposing physics.");
        }

        if (_world != null)
        {
            //remove/dispose constraints
            int i;
            if (DDWorld != null)
            {
                if (physicWorldParameters.debugType >= BDebug.DebugType.Debug)
                {
                    Debug.LogFormat("Removing Constraints {0}", DDWorld.NumConstraints);
                }

                for (i = DDWorld.NumConstraints - 1; i >= 0; i--)
                {
                    TypedConstraint constraint = DDWorld.GetConstraint(i);
                    DDWorld.RemoveConstraint(constraint);

                    if (constraint.Userobject is BTypedConstraint)
                    {
                        ((BTypedConstraint)constraint.Userobject).m_isInWorld = false;
                    }

                    if (physicWorldParameters.debugType >= BDebug.DebugType.Debug)
                    {
                        Debug.LogFormat("Removed Constaint {0}", constraint.Userobject);
                    }

                    constraint.Dispose();
                }
            }

            if (physicWorldParameters.debugType >= BDebug.DebugType.Debug)
            {
                Debug.LogFormat("Removing Collision Objects {0}", DDWorld.NumCollisionObjects);
            }

            //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)
                {
                    Debug.Assert(body.NumConstraintRefs == 0, "Rigid body still had constraints");
                    body.MotionState.Dispose();
                }

                _world.RemoveCollisionObject(obj);

                if (obj.UserObject is BCollisionObject)
                {
                    ((BCollisionObject)obj.UserObject).isInWorld = false;
                }

                if (physicWorldParameters.debugType >= BDebug.DebugType.Debug)
                {
                    Debug.LogFormat("Removed CollisionObject {0}", obj.UserObject);
                }

                obj.Dispose();
            }

            if (_world.DebugDrawer != null && _world.DebugDrawer is IDisposable)
            {
                ((IDisposable)_world.DebugDrawer).Dispose();
            }

            _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 (constraintSolver != null)
        {
            constraintSolver.Dispose();
            constraintSolver = null;
        }

        if (ghostPairCallback != null)
        {
            ghostPairCallback.Dispose();
            ghostPairCallback = null;
        }

        isDisposed = true;
    }
Exemple #22
0
        protected void Dispose(bool disposing)
        {
            BDebug.Log(debugType, "BDynamicsWorld Disposing physics.");

            if (m_world != null)
            {
                //remove/dispose constraints
                int i;
                if (_ddWorld != null)
                {
                    for (i = _ddWorld.NumConstraints - 1; i >= 0; i--)
                    {
                        TypedConstraint constraint = _ddWorld.GetConstraint(i);
                        _ddWorld.RemoveConstraint(constraint);
                        if (constraint.Userobject is BTypedConstraint)
                        {
                            ((BTypedConstraint)constraint.Userobject).isInWorld = false;
                        }
                        if ((debugType & BDebug.DebugType.Debug) == BDebug.DebugType.Debug)
                        {
                            Debug.LogFormat("Removed Constaint {0}", constraint.Userobject);
                        }
                        constraint.Dispose();
                    }
                }

                //remove the rigidbodies from the dynamics world and delete them
                for (i = m_world.NumCollisionObjects - 1; i >= 0; i--)
                {
                    CollisionObject obj  = m_world.CollisionObjectArray[i];
                    RigidBody       body = obj as RigidBody;
                    if (body != null && body.MotionState != null)
                    {
                        body.MotionState.Dispose();
                    }
                    m_world.RemoveCollisionObject(obj);
                    if (obj.UserObject is BCollisionObject)
                    {
                        ((BCollisionObject)obj.UserObject).isInWorld = false;
                    }
                    if ((debugType & BDebug.DebugType.Debug) == BDebug.DebugType.Debug)
                    {
                        Debug.LogFormat("Removed CollisionObject {0}", obj.UserObject);
                    }
                    obj.Dispose();
                }

                if (m_world.DebugDrawer != null)
                {
                    if (m_world.DebugDrawer is IDisposable)
                    {
                        IDisposable dis = (IDisposable)m_world.DebugDrawer;
                        dis.Dispose();
                    }
                }

                m_world.Dispose();
                Broadphase.Dispose();
                Dispatcher.Dispose();
                CollisionConf.Dispose();
                _ddWorld = null;
                m_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;
            }
            if (softBodyWorldInfo != null)
            {
                softBodyWorldInfo.Dispose();
                softBodyWorldInfo = null;
            }
            _isDisposed = true;
            singleton   = null;
        }
Exemple #23
0
        protected override void Dispose(bool disposing)
        {
            Debug.Log("BDynamicsWorld Disposing physics.");

            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();
                }

                if (World.DebugDrawer != null)
                {
                    if (World.DebugDrawer is IDisposable)
                    {
                        IDisposable dis = (IDisposable)World.DebugDrawer;
                        dis.Dispose();
                    }
                }

                //delete collision shapes
                //foreach (CollisionShape shape in CollisionShapes)
                //    shape.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();
            }
            base.Dispose(disposing);
        }
Exemple #24
0
 public void EliminarObjeto(CollisionObject objeto)
 {
     collisionWorld.RemoveCollisionObject(objeto);
     objeto.Dispose();
 }
        /*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();
            }
        }
Exemple #26
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);
    }
Exemple #27
0
    public virtual void exitPhysics()
    {
        removePickingConstraint();
        //cleanup in the reverse order of creation/initialization

        //remove the rigidbodies from the dynamics world and delete them

        if (m_dynamicsWorld == null)
        {
            int i;
            for (i = m_dynamicsWorld.NumConstraints - 1; i >= 0; i--)
            {
                m_dynamicsWorld.RemoveConstraint(m_dynamicsWorld.GetConstraint(i));
            }

            for (i = m_dynamicsWorld.NumMultiBodyConstraints - 1; i >= 0; i--)
            {
                MultiBodyConstraint mbc = m_dynamicsWorld.GetMultiBodyConstraint(i);
                m_dynamicsWorld.RemoveMultiBodyConstraint(mbc);
                mbc.Dispose();
            }

            for (i = m_dynamicsWorld.NumMultibodies - 1; i >= 0; i--)
            {
                MultiBody mb = m_dynamicsWorld.GetMultiBody(i);
                m_dynamicsWorld.RemoveMultiBody(mb);
                mb.Dispose();
            }
            for (i = m_dynamicsWorld.NumCollisionObjects - 1; i >= 0; i--)
            {
                CollisionObject obj  = m_dynamicsWorld.CollisionObjectArray[i];
                RigidBody       body = RigidBody.Upcast(obj);
                if (body != null && body.MotionState != null)
                {
                    body.MotionState.Dispose();
                }
                m_dynamicsWorld.RemoveCollisionObject(obj);
                obj.Dispose();
            }
        }
        //delete collision shapes
        for (int j = 0; j < m_collisionShapes.Count; j++)
        {
            CollisionObject shape = m_collisionShapes[j];
            shape.Dispose();
        }
        m_collisionShapes.Clear();

        m_dynamicsWorld.Dispose();
        m_dynamicsWorld = null;

        m_solver.Dispose();
        m_solver = null;

        m_broadphase.Dispose();
        m_broadphase = null;

        m_dispatcher.Dispose();
        m_dispatcher = null;

        m_pairCache.Dispose();
        m_pairCache = null;

        //m_filterCallback.Dispose();
        //m_filterCallback = null;

        m_collisionConfiguration.Dispose();
        m_collisionConfiguration = null;
    }
Exemple #28
0
    // Disposes of all Unity and Bullet objects and closes the application
    protected void ExitSimulation()
    {
        Debug.Log("Quitting simulation...");
        m_quitting = true;
        // destroy all Unity objects
        for (int i = 0; i < m_createdObjs.Count; i++)
        {
            Destroy(m_createdObjs[i]);
        }
        m_createdObjs.Clear();

        // destroy all bullet objects
        if (m_world != null)
        {
            //remove/dispose constraints
            int i;
            for (i = m_world.NumConstraints - 1; i >= 0; i--)
            {
                TypedConstraint constraint = m_world.GetConstraint(i);
                m_world.RemoveConstraint(constraint);
                constraint.Dispose();
            }

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

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

            m_world.Dispose();
            m_broadphase.Dispose();
            m_colDispatcher.Dispose();
            m_colConfig.Dispose();
        }

        if (m_broadphase != null)
        {
            m_broadphase.Dispose();
        }
        if (m_colDispatcher != null)
        {
            m_colDispatcher.Dispose();
        }
        if (m_colConfig != null)
        {
            m_colConfig.Dispose();
        }

        Application.Quit();
    }
Exemple #29
0
        public override void ExitPhysics()
        {
            if (m_inverseModel != null)
            {
                Debug.Log("Dispose inverse model " + m_inverseModel.NumBodies);
                m_inverseModel.Dispose();
            }

            Debug.Log("InverseDynamicsExitPhysics");
            //cleanup in the reverse order of creation/initialization

            //remove the rigidbodies from the dynamics world and delete them

            if (m_dynamicsWorld == null)
            {
                int i;
                for (i = m_dynamicsWorld.NumConstraints - 1; i >= 0; i--)
                {
                    TypedConstraint tc = m_dynamicsWorld.GetConstraint(i);
                    m_dynamicsWorld.RemoveConstraint(tc);
                    tc.Dispose();
                }

                for (i = m_dynamicsWorld.NumMultiBodyConstraints - 1; i >= 0; i--)
                {
                    MultiBodyConstraint mbc = m_dynamicsWorld.GetMultiBodyConstraint(i);
                    m_dynamicsWorld.RemoveMultiBodyConstraint(mbc);
                    mbc.Dispose();
                }

                for (i = m_dynamicsWorld.NumMultibodies - 1; i >= 0; i--)
                {
                    MultiBody mb = m_dynamicsWorld.GetMultiBody(i);
                    m_dynamicsWorld.RemoveMultiBody(mb);
                    mb.Dispose();
                }
                for (i = m_dynamicsWorld.NumCollisionObjects - 1; i >= 0; i--)
                {
                    CollisionObject obj  = m_dynamicsWorld.CollisionObjectArray[i];
                    RigidBody       body = RigidBody.Upcast(obj);
                    if (body != null && body.MotionState != null)
                    {
                        body.MotionState.Dispose();
                    }
                    m_dynamicsWorld.RemoveCollisionObject(obj);
                    obj.Dispose();
                }
            }

            if (m_multiBody != null)
            {
                m_multiBody.Dispose();
            }

            //delete collision shapes
            for (int j = 0; j < CollisionShapes.Count; j++)
            {
                CollisionShape shape = CollisionShapes[j];
                shape.Dispose();
            }
            CollisionShapes.Clear();

            m_dynamicsWorld.Dispose();
            m_dynamicsWorld = null;

            m_solver.Dispose();
            m_solver = null;

            Broadphase.Dispose();
            Broadphase = null;

            Dispatcher.Dispose();
            Dispatcher = null;

            m_pairCache.Dispose();
            m_pairCache = null;

            CollisionConf.Dispose();
            CollisionConf = null;
            Debug.Log("After dispose B");
        }