How to get Color and coordinates(x,y) from Texture2D XNA c#?

Harry89pl picture Harry89pl · Mar 2, 2012 · Viewed 9.8k times · Source

What i'm trying to do is check perfect-pixel colision with 2 textures which have black edges for example: one of this texture is a circle the second one can be triangle or rectangle.

this my code which give me only array of color without coordinates which i need

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

and my question is how to get coordinates from Texture2D for each pixel which are Black in this Texture2D.

thanks for advance:)

Answer

Thaven picture Thaven · Mar 2, 2012

You already have array of colors, so only one you need is to determinate coordinate in 2D of each from pixels in your arrays.

in Riemers tutorial (which I recommend), it's done like that:

    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]; 
         }
     }