2013-09-28 110 views
1

我有像素完美碰撞下来,但它只能在0弧度旋转质感的作品。这是我确定像素完美的碰撞 -像素完美碰撞纹理旋转

public static bool IntersectPixels(Texture2D sprite, Rectangle rectangleA, Color[] dataA, Texture2D sprite2, Rectangle rectangleB, Color[] dataB) 
{ 
    sprite.GetData<Color>(dataA); 
    sprite2.GetData<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; 
} 

我有我的麻烦发生碰撞时,旋转的代码。我将如何去检查像素碰撞与旋转的精灵?谢谢,任何帮助表示赞赏。

+0

你知道你可以借鉴的旋转纹理一个'RenderTardet2D',其转换为'Texture2D',然后核对你的现有的方法。只要知道它可能会拿走大量的CPU来做到这一点。我建议采用更高效,不同的方法,但我不知道。 – user1306322

+0

是的,这就是问题一旦当你载入纹理它不会,如果你将其旋转或缩放工作..检查矩阵旋转......可是,你真的NEET像素的碰撞?这是cpu杀手。更好地使用rectange,圆形甚至多边形collsion。 –

回答

0

理想情况下,你可以用exress矩阵的所有转换。这不应该是辅助方法的问题。如果使用矩阵,则可以轻松扩展程序,而无需更改碰撞代码。到目前为止,您可以使用矩形的位置来表示翻译。

假设对象A的转变transA和对象B transB。然后,您将迭代对象A的所有像素。如果像素不透明,请检查对象B的哪个像素位于此位置,并检查此像素是否透明。

棘手的部分是确定哪些对象B的像素是在给定位置。这可以通过一些矩阵数学来实现。

你知道,在对象A的空间中的位置首先,我们希望这个地方位置转换到屏幕上的全球地位。这正是transA所做的。在那之后,我们要变换的全局位置到本地位置的对象B的空间这是transB倒数。所以,我们要改变当地的位置。在与下面的矩阵:用

var fromAToB = transA * Matrix.Invert(transB); 
//now iterate over each pixel of A 
for(int x = 0; x < ...; ++x) 
    for(int y = 0; y < ...; ++y) 
    { 
     //if the pixel is not transparent, then 
     //calculate the position in B 
     var posInA = new Vector2(x, y); 
     var posInB = Vector2.Transform(posInA, fromAToB); 
     //round posInB.X and posInB.Y to integer values 
     //check if the position is within the range of texture B 
     //check if the pixel is transparent 
    }