Вопрос

I have successfully created a bounding sphere collision method for my 3D game. It works just fine, but when I want to check collision on a large flat floor, it creates a huge sphere that does not work. For objects like that, I need a bounding sphere collision method. I tried looking it up and came up with nothing. I then tried to create my own by following the same outline as the bounding sphere collision method, but that also did not work. Does anyone have a bounding box collision method that is preferably similar to the bounding sphere one? Thanks!

    private bool IsCollision(Model model1, Matrix world1, Model model2, Matrix world2)
    {
        Matrix gameWorldRotation = Matrix.CreateRotationX(MathHelper.ToRadians(RotationX)) * Matrix.CreateRotationY(MathHelper.ToRadians(RotationY));
        Matrix[] transforms1 = new Matrix[model1.Bones.Count];
        model1.CopyAbsoluteBoneTransformsTo(transforms1);
        Matrix[] transforms2 = new Matrix[model2.Bones.Count];
        model2.CopyAbsoluteBoneTransformsTo(transforms2);
        foreach (ModelMesh mesh1 in model1.Meshes)
        {
            Matrix meshWorld1 = gameWorldRotation * transforms1[mesh1.ParentBone.Index] * world1;
            foreach (ModelMesh mesh2 in model2.Meshes)
            {
                Matrix meshWorld2 = gameWorldRotation * transforms2[mesh2.ParentBone.Index] * world2;
                if (oldCollision(model1, meshWorld1 * Matrix.CreateScale(0.8f), model2, meshWorld2 * Matrix.CreateScale(0.8f))) return true;
            }
        }
        return false;
    }

    private bool oldCollision(Model model1, Matrix world1, Model model2, Matrix world2)
    {
        for (int meshIndex1 = 0; meshIndex1 < model1.Meshes.Count; meshIndex1++)
        {
            BoundingSphere sphere1 = model1.Meshes[meshIndex1].BoundingSphere;
            sphere1 = sphere1.Transform(world1);

            for (int meshIndex2 = 0; meshIndex2 < model2.Meshes.Count; meshIndex2++)
            {
                BoundingSphere sphere2 = model2.Meshes[meshIndex2].BoundingSphere;
                sphere2 = sphere2.Transform(world2);

                if (sphere1.Intersects(sphere2))
                    return true;
            }
        }
        return false;

    }
Это было полезно?

Решение

The floor could be a Microsoft.Xna.Framework.Plane object. then you could use the following Xna framework method to check collision between any BoundingSphere and the floor Plane: http://msdn.microsoft.com/en-us/library/bb197613.aspx

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top