How to create a destructible Texture in XNA

Ever wonder how to create a destructible texture like the one in Worms games? Well, then you are in luck.
In this tutorial, i am going to show how to create a destructible texture2D, as shown in the video above.

The term destroy refers to setting the pixels to transparent.

The class used to manage this texture is called “DestructibeTexture2D”. The class will receive a texture as the constructor and will provide methods to destroy that texture based on 3 shapes: circle, rectangle and based on another texture.

In order to access the pixels from a Texture2D we have to use the method “GetData” whose parameter will include and Color object array which will be populated with pixels’ data. The next snippet shows how that’s done:

Texture2D texture  = Content.Load(textureName);
Color[] data = new Color[texture.Width * texture.Height];

Whenever we want  to set the pixels in the texture, we use the “SetData” method whose parameter is the Color object array.


Now, when we want to change the data of a pixel, we just need to change the Color array object corresponding to that pixel and the set the data in the texture. The next code snippet changes the color of the pixel in the third row and fourth column to green.

data[4 + 3*texture.Width] = Color.Green;
With this in mind it should be easy to change the color of any pixel to transparent. The next code snippet shows how set a rectangle in the texture to transparent.
public void DestroyTexture(Rectangle rectangle)
int areaLeft = Math.Max(0, rectangle.Left);
int areaRight = Math.Min(Texture.Width, rectangle.Right);
int areaTop = Math.Max(0, rectangle.Top);
int areaBottom = Math.Min(Texture.Height, rectangle.Bottom);
for (int i = areaLeft; i < areaRight; i++)
for (int j = areaTop; j < areaBottom; j++)
TextureData[i + j * Texture.Width] = Color.Transparent;

Pretty simple, hay? =)

This is all you need to know to create your own destructible texture.

You can download the d texture class and all the test project source code using the link below.

You should know that the class provides to implementation for each destruction method, one fast and another that receives in the parameter a bool that is set true if the texture changed or false if the texture did not.

I think that the code is  pretty much self-explanatory. Any question fell free to comment below ;).

Usage Example

Here is an example on how to use the destructible texture class.

//Load destructible texture
Texture2D textureTemp = Content.Load("Level");
DestructibleTexture2D destructibleTexture  = new DestructibleTexture2D(textureTemp, GraphicsDevice);
//Destroy texture in shape of a rectangle
Rectangle rectangle = new Rectangle(30, 30, 40, 20);

Source Code

(you can download the project full source code here.)

Interesting Links

Emanuel Feronato’s Destructible Terrain in Flash

Reimer’s resources


0 Responses to “How to create a destructible Texture in XNA”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Twitter Updates

Error: Twitter did not respond. Please wait a few minutes and refresh this page.