XNA 3D边界框碰撞检测方法

时间:2013-12-28 18:03:37

标签: c# 3d xna collision-detection bounding-box

我已成功为我的3D游戏创建了一个边界球体碰撞方法。它工作得很好,但是当我想检查大平地板上的碰撞时,它会产生一个无法工作的巨大球体。对于像这样的对象,我需要一个边界球碰撞方法。我试着把它抬起来,什么也没找到。然后我尝试通过遵循与边界球碰撞方法相同的轮廓来创建我自己的,但这也不起作用。有没有人有一个边界框碰撞方法,最好类似于边界球?谢谢!

    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;

    }

1 个答案:

答案 0 :(得分:1)

地板可以是Microsoft.Xna.Framework.Plane对象。那么您可以使用以下Xna框架方法来检查任何BoundingSphere和楼层平面之间的碰撞:http://msdn.microsoft.com/en-us/library/bb197613.aspx