protected override void OnInitializePhysics()
        {
            // collision configuration contains default setup for memory, collision setup
            CollisionConf = new DefaultCollisionConfiguration();
            Dispatcher    = new CollisionDispatcher(CollisionConf);

            Broadphase = new AxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000));
            Solver     = new SequentialImpulseConstraintSolver();

            World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf);
            World.DispatchInfo.AllowedCcdPenetration = 0.0001f;
            //World.Gravity = Freelook.Up * -10.0f;

            Matrix startTransform = Matrix.Translation(10.210098f, -1.6433364f, 16.453260f);

            ghostObject = new PairCachingGhostObject();
            ghostObject.WorldTransform = startTransform;
            Broadphase.OverlappingPairCache.SetInternalGhostPairCallback(new GhostPairCallback());

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

            ghostObject.CollisionShape = capsule;
            ghostObject.CollisionFlags = CollisionFlags.CharacterObject;

            const float stepHeight = 0.35f;

            character = new KinematicCharacterController(ghostObject, capsule, stepHeight);

            BspLoader bspLoader = new BspLoader();

            //string filename = UnityEngine.Application.dataPath + "/BulletUnity/Examples/Scripts/BulletSharpDemos/CharacterDemo/data/BspDemo.bsp";
            UnityEngine.TextAsset bytes      = (UnityEngine.TextAsset)UnityEngine.Resources.Load("BspDemo");
            System.IO.Stream      byteStream = new System.IO.MemoryStream(bytes.bytes);
            bspLoader.LoadBspFile(byteStream);
            BspConverter bsp2Bullet = new BspToBulletConverter(this);

            bsp2Bullet.ConvertBsp(bspLoader, 0.1f);

            World.AddCollisionObject(ghostObject, CollisionFilterGroups.CharacterFilter, CollisionFilterGroups.StaticFilter | CollisionFilterGroups.DefaultFilter);

            World.AddAction(character);

            Vector3 v1 = new Vector3(0f, 0f, 0f);
            Vector3 v2 = new Vector3(0f, 0f, 0f);

            convexResultCallback = new ClosestConvexResultCallback(ref v1, ref v2);
            convexResultCallback.CollisionFilterMask = (short)CollisionFilterGroups.StaticFilter;
            cameraSphere = new SphereShape(0.2f);
        }
        protected override void OnInitializePhysics()
        {
            // collision configuration contains default setup for memory, collision setup
            CollisionConf = new DefaultCollisionConfiguration();
            Dispatcher    = new CollisionDispatcher(CollisionConf);

            Broadphase = new AxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000));
            Solver     = new SequentialImpulseConstraintSolver();

            World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf);
            World.DispatchInfo.AllowedCcdPenetration = 0.0001f;
            //World.Gravity = Freelook.Up * -10.0f;

            Matrix startTransform = Matrix.Translation(10.210098f, -1.6433364f, 16.453260f);

            ghostObject = new PairCachingGhostObject();
            ghostObject.WorldTransform = startTransform;
            Broadphase.OverlappingPairCache.SetInternalGhostPairCallback(new GhostPairCallback());

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

            ghostObject.CollisionShape = capsule;
            ghostObject.CollisionFlags = CollisionFlags.CharacterObject;

            const float stepHeight = 0.35f;

            character = new KinematicCharacterController(ghostObject, capsule, stepHeight);

            BspLoader bspLoader = new BspLoader();

            bspLoader.LoadBspFile("data/BspDemo.bsp");
            BspConverter bsp2Bullet = new BspToBulletConverter(this);

            bsp2Bullet.ConvertBsp(bspLoader, 0.1f);

            World.AddCollisionObject(ghostObject, CollisionFilterGroups.CharacterFilter, CollisionFilterGroups.StaticFilter | CollisionFilterGroups.DefaultFilter);

            World.AddAction(character);

            Vector3 zero = Vector3.Zero;

            convexResultCallback = new ClosestConvexResultCallback(ref zero, ref zero);
            convexResultCallback.CollisionFilterMask = (short)CollisionFilterGroups.StaticFilter;
            cameraSphere = new SphereShape(0.2f);
        }
        public CharacterDemoSimulation()
        {
            CollisionConfiguration = new DefaultCollisionConfiguration();
            Dispatcher             = new CollisionDispatcher(CollisionConfiguration);

            Broadphase = new AxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000));

            World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConfiguration);
            World.DispatchInfo.AllowedCcdPenetration = 0.0001f;

            CreateCharacter();

            var path      = Path.Combine("data", "BspDemo.bsp");
            var bspLoader = new BspLoader();

            bspLoader.LoadBspFile(path);
            var bsp2Bullet = new BspToBulletConverter(World);

            bsp2Bullet.ConvertBsp(bspLoader, 0.1f);

            ConvexResultCallback = new ClosestConvexResultCallback();
            ConvexResultCallback.CollisionFilterMask = (short)CollisionFilterGroups.StaticFilter;
            CameraSphere = new SphereShape(0.2f);
        }
        protected override void OnInitializePhysics()
        {
            // collision configuration contains default setup for memory, collision setup
            CollisionConf = new DefaultCollisionConfiguration();
            Dispatcher = new CollisionDispatcher(CollisionConf);

            Broadphase = new AxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000));
            Solver = new SequentialImpulseConstraintSolver();

            World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf);
            World.DispatchInfo.AllowedCcdPenetration = 0.0001f;
            //World.Gravity = Freelook.Up * -10.0f;

            Matrix startTransform = Matrix.Translation(10.210098f, -1.6433364f, 16.453260f);
            ghostObject = new PairCachingGhostObject();
            ghostObject.WorldTransform = startTransform;
            Broadphase.OverlappingPairCache.SetInternalGhostPairCallback(new GhostPairCallback());

            const float characterHeight = 1.75f;
            const float characterWidth = 1.75f;
            ConvexShape capsule = new CapsuleShape(characterWidth, characterHeight);
            ghostObject.CollisionShape = capsule;
            ghostObject.CollisionFlags = CollisionFlags.CharacterObject;

            const float stepHeight = 0.35f;
            character = new KinematicCharacterController(ghostObject, capsule, stepHeight);

            BspLoader bspLoader = new BspLoader();
            bspLoader.LoadBspFile("data/BspDemo.bsp");
            BspConverter bsp2Bullet = new BspToBulletConverter(this);
            bsp2Bullet.ConvertBsp(bspLoader, 0.1f);

            World.AddCollisionObject(ghostObject, CollisionFilterGroups.CharacterFilter, CollisionFilterGroups.StaticFilter | CollisionFilterGroups.DefaultFilter);

            World.AddAction(character);

            convexResultCallback = new ClosestConvexResultCallback();
            convexResultCallback.CollisionFilterMask = (short)CollisionFilterGroups.StaticFilter;
            cameraSphere = new SphereShape(0.2f);
        }
        public void ConvertBsp(BspLoader bspLoader, float scaling)
        {
            Vector3 playerStart = new Vector3(0, 0, 100);
            if (bspLoader.FindVectorByName("info_player_start", ref playerStart) == false)
            {
                bspLoader.FindVectorByName("info_player_deathmatch", ref playerStart);
            }
            playerStart[2] += 20.0f; //start a bit higher
            playerStart *= scaling;

            foreach (BspLeaf leaf in bspLoader.Leaves)
            {
                bool isValidBrush = false;

                for (int b = 0; b < leaf.NumLeafBrushes; b++)
                {
                    AlignedVector3Array planeEquations = new AlignedVector3Array();

                    int brushID = bspLoader.LeafBrushes[leaf.FirstLeafBrush + b];
                    BspBrush brush = bspLoader.Brushes[brushID];

                    if (brush.ShaderNum != -1)
                    {
                        ContentFlags flags = bspLoader.IsVbsp ? (ContentFlags)brush.ShaderNum : bspLoader.Shaders[brush.ShaderNum].ContentFlags;
                        if ((flags & ContentFlags.Solid) == ContentFlags.Solid)
                        {
                            brush.ShaderNum = -1;

                            for (int p = 0; p < brush.NumSides; p++)
                            {
                                int sideid = brush.FirstSide + p;

                                BspBrushSide brushside = bspLoader.BrushSides[sideid];
                                int planeid = brushside.PlaneNum;
                                BspPlane plane = bspLoader.Planes[planeid];
                                Vector4 planeEq = new Vector4(plane.Normal, scaling * -plane.Distance);
                                planeEquations.Add(planeEq);
                                isValidBrush = true;
                            }
                            if (isValidBrush)
                            {
                                AlignedVector3Array vertices = new AlignedVector3Array();
                                GeometryUtil.GetVerticesFromPlaneEquations(planeEquations, vertices);

                                const bool isEntity = false;
                                Vector3 entityTarget = Vector3.Zero;
                                AddConvexVerticesCollider(vertices, isEntity, entityTarget);
                            }
                        }
                    }
                }
            }
            /*
            foreach (BspEntity entity in bspLoader.Entities)
            {
                if (entity.ClassName == "trigger_push")
                {
                }
            }
            */
        }
Exemple #6
0
        public void ConvertBsp(BspLoader bspLoader, float scaling)
        {
            Vector3 playerStart = new Vector3(0, 0, 100);

            if (bspLoader.FindVectorByName("info_player_start", ref playerStart) == false)
            {
                bspLoader.FindVectorByName("info_player_deathmatch", ref playerStart);
            }
            playerStart[2] += 20.0f; //start a bit higher
            playerStart    *= scaling;

            foreach (BspLeaf leaf in bspLoader.Leaves)
            {
                bool isValidBrush = false;

                for (int b = 0; b < leaf.NumLeafBrushes; b++)
                {
                    AlignedVector3Array planeEquations = new AlignedVector3Array();

                    int      brushID = bspLoader.LeafBrushes[leaf.FirstLeafBrush + b];
                    BspBrush brush   = bspLoader.Brushes[brushID];

                    if (brush.ShaderNum != -1)
                    {
                        ContentFlags flags = bspLoader.IsVbsp ? (ContentFlags)brush.ShaderNum : bspLoader.Shaders[brush.ShaderNum].ContentFlags;
                        if ((flags & ContentFlags.Solid) == ContentFlags.Solid)
                        {
                            brush.ShaderNum = -1;

                            for (int p = 0; p < brush.NumSides; p++)
                            {
                                int sideid = brush.FirstSide + p;

                                BspBrushSide brushside = bspLoader.BrushSides[sideid];
                                int          planeid   = brushside.PlaneNum;
                                BspPlane     plane     = bspLoader.Planes[planeid];
                                Vector4      planeEq   = new Vector4(plane.Normal, scaling * -plane.Distance);
                                planeEquations.Add(planeEq);
                                isValidBrush = true;
                            }
                            if (isValidBrush)
                            {
                                AlignedVector3Array vertices = new AlignedVector3Array();
                                GeometryUtil.GetVerticesFromPlaneEquations(planeEquations, vertices);

                                const bool isEntity     = false;
                                Vector3    entityTarget = Vector3.Zero;
                                AddConvexVerticesCollider(vertices, isEntity, entityTarget);
                            }
                        }
                    }
                }
            }

            /*
             * foreach (BspEntity entity in bspLoader.Entities)
             * {
             *  if (entity.ClassName == "trigger_push")
             *  {
             *  }
             * }
             */
        }