简体   繁体   中英

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

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:)

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

Personally I rather write extension methods:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM