Exemplo n.º 1
0
        public void Initialize()
        {
            if (World != null) // check if we are already initialized
            {
                return;
            }

            enabled = true;

            CollisionConf = new DefaultCollisionConfiguration();
            Dispatcher    = new CollisionDispatcher(CollisionConf);

            // Select Brodphase
            Broadphase = new DbvtBroadphase();
            //Broadphase = new AxisSweep3(new Vector3(-1000.0f, -1000.0f, -30.0f), new Vector3(2000.0f, 3000.0f, 1000.0f), 32766);

            DantzigSolver dtsolver = new DantzigSolver();

            Solver = new MlcpSolver(dtsolver);

            //Create a collision world of your choice, for now pass null as the constraint solver
            World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf);
            var si = World.SolverInfo;

            si.NumIterations = 2;
            si.TimeStep      = 0.001f;

            World.SetGravity(ref _gravityVec);
            ghostPairCallback = new GhostPairCallback();
            World.PairCache.SetInternalGhostPairCallback(ghostPairCallback);
        }
Exemplo n.º 2
0
        public bool AddCollisionObject(BCollisionObject co)
        {
            if (co is BRigidBody)
            {
                return(AddRigidBody((BRigidBody)co));
            }
            if (co is BSoftBody)
            {
                return(AddSoftBody((BSoftBody)co));
            }

            if (!_isDisposed)
            {
                if (debugType >= BDebug.DebugType.Debug)
                {
                    Debug.LogFormat("Adding collision object {0} to world", co);
                }
                if (co._BuildCollisionObject())
                {
                    m_world.AddCollisionObject(co.GetCollisionObject(), co.groupsIBelongTo, co.collisionMask);
                    co.isInWorld = true;
                    if (ghostPairCallback == null && co is BGhostObject && world is DynamicsWorld)
                    {
                        ghostPairCallback = new GhostPairCallback();
                        ((DynamicsWorld)world).PairCache.SetInternalGhostPairCallback(ghostPairCallback);
                    }
                    if (co is BCharacterController && world is DynamicsWorld)
                    {
                        AddAction(((BCharacterController)co).GetKinematicCharacterController());
                    }
                }
                return(true);
            }
            return(false);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Constructs a new PhysicsSimulation. Must be created on the physics thread.
        /// </summary>
        internal PhysicsSimulation()
        {
            _ghostPairCallback     = new GhostPairCallback();
            Broadphase             = new DbvtBroadphase();
            CollisionConfiguration = new DefaultCollisionConfiguration();

            Dispatcher = new CollisionDispatcher(CollisionConfiguration);
            Solver     = new SequentialImpulseConstraintSolver(); // Projected Gauss Seidel
            World      = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConfiguration)
            {
                DebugDrawer = _debugDraw = new PhysicsDebugDraw()
            };
            World.PairCache.SetInternalGhostPairCallback(_ghostPairCallback);

            _stopwatch.Start();
        }
        public void SetUp()
        {
            _context = new PhysicsContext();
            _context.InitializeWorld();

            _ghostPairCallback = new GhostPairCallback();
            _context.Broadphase.OverlappingPairCache.SetInternalGhostPairCallback(_ghostPairCallback);

            _shape = new BoxShape(2);
            RigidBody staticBox  = _context.AddStaticBody(_shape, Matrix.Translation(0, 0, 0));
            RigidBody dynamicBox = _context.AddBody(_shape, Matrix.Translation(0, 1, 0), 1);

            _ghostObject = new PairCachingGhostObject
            {
                CollisionShape = _shape,
                WorldTransform = Matrix.Translation(2, 2, 0)
            };
            _context.World.AddCollisionObject(_ghostObject);
        }
        private void CreateCharacter()
        {
            _ghostPairCallback = new GhostPairCallback();
            Broadphase.OverlappingPairCache.SetInternalGhostPairCallback(_ghostPairCallback);

            const float characterHeight = 1.75f;
            const float characterWidth  = 1.75f;
            var         capsule         = new CapsuleShape(characterWidth, characterHeight);

            GhostObject = new PairCachingGhostObject()
            {
                CollisionShape = capsule,
                WorldTransform = Matrix.Translation(10.210098f, -1.6433364f, 16.453260f)
            };
            GhostObject.CollisionFlags |= CollisionFlags.CharacterObject;
            World.AddCollisionObject(GhostObject, CollisionFilterGroups.CharacterFilter, CollisionFilterGroups.StaticFilter | CollisionFilterGroups.DefaultFilter);

            const float stepHeight = 0.35f;
            Vector3     up         = Vector3.UnitY;

            Character = new KinematicCharacterController(GhostObject, capsule, stepHeight, ref up);
            World.AddAction(Character);
        }
Exemplo n.º 6
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();
        }
Exemplo n.º 7
0
        protected override void Initialize()
        {
            base.Initialize();
            SetCameraDistance(50.0f);

            ///collision configuration contains default setup for memory, collision setup
            m_collisionConfiguration = new DefaultCollisionConfiguration();
            //m_collisionConfiguration.setConvexConvexMultipointIterations();

            ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
            m_dispatcher = new CollisionDispatcher(m_collisionConfiguration);

            m_broadphase = new DbvtBroadphase();

            ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
            m_constraintSolver = new SequentialImpulseConstraintSolver();

            m_dynamicsWorld = new DiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_constraintSolver, m_collisionConfiguration);

            IndexedVector3 gravity = new IndexedVector3(0, -10, 0);

            m_dynamicsWorld.SetGravity(ref gravity);


            // NEW => btGhostPairCallback =================================
            m_ghostPairCallback = new GhostPairCallback();
            m_dynamicsWorld.GetBroadphase().GetOverlappingPairCache().SetInternalGhostPairCallback(m_ghostPairCallback);        // Needed once to enable ghost objects inside Bullet

            // NEW => btGhostObject =======================================
            m_ghostObject = new GhostObject();
            CollisionShape shape = new BoxShape(new IndexedVector3(5f));        // As far as I know only the world aabb of the shape will be used (i.e. a box always parallel to the x,y,z axis of variable size)

            m_collisionShapes.Add(shape);
            m_ghostObject.SetCollisionShape(shape);
            m_ghostObject.SetCollisionFlags(CollisionFlags.CF_NO_CONTACT_RESPONSE);             // We can choose to make it "solid" if we want...
            m_dynamicsWorld.AddCollisionObject(m_ghostObject, CollisionFilterGroups.SensorTrigger, CollisionFilterGroups.AllFilter & ~CollisionFilterGroups.SensorTrigger);
            //m_ghostObject.setWorldTransform(btTransform(btQuaternion::getIdentity(),btVector3(0,5,-15)));
            IndexedMatrix im = IndexedMatrix.CreateFromQuaternion(quatDeg45Y);

            im._origin = new IndexedVector3(0, 5, -15);
            m_ghostObject.SetWorldTransform(im);

            // NEW => btPairCachingGhostObject ============================
            m_pairCachingGhostObject = new PairCachingGhostObject();
            shape = new ConeShape(7.0f, 14.0f);
            m_collisionShapes.Add(shape);
            m_pairCachingGhostObject.SetCollisionShape(shape);
            m_pairCachingGhostObject.SetCollisionFlags(CollisionFlags.CF_NO_CONTACT_RESPONSE);  // We can choose to make it "solid" if we want...
            m_dynamicsWorld.AddCollisionObject(m_pairCachingGhostObject, CollisionFilterGroups.SensorTrigger, CollisionFilterGroups.AllFilter & ~CollisionFilterGroups.SensorTrigger);
            //m_pairCachingGhostObject.setWorldTransform(btTransform(btQuaternion::getIdentity(),btVector3(0,5,15)));
            im._origin = new IndexedVector3(0, 7, 15);
            m_pairCachingGhostObject.SetWorldTransform(im);
            //=============================================================

            ///create a few basic rigid bodies
            CollisionShape groundShape = new BoxShape(new IndexedVector3(50));

            m_collisionShapes.Add(groundShape);

            IndexedMatrix groundTransform = IndexedMatrix.Identity;

            groundTransform._origin = new IndexedVector3(0, -50, 0);

            float mass = 0.0f;

            LocalCreateRigidBody(mass, groundTransform, groundShape);

            // spawn some cubes (code pasted from appBasicDemo...)
            if (true)
            {
                //create a few dynamic rigidbodies
                CollisionShape colShape = new BoxShape(new IndexedVector3(SCALING, SCALING, SCALING));
                //btCollisionShape* colShape = new btSphereShape(btScalar(1.));
                //CollisionShape colShape = new CylinderShape(new IndexedVector3(1f, 1, 1f));
                m_collisionShapes.Add(colShape);

                /// Create Dynamic Objects
                IndexedMatrix startTransform = IndexedMatrix.Identity;

                mass = 1f;

                //rigidbody is dynamic if and only if mass is non zero, otherwise static
                bool isDynamic = mass != 0f;

                IndexedVector3 localInertia = IndexedVector3.Zero;
                if (isDynamic)
                {
                    colShape.CalculateLocalInertia(mass, out localInertia);
                }
                float start_x = START_POS_X - ARRAY_SIZE_X / 2;
                float start_y = START_POS_Y;
                float start_z = START_POS_Z - ARRAY_SIZE_Z / 2;

                for (int k = 0; k < ARRAY_SIZE_Y; k++)
                {
                    for (int i = 0; i < ARRAY_SIZE_X; i++)
                    {
                        for (int j = 0; j < ARRAY_SIZE_Z; j++)
                        {
                            startTransform._origin = (new IndexedVector3(2.0f * i + start_x, 20 + 2.0f * k + start_y, 2.0f * j + start_z) * SCALING);

                            //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
                            DefaultMotionState        myMotionState = new DefaultMotionState(startTransform, IndexedMatrix.Identity);
                            RigidBodyConstructionInfo rbInfo        = new RigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia);
                            RigidBody body = new RigidBody(rbInfo);
                            //body.setContactProcessingThreshold(colShape.getContactBreakingThreshold());
                            body.SetActivationState(ActivationState.ISLAND_SLEEPING);

                            m_dynamicsWorld.AddRigidBody(body);
                            body.SetActivationState(ActivationState.ISLAND_SLEEPING);
                            body.SetUserPointer(String.Format("Box X{0} Y{1} Z{2}", k, i, j));
                        }
                    }
                }
            }

            ClientResetScene();
        }