2D Pixel Collision on 3D API Textures

Started by
4 comments, last by barakus 16 years, 3 months ago
Hello,I want to do 2D pixel collision on my game, but in 3D APIs, the texture 's width and height is not the same as the width and height of the source image . It's scaled to a power_of_two length by the loading function. Now I implement the pixel collision (and the pixel erase (DigDug 1 use this))with the non_power_of_two textures,but this feature is not supported by some graphic cards. I don't know how to do the pixel collision and erase in 3D api . I use XNA studio 2.0,which is based on 3D API. and the image should be loaded as textures . Is there any methods to do accurate pixel collision and pixel erase (DigDug1) in 3D api textures? I want your help. Thank you.
Advertisement
Disregarding the scaling on non power of two textures, you'd first get the color data like so:

Color[] texData = new Color[texture.Width * texture.Height];texture.GetData(texData);


If the there is a collision at the row and column your looking for, then you get the index of the pixel from a row and column and check if its transparent:

int index = (column * texture.Width) + row;if (texData[index].A == 0)    return false;


If you have applied a transformation to the object with the texture, just transform the checking position with an inverted Matrix of that transformation.

Hope that helps.
Thank you. I have implemented this.My code :
static bool intersectsPixel(Rectangle srcRect, Color[] srcData, Rectangle destRect, Color[] destData)        {                   int left, right, top, bottom;            left = Math.Max(srcRect.Left, destRect.Left);            top = Math.Max(srcRect.Top, destRect.Top);            right = Math.Min(srcRect.Right, destRect.Right);            bottom = Math.Min(srcRect.Bottom, destRect.Bottom);                       if (left >= right || top >= bottom)                return false;                       int i, j;            for (i = left; i < right; i ++)                for (j = top; j < bottom; j++)                {                                      if (srcData[(i - srcRect.X) + (j - srcRect.Y) * srcRect.Width].A != 0 && destData[(i - destRect.X) + (j - destRect.Y) * destRect.Width].A != 0)                        return true;                }                       return false;        }

static void erasePixel(Rectangle srcRect, Color[] srcData, Rectangle destRect, Color[] destData)        {                       int left, right, top, bottom;            left = Math.Max(srcRect.Left, destRect.Left);            top = Math.Max(srcRect.Top, destRect.Top);            right = Math.Min(srcRect.Right, destRect.Right);            bottom = Math.Min(srcRect.Bottom, destRect.Bottom);                        if (left >= right || top >= bottom)                return ;            Color zero = new Color(0,0,0,0);                        int i, j;            for (i = left; i < right; i++)                for (j = top; j < bottom; j++)                {                                       if (srcData[(i - srcRect.X) + (j - srcRect.Y) * srcRect.Width].A != 0)                        destData[(i - destRect.X) + (j - destRect.Y) * destRect.Width] = zero;                }        }


and my probelem is if I have a 32 * 32 sprite . and a 640 * 480 background, I use a 640 * 480 mask texture to mask the background (use stencil buffer),if the card doesn't support non_power_of_two texture, 640 * 480 mask texture will expend to 1024 * 512(but 640 * 480 on srceen). then the pixel collision and erase function will get wrong result. I can use scaling transform on pixel collision to make it seems right,but in the erase function it doesn't work well .

BTW: the code snippet box doesn't support chinese (character )comments :)
I am not sure what you could do here, but there is a tutorial on the creators xna website where they merge many non power of 2 textures into one power of 2 textures. It could help you out.

You can find it here.

Otherwise, just make sure your textures are power of 2 and leave the empty space blank. If you use a compressed format the additional size will be negligible.
Quote:Original post by barakus
I am not sure what you could do here, but there is a tutorial on the creators xna website where they merge many non power of 2 textures into one power of 2 textures. It could help you out.

You can find it here.

Otherwise, just make sure your textures are power of 2 and leave the empty space blank. If you use a compressed format the additional size will be negligible.


The tutorial helps me a lot .and the idea to leave the empty space blank helps me too. I solve the problem with your help. Thank you very much .
No worries, glad I could help :)

[Edited by - barakus on January 15, 2008 12:57:05 AM]

This topic is closed to new replies.

Advertisement