public void Dispose()
        {
            this.StandardCleanup();

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

            if (_parallelSolver != null)
            {
                _parallelSolver.Dispose();
                _parallelSolver = null;
            }
        }
        public MultiThreadedDemoSimulation()
        {
            CreateSchedulers();
            NextTaskScheduler();

            using (var collisionConfigurationInfo = new DefaultCollisionConstructionInfo
            {
                DefaultMaxPersistentManifoldPoolSize = 80000,
                DefaultMaxCollisionAlgorithmPoolSize = 80000
            })
            {
                CollisionConfiguration = new DefaultCollisionConfiguration(collisionConfigurationInfo);
            };
            Dispatcher                  = new CollisionDispatcherMultiThreaded(CollisionConfiguration);
            Broadphase                  = new DbvtBroadphase();
            _constraintSolver           = new ConstraintSolverPoolMultiThreaded(MaxThreadCount);
            World                       = new DiscreteDynamicsWorldMultiThreaded(Dispatcher, Broadphase, _constraintSolver, CollisionConfiguration);
            World.SolverInfo.SolverMode = SolverModes.Simd | SolverModes.UseWarmStarting;

            CreateGround();
            CreateBoxes();
        }
Пример #3
0
        /// <summary>
        /// Does not set any local variables. Is safe to use to create duplicate physics worlds for independant simulation.
        /// </summary>
        /// <param name="world"></param>
        /// <param name="collisionConfig"></param>
        /// <param name="dispatcher"></param>
        /// <param name="broadphase"></param>
        /// <param name="solver"></param>
        /// <param name="softBodyWorldInfo"></param>
        /// <returns></returns>
        public bool CreatePhysicsWorld(out CollisionWorld world,
                                       out CollisionConfiguration collisionConfig,
                                       out CollisionDispatcher dispatcher,
                                       out BroadphaseInterface broadphase,
                                       out ConstraintSolver solver,
                                       out SoftBodyWorldInfo softBodyWorldInfo)
        {
            bool success = true;

            if (m_worldType == WorldType.SoftBodyAndRigidBody && m_collisionType == CollisionConfType.DefaultDynamicsWorldCollisionConf)
            {
                Debug.LogError("For World Type = SoftBodyAndRigidBody collisionType must be collisionType=SoftBodyRigidBodyCollisionConf. Switching");
                m_collisionType = CollisionConfType.SoftBodyRigidBodyCollisionConf;
                success         = false;
            }

            collisionConfig = null;
            if (m_collisionType == CollisionConfType.DefaultDynamicsWorldCollisionConf)
            {
                collisionConfig = new DefaultCollisionConfiguration();
            }
            else if (m_collisionType == CollisionConfType.SoftBodyRigidBodyCollisionConf)
            {
                collisionConfig = new SoftBodyRigidBodyCollisionConfiguration();
            }

            dispatcher = new CollisionDispatcher(collisionConfig);

            if (m_broadphaseType == BroadphaseType.DynamicAABBBroadphase)
            {
                broadphase = new DbvtBroadphase();
            }
            else if (m_broadphaseType == BroadphaseType.Axis3SweepBroadphase)
            {
                broadphase = new AxisSweep3(m_axis3SweepBroadphaseMin.ToBullet(), m_axis3SweepBroadphaseMax.ToBullet(), axis3SweepMaxProxies);
            }
            else if (m_broadphaseType == BroadphaseType.Axis3SweepBroadphase_32bit)
            {
                broadphase = new AxisSweep3_32Bit(m_axis3SweepBroadphaseMin.ToBullet(), m_axis3SweepBroadphaseMax.ToBullet(), axis3SweepMaxProxies);
            }
            else
            {
                broadphase = null;
            }
            world             = null;
            softBodyWorldInfo = null;
            solver            = null;
            if (m_worldType == WorldType.CollisionOnly)
            {
                world = new CollisionWorld(dispatcher, broadphase, collisionConfig);
            }
            else if (m_worldType == WorldType.RigidBodyDynamics)
            {
                world = new DiscreteDynamicsWorld(dispatcher, broadphase, null, collisionConfig);
            }
            else if (m_worldType == WorldType.MultiBodyWorldMultiThreaded)
            {
                ConstraintSolverPoolMultiThreaded multiThreadedConstraintSolver = new ConstraintSolverPoolMultiThreaded(4);
                world = new DiscreteDynamicsWorldMultiThreaded(dispatcher, broadphase, multiThreadedConstraintSolver, solver, collisionConfig);
            }
            else if (m_worldType == WorldType.MultiBodyWorld)
            {
                MultiBodyConstraintSolver mbConstraintSolver = new MultiBodyConstraintSolver();
                constraintSolver = mbConstraintSolver;
                world            = new MultiBodyDynamicsWorld(dispatcher, broadphase, mbConstraintSolver, collisionConfig);
                if (debugType >= BDebug.DebugType.Debug)
                {
                    Debug.Log("Created MultiBodyDynamicsWorld " + world);
                }
            }
            else if (m_worldType == WorldType.SoftBodyAndRigidBody)
            {
                SequentialImpulseConstraintSolver siConstraintSolver = new SequentialImpulseConstraintSolver();
                constraintSolver            = siConstraintSolver;
                siConstraintSolver.RandSeed = sequentialImpulseConstraintSolverRandomSeed;
                m_world  = new SoftRigidDynamicsWorld(Dispatcher, Broadphase, siConstraintSolver, CollisionConf);
                _ddWorld = (DiscreteDynamicsWorld)m_world;
                SoftRigidDynamicsWorld _sworld = (SoftRigidDynamicsWorld)m_world;

                m_world.DispatchInfo.EnableSpu = true;
                _sworld.WorldInfo.SparseSdf.Initialize();
                _sworld.WorldInfo.SparseSdf.Reset();
                _sworld.WorldInfo.AirDensity   = 1.2f;
                _sworld.WorldInfo.WaterDensity = 0;
                _sworld.WorldInfo.WaterOffset  = 0;
                _sworld.WorldInfo.WaterNormal  = BulletSharp.Math.Vector3.Zero;
                _sworld.WorldInfo.Gravity      = m_gravity.ToBullet();
            }
            if (world is DiscreteDynamicsWorld)
            {
                ((DiscreteDynamicsWorld)world).Gravity = m_gravity.ToBullet();
            }
            if (_doDebugDraw)
            {
                DebugDrawUnity db = new DebugDrawUnity();
                db.DebugMode      = _debugDrawMode;
                world.DebugDrawer = db;
            }
            return(success);
        }
        //Initialize the physics world
        private void InitializeWorld(DynamicsWorld.InternalTickCallback tickCallBack)
        {
            //Collision configuration and dispatcher
            var collisionConfigInfo = new DefaultCollisionConstructionInfo {
                DefaultMaxCollisionAlgorithmPoolSize = 80000,
                DefaultMaxPersistentManifoldPoolSize = 80000
            };

            _collisionConfig = new DefaultCollisionConfiguration(collisionConfigInfo);

            //Solver Type Config
            switch (_solverType)
            {
            case WorldSolverType.SequentialImpulse:
                _constraintSolver    = new MultiBodyConstraintSolver();
                _collisionDispatcher = new CollisionDispatcher(_collisionConfig);
                break;

            case WorldSolverType.NonSmoothNonLinearConjugate:
                _constraintSolver    = new NncgConstraintSolver();
                _collisionDispatcher = new CollisionDispatcher(_collisionConfig);
                break;

            case WorldSolverType.ExperimentalMultiThreaded:     //EXPERIMENTAL
                switch (_schedulerType)
                {
                case TaskSchedulerType.Sequential:
                    Threads.TaskScheduler = Threads.GetSequentialTaskScheduler();
                    break;

                case TaskSchedulerType.OpenMp:
                    Threads.TaskScheduler = Threads.GetOpenMPTaskScheduler();
                    break;

                case TaskSchedulerType.Ppl:
                    Threads.TaskScheduler = Threads.GetPplTaskScheduler();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                _threadedSolver = new ConstraintSolverPoolMultiThreaded(_solverThreads);
                Threads.TaskScheduler.NumThreads = _threadPoolSize;
                _collisionDispatcher             = new CollisionDispatcherMultiThreaded(_collisionConfig);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            //Broadphase Type COnfig
            switch (_broadphaseType)
            {
            case WorldBroadphaseType.DynamicAabb:
                _broadphaseInterface = new DbvtBroadphase();
                break;

            case WorldBroadphaseType.AxisSweep:
                _broadphaseInterface = new AxisSweep3(-_axisSweepMargin.ToBullet(), _axisSweepMargin.ToBullet());
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            //Create the physics world
            if (_solverType == WorldSolverType.ExperimentalMultiThreaded)
            {
                _dynamicsWorld = new DiscreteDynamicsWorldMultiThreaded(_collisionDispatcher, _broadphaseInterface, _threadedSolver, _collisionConfig);
            }
            else
            {
                _dynamicsWorld = new DiscreteDynamicsWorld(_collisionDispatcher, _broadphaseInterface, _constraintSolver, _collisionConfig);
            }

            //Configure the physics world
            _dynamicsWorld.SolverInfo.NumIterations = _solverIterations;
            _dynamicsWorld.SolverInfo.SolverMode    = SolverModes.Simd | SolverModes.UseWarmStarting;
            _dynamicsWorld.SetInternalTickCallback(tickCallBack);
            _btGravity = _gravity.ToBullet();
            _dynamicsWorld.SetGravity(ref _btGravity);
            _initlialized = true;
            if (_debugging)
            {
                Debug.Log("<b>Bullet4Unity:</b> Initialized Bullet Physics World");
            }
        }