Beispiel #1
0
        private bool FinerCheck(Model model1, Matrix world1, Model model2, Matrix world2)
        {
            if (CoarseCheck(model1, world1, model2, world2) == false)
            {
                return(false);
            }

            bool collision = false;

            Matrix[] model1Transforms = new Matrix[model1.Bones.Count];
            Matrix[] model2Transforms = new Matrix[model2.Bones.Count];
            model1.CopyAbsoluteBoneTransformsTo(model1Transforms);
            model2.CopyAbsoluteBoneTransformsTo(model2Transforms);
            foreach (ModelMesh mesh1 in model1.Meshes)
            {
                BoundingSphere origSphere1  = mesh1.BoundingSphere;
                Matrix         trans1       = model1Transforms[mesh1.ParentBone.Index] * world1;
                BoundingSphere transSphere1 = XNAUtils.TransformBoundingSphere(origSphere1, trans1);

                foreach (ModelMesh mesh2 in model2.Meshes)
                {
                    BoundingSphere origSphere2  = mesh2.BoundingSphere;
                    Matrix         trans2       = model2Transforms[mesh2.ParentBone.Index] * world2;
                    BoundingSphere transSphere2 = XNAUtils.TransformBoundingSphere(origSphere2, trans2);

                    if (transSphere1.Intersects(transSphere2))
                    {
                        collision = true;
                    }
                }
            }
            return(collision);
        }
Beispiel #2
0
        private bool RayCollision(Model model, Matrix world, Vector3 lastPosition, Vector3 currentPosition)
        {
            BoundingSphere modelSpere  = (BoundingSphere)model.Tag;
            BoundingSphere transSphere = XNAUtils.TransformBoundingSphere(modelSpere, world);

            Vector3 direction       = currentPosition - lastPosition;
            float   distanceCovered = direction.Length();

            direction.Normalize();

            Ray ray = new Ray(lastPosition, direction);

            bool  collision    = false;
            float?intersection = ray.Intersects(transSphere);

            if (intersection != null)
            {
                if (intersection <= distanceCovered)
                {
                    collision = true;
                }
            }

            return(collision);
        }
Beispiel #3
0
        private bool CoarseCheck(Model model1, Matrix world1, Model model2, Matrix world2)
        {
            BoundingSphere origSphere1 = (BoundingSphere)model1.Tag;
            BoundingSphere sphere1     = XNAUtils.TransformBoundingSphere(origSphere1, world1);

            BoundingSphere origSphere2 = (BoundingSphere)model2.Tag;
            BoundingSphere sphere2     = XNAUtils.TransformBoundingSphere(origSphere2, world2);

            bool collision = sphere1.Intersects(sphere2);

            return(collision);
        }
Beispiel #4
0
        void LoadBoundingSphere()
        {
            BoundingSphere completeBoundingSphere = new BoundingSphere();

            foreach (ModelMesh mesh in _model.Meshes)
            {
                BoundingSphere origMeshSphere  = mesh.BoundingSphere;
                BoundingSphere transMeshSphere = XNAUtils.TransformBoundingSphere(origMeshSphere, _boneTransforms[mesh.ParentBone.Index]);
                completeBoundingSphere = BoundingSphere.CreateMerged(completeBoundingSphere, transMeshSphere);
            }
            _model.Tag = completeBoundingSphere;
        }
Beispiel #5
0
        public static Model LoadModelWithBoundingSphere(string asset, ContentManager content)
        {
            Model newModel = content.Load <Model>(asset);

            Matrix[] modelTransforms = new Matrix[newModel.Bones.Count];
            newModel.CopyAbsoluteBoneTransformsTo(modelTransforms);

            BoundingSphere completeBoundingSphere = new BoundingSphere();

            foreach (ModelMesh mesh in newModel.Meshes)
            {
                BoundingSphere origMeshSphere  = mesh.BoundingSphere;
                BoundingSphere transMeshSphere = XNAUtils.TransformBoundingSphere(origMeshSphere, modelTransforms[mesh.ParentBone.Index]);
                completeBoundingSphere = BoundingSphere.CreateMerged(completeBoundingSphere, transMeshSphere);
            }
            newModel.Tag = completeBoundingSphere;

            return(newModel);
        }