2012-03-02 124 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