コード例 #1
0
ファイル: Collisions.cs プロジェクト: patrykos91/Laikos
        //This method is performing basic collision detection between two models
        //Whole model is surrounded by BoundingBox stored in model.Tag info
        public static bool GeneralCollisionCheck(Unit unit1, Unit unit2)
        {
            //Retrieving data about BoundingBox from model.Tag for first model
            ModelExtra modelExtra = unit1.currentModel.Model.Tag as ModelExtra;
            BoundingSphere originalSphere1 = modelExtra.boundingSphere;
            unit1.boundingSphere = XNAUtils.TransformBoundingSphere(originalSphere1, unit1.GetWorldMatrix());

            //Doing the same thing for second model
            ModelExtra modelExtra1 = unit2.currentModel.Model.Tag as ModelExtra;
            BoundingSphere originalSphere2 = modelExtra1.boundingSphere;
            unit2.boundingSphere = XNAUtils.TransformBoundingSphere(originalSphere2, unit2.GetWorldMatrix());

            //Checking if global bounding Box(surronds whole model) intersects another Box
            bool collision = unit1.boundingSphere.Intersects(unit2.boundingSphere);

            return collision;
        }
コード例 #2
0
ファイル: Collisions.cs プロジェクト: patrykos91/Laikos
        //This method performs much more detailed collision check.
        //It checks if there is collision for each mesh of model
        public static bool DetailedCollisionCheck(Unit unit1, Unit unit2)
        {
            //first we check if there is general collision between two models
            //If method returns false we dont have to perform detailed check
            if (!GeneralCollisionCheck(unit1, unit2))
                return false;

            //Here we are creating BoundingBox for each mesh for model1
            Matrix[] model1Transforms = new Matrix[unit1.currentModel.Model.Bones.Count];
            unit1.currentModel.Model.CopyAbsoluteBoneTransformsTo(model1Transforms);
            BoundingBox[] model1Boxs = new BoundingBox[unit1.currentModel.Model.Meshes.Count];

            for (int i = 0; i < unit1.currentModel.Model.Meshes.Count; i++)
            {
                ModelMesh mesh = unit1.currentModel.Model.Meshes[i];
                BoundingSphere origSphere = mesh.BoundingSphere;
                BoundingBox origBox = BoundingBox.CreateFromSphere(origSphere);
                Matrix trans = model1Transforms[mesh.ParentBone.Index] * unit1.GetWorldMatrix();
                BoundingBox transBox = XNAUtils.TransformBoundingBox(origBox, trans);
                model1Boxs[i] = transBox;
            }

            //and here for second model
            Matrix[] model2Transforms = new Matrix[unit2.currentModel.Model.Bones.Count];
            unit2.currentModel.Model.CopyAbsoluteBoneTransformsTo(model2Transforms);
            BoundingBox[] model2Boxs = new BoundingBox[unit2.currentModel.Model.Meshes.Count];

            for (int i = 0; i < unit2.currentModel.Model.Meshes.Count; i++)
            {
                ModelMesh mesh = unit2.currentModel.Model.Meshes[i];
                BoundingSphere origSphere = mesh.BoundingSphere;
                BoundingBox origBox = BoundingBox.CreateFromSphere(origSphere);
                Matrix trans = model2Transforms[mesh.ParentBone.Index] * unit2.GetWorldMatrix();
                BoundingBox transBox = XNAUtils.TransformBoundingBox(origBox, trans);
                model2Boxs[i] = transBox;
            }

            bool collision = false;

            //Check if any of created before Boxs intersects with another Box
            for (int i = 0; i < model1Boxs.Length; i++)
                for (int j = 0; j < model2Boxs.Length; j++)
                    if (BoundingSphere.CreateFromBoundingBox(model1Boxs[i]).Intersects(BoundingSphere.CreateFromBoundingBox(model2Boxs[j])))
                        return true;

            return collision;
        }