2013-09-28 107 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 
    }