2
votes

I'm just starting game development and I thought a game like Tank wars or Worms would be nice. The hardest part I can think of so far is making the terrain destructible and I want to know how it's done before doing the easy parts.

I thought that explosion could have a mask texture which could be scaled for different weapons. Then using that mask I should make underlying terrain transparent (and optionally draw a dark border).
(source: mikakolari.fi)

How do I achieve that? Do I have to change the alpha value pixel by pixel or can I use some kind of masking technique? Drawing a blue circle on top of the terrain isn't an option.

I have versions 3.1 and 4.0 of XNA.

3
If you'll make the texture transparent and you'll use some kind of collision detection, your objects will probably float somewhere in the air in that particular place.Adrian Fâciu
I can use per pixel collision detection against nontransparent pixels.Mike Koder

3 Answers

1
votes

This is a difficult question to answer - because there are many ways you could do it. And there are pros and cons to each method. I'll just give an overview:

As an overall design, you need to keep track of: the original texture, the "darkness" applied, and the "transparency" applied. One thing I can say almost for sure is you want to "accumulate" the results of the explosions somewhere - what you don't want to be doing is maintaining a list of all explosions that have ever happened.

So you have surfaces for texture, darkness and transparency. You could probably merge darkness and transparency into a single surface with a single channel that stores "normal", "dark" (or a level of darkness) and "transparent".

Because you probably don't want the dark rings to get progressively darker where they intersect, when you apply an explosion to your darkness layer with the max function (Math.Max in C#).

To produce your final texture you could just write from the darkness/transparency texture to your original texture or a copy of it (you only need to update the area that each explosion touches).

Or you could use a pixel shader to combine them - the details of which are beyond the scope of this question. (Also a pixel shader won't work on XNA 4.0 on Windows Phone 7.)

3
votes

This tutorial is what you are searching: http://www.riemers.net/eng/Tutorials/XNA/Csharp/series2d.php

Capter 20: Adding explosion craters

In short: You have 2 textures: 1 Color Texture (visible), 1 Collision Texture (invisible) You substract the explosion image from your collision texture. To get the dark border: expand the explosion texture and darken the color in this area. Now you generate a new Color Texture (old color - collison = new color).

0
votes

You should Make a new Texure2D with the Color of desired pixels.Alpha = 0.

Color[] bits = new Color[Texture.Width * Texture.Height];
Texture.GetData(bits);

foreach(Vector2D pixel in overlapedArea)
{
   int x = (int)(pixel.X);
   int y = (int)(pixel.Y);

   bits[x + y * texture.Width] = Color.FromNonPremultiplied(0,0,0,0));
}

Texture2D newTexture = new Texture2D(texture.GraphicsDevice, texture.Width, texture.Height);
newTexture.SetData(bits);

Now replace the new Texture2D with the Last Texture and you're good to go!

For more code about Collision, or changing texture pixels color go to this page for codes: http://www.codeproject.com/Articles/328894/XNA-Sprite-Class-with-useful-methods