2014-02-06 43 views
0

我试图制作一个玩家碰撞系统,玩家将与屏幕上绘制的物体发生碰撞。我使用基于像素的系统,我创建了4个矩形,顶部,底部,左侧和右侧。当它们中的任何一个碰撞该图像时,公式将检查矩形和块是否相交,如果是,则颜色相交。玩家运动,更新速度太慢?

现在这个系统,我用的是这样我可以创建任何类型的图像,这将具有使互动地图开展工作。让我举个例子此块在中心有一个孔:

Black box with a hole in the center

现在,如果玩家是块玩家仍然会与即使玩家在矩形内的块碰撞的内部。

但是有一个问题。

当我使用测试地图在黑盒子的思想工作的一些侧面板形式,善良的。玩家确实停下来,并且可以在玩家仍然在矩形内时自由移动,但是玩家在他停下之前稍微下沉到该区域。

*我如何解决这个问题?

*我想不能用矩形碰撞到类型的系统和使用它。有没有更好的办法?

*

这是怎样的相交像素的工作的一个例子。使用播放器的底部矩形和测试图。

IntersectPixels需要两个矩形和两个彩色数据的方法;将底部矩形和玩家将碰撞的图像的矩形以及这两个图像的Color[]

public Rectangle bottomRectangle;

Rectangle mapRectangle;

Color[] mapColor;

Color[] bottomColor;

首先每个矩形必须有它内部的纹理工作。所以,需要有两个纹理:

Texture2D bottomTexture;

Texture2D mapTexture;

然后通过公式在puting每幅图像的纹理和色彩的LoadContent()内。

Color = 
    new Color[Texture.Width * Texture.Height]; 
Texture.GetData(Color); 

然后,两个矩形和颜色后,有正确的设置了IntersectPixels方法:

static bool IntersectPixels(Rectangle rectangleA, Color[] dataA, 
           Rectangle rectangleB, Color[] dataB) 
    { 
     // Find the bounds of the rectangle intersection 
     int top = Math.Max(rectangleA.Top, rectangleB.Top); 
     int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom); 
     int left = Math.Max(rectangleA.Left, rectangleB.Left); 
     int right = Math.Min(rectangleA.Right, rectangleB.Right); 

     // Check every point within the intersection bounds 
     for (int y = top; y < bottom; y++) 
     { 
      for (int x = left; x < right; x++) 
      { 
       // Get the color of both pixels at this point 
       Color colorA = dataA[(x - rectangleA.Left) + 
            (y - rectangleA.Top) * rectangleA.Width]; 
       Color colorB = dataB[(x - rectangleB.Left) + 
            (y - rectangleB.Top) * rectangleB.Width]; 

       // If both pixels are not completely transparent, 
       if (colorA.A != 0 && colorB.A != 0) 
       { 
        // then an intersection has been found 
        return true; 
       } 
      } 
     } 

     // No intersection found 
     return false; 
    } 
+1

这里的问题究竟是什么? – bmm6o

+0

另外,http://sscce.org/ – bmm6o

+0

我重申了我的问题并简化了它,对于最后一篇文章的极端长度,我很遗憾,并希望能够将其降低。我最近才了解到,碰撞检测和碰撞响应存在差异,这可能是为什么玩家在撞地后停止需要更长的时间。 –

回答

0

一种解决方案是首先要检查碰撞,然后更新位置和借鉴。通过这个,你可以在发生碰撞之前检查碰撞的一个更新位置,你可以在这种情况下防止更新位置...设置速度0或其他任何东西。