Ceej100

Hi all

I am currently wondering how I could put a hole through a sprite "on the fly" during gameplay, such that the background behind will show through Some sort of blending, or combination in the spritebatch class I suppose, but I havn't yet worked out how.

Any suggestions

Chris J.



Re: XNA Game Studio Express Putting a hole in a sprite

The ZMan

If you modify the sprite by setting some of the pixels to have an alpha component then your sprite will draw with the 'hole'




Re: XNA Game Studio Express Putting a hole in a sprite

Ceej100

Thanks for your prompt reply.

I see what you mean... but I don't want to predefine where the hole would be. E.g. the game player could shoot the sprite anywhere on its surface. I want to put the hole where the target crosshairs are. Can I apply transparency to the pixels in the vicinity of the crosshairs during gameplay

Chris





Re: XNA Game Studio Express Putting a hole in a sprite

e. ogas

 The ZMan wrote:
If you modify the sprite by setting some of the pixels to have an alpha component then your sprite will draw with the 'hole'

This would only work if he had no transparent parts of the sprite except the hole.  If you do have tranparency in another part of the sprite, it can be done by switching between two different sprites (one with a hole one without) or by using one sprite with a hole and a second sprite filling in the hole, switching the second sprite on and off depending on whether the hole is closed or open.

EDIT: Sorry, you posted while I was posting.  For this I think you would either have to access the texture data or create a bitmap from it so you can edit the color values at the desired location.  You would change these values either to a color that is predetermined for transparency or an rgba color with alpha value reflecting just how transparent it will be.






Re: XNA Game Studio Express Putting a hole in a sprite

The ZMan

Yes you have to modify the texture data since thats where the pixel data is stored. Yes if you are modifying something that already has transarancy you will need to refer to the original if you want to set it back but it will work OK.

If you want to to it in a more general way you could write a pixel shader to do it on the GPU. Pass inthe cross hair location and modify the output data for the sprites.






Re: XNA Game Studio Express Putting a hole in a sprite

DSent

Actually, recently I've made an example on exactly what you described.

Here it is:
http://www.hot.ee/qstorage/AlphaTest2.rar

There are 2 different ways to do that:
1. With the separate mask file (you apply a predefined masking texture to another texture).
The advantage of this method is that you can make a hole of any shape (just edit the mask file).
2. Without the mask texture, just using some simple math functions
You'll probably want to use this one, because it's more efficient and you can change the radius.

In the example I linked they're called ApplyMask() and CircleCut() respectively.
PS: The example was made for Beta 2 so if you're using the final release of GSE you may have to change some references.





Re: XNA Game Studio Express Putting a hole in a sprite

DSent

I updated the example, it now should look a lot cleaner.

http://www.hot.ee/qstorage/AlphaTest3.rar





Re: XNA Game Studio Express Putting a hole in a sprite

Ceej100

Hi... I would very much like to see your solution, but following the link freezes my browser!! (ie 7.0)

thanks

Chris





Re: XNA Game Studio Express Putting a hole in a sprite

Bapa

Just go open up the sprite in paint, look at the colors at the bottom. Look for the first column of two purples. A darker one, and a lighter one in between the blues and yellows. Choose the purple in the middle, and paint all the pixels you want transparent with that color.

I use it all the time - certain pixel colors are called "transparent" pixels.

Note: After coloring the pixels, when you load up the sprite in your game, you have to use the ContentManager variable "content.Load<Texture2D>(dahtexchar.bmp);", if you load up the sprite another method. The purple won't show up as transparent pixels.

This solution seems to be simpler than all the others. No extra code, no mask layers :D





Re: XNA Game Studio Express Putting a hole in a sprite

DSent

Here's another link for you:
http://rapidshare.com/files/7427502/AlphaTest3.rar.html

Press the 'free' button at the bottom.
Hope it works...





Re: XNA Game Studio Express Putting a hole in a sprite

Ceej100

Thanks very much DSent...

I got your download to work... Very neat solution.

After my initial post I had homed in on the texture2d Getdata & Setdata methods also. Your use of them is elegant & illuminating. Have you noticed the behaviour when you target near the edge of the screen

Last night I also used GetData/SetData to generate a smaller texture by grabbing part of a larger texture. Previously I just mapped onto the larger texture. Now with a distinct, sub texture I can manipulate it more easliy (I want to shape its edges to mimic a jigsaw puzzle piece).

This XNA stuff is certainly exciting. Its also nice to have a commited forum. Thank you all for you replies.

Chris





Re: XNA Game Studio Express Putting a hole in a sprite

DSent

>>Your use of them is elegant & illuminating
Why, thank you! :)

>>Have you noticed the behaviour when you target near the edge of the screen
Oh yeah, that's because the code tries to crop a part of a texture that doesn't exist...could easily be avoided by putting it into try-catch block.

>>This XNA stuff is certainly exciting. Its also nice to have a commited forum.
I'm also new to XNA, and I have to agree with you, having a place where you can always find some help is a very good thing :).