2012-03-02 92 views
2

我想要做的是檢查完美像素colision與2黑色邊緣紋理例如:其中一個紋理是一個圓圈,第二個可以是三角形或矩形。如何從Texture2D XNA c#獲取顏色和座標(x,y)?

這是我的代碼,給我的只有顏色的排列沒有它,我需要

Color[] playerColorArray = new Color[texturePlayer.Width * texturePlayer.Height]; 
Color[] secondColorArray = new Color[secondTexture.Width * sencondTexture.Height]; 
texturePlayer.GetData(playerColorArray); 
secondTexture.GetData(secondTextureArray); 

和我的問題是如何從的Texture2D獲取座標對於其在這的Texture2D黑色每個像素座標。

感謝提前:)

回答

4

你已經擁有的顏色陣列,所以只有一個你需要的是在每個2D到確定的座標從你的陣列像素。

Riemers tutorial

(我推薦),它的完成這樣的:

Color[,] colors2D = new Color[texture.Width, texture.Height]; 
    for (int x = 0; x < texture.Width; x++) 
    { 
     for (int y = 0; y < texture.Height; y++) 
     { 
      colors2D[x, y] = colors1D[x + y * texture.Width]; 
     } 
    } 
1

個人而言,我寧願寫擴展方法:

public static class Texture2dHelper 
{ 
    public static Color GetPixel(this Color[] colors, int x, int y, int width) 
    { 
     return colors[x + (y * width)]; 
    } 
    public static Color[] GetPixels(this Texture2D texture) 
    { 
     Color[] colors1D = new Color[texture.Width * texture.Height]; 
     texture.GetData<Color>(colors1D); 
     return colors1D; 
    } 
} 
+0

但我不得不說..即時通訊使用這生成從一個彩色圖像瓷磚地圖...但它不是很好....它的偏移量每行1寬度....沒有找到錯誤的地方... – Zerq 2014-06-07 20:32:43