2.5d地图与玩家碰撞

问题描述:

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

我原本计划有一个Rectangle数组,每次调用Update()函数时都会检查它以检查精灵是否与它们发生碰撞,但其中一些形状是方式太复杂了。有另一种方法我应该做碰撞检测?因为这种方式在大规模上似乎不可行。2.5d地图与玩家碰撞

您可以根据对象使用不同种类的边界形状。简单地让他们都实现一个共同的接口:

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为边界层次早早放弃的对象。

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

+0

是否可以通过一组顶点定义多边形? – jae 2013-04-10 13:52:36

+0

当然,只是相应地设计你的交集方法。搜索*矩形多边形交集*或类似的东西。 [点多边形算法](http://en.wikipedia.org/wiki/Point_in_polygon)也可能是你感兴趣的。 – Lucius 2013-04-10 14:55:52