2.5d地图与玩家的碰撞

时间:2013-04-10 11:40:07

标签: c# xna collision-detection

我打算制作一个2d自上而下的RPG,所以在开始之前我决定玩一些东西。举个例子,我从母亲那里拿了this map

  我最初计划有一个矩形数组,每次调用Update()时都要检查它以检查精灵是否与它们发生碰撞,但是有些形状太复杂了。还有其他方法我应该进行碰撞检测吗?因为这种方式在大规模上似乎不可行。

1 个答案:

答案 0 :(得分:1)

您可以根据对象使用不同类型的边界形状。只需让他们都实现一个通用的界面:

public interface IBoundingShape
{
    // Replace 'Rectangle' with your character bounding shape
    bool Intersects(Rectangle rect);
}

然后,您可以CircleRectanglePolygon全部实施IBoundingShape。对于更复杂的对象,您可以引入复合边界形状:

public class CompoundBoundingShape : IBoundingShape
{
    public CompoundBoundingShape()
    {
        Shapes = new List<IBoundingShape>();
    }

    public List<IBoundingShape> Shapes { get; private set; }

    public bool Interesects(Rectangle rect)
    {
        foreach (var shape in Shapes)
        {
            if (shape.Intersects(rect))
                return true;
        }

        return false;
    }
}

此外,您可以使用CompoundBoundingShape作为早期丢弃对象的边界层次结构。

在游戏中,您只需迭代所有游戏对象,并检查玩家的边界形状是否与风景相交。

相关问题