예제 #1
0
        public void ExitPhysics()
        {
            // Remove/dispose constraints
            for (int i = World.NumConstraints - 1; i >= 0; i--)
            {
                var constraint = World.GetConstraint(i);
                World.RemoveConstraint(constraint);
                constraint.Dispose();
            }

            // Remove/dispose rigid bodies
            for (int i = World.NumCollisionObjects - 1; i >= 0; i--)
            {
                var obj  = World.CollisionObjectArray[i];
                var body = obj as RigidBody;
                if (body != null && body.MotionState != null)
                {
                    body.MotionState.Dispose();
                }
                World.RemoveCollisionObject(obj);
                obj.Dispose();
            }

            //delete collision shapes
            foreach (var shape in CollisionShapes)
            {
                shape.Dispose();
            }
            CollisionShapes.Clear();

            World.Dispose();
            Broadphase.Dispose();
            if (Dispatcher != null)
            {
                Dispatcher.Dispose();
            }
            CollisionConf.Dispose();
        }
예제 #2
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);
    }
예제 #3
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");
        }