Destructable terrain last step

Started by
2 comments, last by dpadam450 12 years, 5 months ago
Hello everyone. I am working on a scorthed earth sorta clone in my free time and the last step I need to get working is the destructable terrain. The way I am doing this is I use RenderTarget2D to render the terrain and overlay a crater image. My terrain is black and everything else is transparent. My crater(round part) is white. The problem I am having is I know how to overlay the textures but I cannot seem to wrap my head around the last step of changing all of the white pixels of the crater to transparent in the new texture.

Here is the code I am using to create the new texture where terrain is the object containing and responsible for rendering the terrain, and crater is the object containing and responsible for rendering the crater.



// Render to texture the crater

this.crater.Position = position;

RenderTarget2D renderTarget = new RenderTarget2D(device, this.terrain.Texture.Width, this.terrain.Texture.Height);

device.SetRenderTarget(renderTarget);

device.Clear(Color.Transparent);

// first pass set the collision point

SpriteBatch spriteBatch = new SpriteBatch(device);

//spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.LinearClamp, DepthStencilState.Default,

// RasterizerState.CullNone);

spriteBatch.Begin();

this.terrain.render(spriteBatch);

this.crater.render(spriteBatch);

spriteBatch.End();

// second pass make the collision point transparent

?

this.terrain.Texture = (Texture2D)renderTarget;

device.SetRenderTarget(null);

this.textureColourData = TextureUtils.getColourData2D(this.terrain.Texture);

}


Here is the resulting image after the terrain has been hit twice.

exampleeb.png

Uploaded with ImageShack.us

If anyone could point me in the direction of the second pass for changing these pixels to transparent that would be great. Please note I also tried just manually going through the textures Color data but this is noticeably slow.

Any help would be great.
Thanks.


Remember to mark someones post as helpful if you found it so.

Journal:

http://www.gamedev.net/blog/908-xxchesters-blog/

Portfolio:

http://www.BrandonMcCulligh.ca

Company:

www.gwnp.ca

Advertisement
Seems like there should be a bit-wise logical operation to do that...
I found a simpler approach that doesn't use the render target. Thank's for taking a look at the topic anyway. Here is the solution that popped into my head, it is simple and quite basic.

First I cache a list of points in relation to the center of the crater;

this.distancesFromMiddle = new List<Vector2>();
Texture2D crater = LoadingUtils.loadTexture2D(content, "Crater");
Color[,] colours = TextureUtils.getColourData2D(crater);
for (int y = 0; y < crater.Height; y++) {
for (int x = 0; x < crater.Width; x++) {
if (colours[x, y] == Color.White) {
this.distancesFromMiddle.Add(new Vector2(x - 32f, y - 32f));// half of the textures size so we render at the origin
}
}
}


Than on impact I do this;

int tcdW = this.textureColourData.GetUpperBound(0) + 1;
int tcdH = this.textureColourData.GetUpperBound(1) + 1;
Vector2 pixelPosition;
for (int i = 0; i < this.distancesFromMiddle.Count; i++) {
pixelPosition = Vector2.Add(position, this.distancesFromMiddle);
if (pixelPosition.X >= 0 && pixelPosition.X < tcdW && pixelPosition.Y >= 0 && pixelPosition.Y < tcdH) {
this.textureColourData[(int)pixelPosition.X, (int)pixelPosition.Y] = Color.Transparent;
}
}
// flatten the array
Color[] colours = new Color[tcdH * tcdW];
for (int x = 0; x < tcdW; x++) {
for (int y = 0; y < tcdH; y++) {
colours[x + y * tcdW] = this.textureColourData[x, y];
}
}
this.terrain.Texture.SetData<Color>(colours);


So basically I am caching a set of points in relation to the origin of the crater and than on impact I am applying that position to the collision point.

Remember to mark someones post as helpful if you found it so.

Journal:

http://www.gamedev.net/blog/908-xxchesters-blog/

Portfolio:

http://www.BrandonMcCulligh.ca

Company:

www.gwnp.ca

I just wrote a tutorial on creating scorched earth. My approach is this:

Put depth testing on.
Clear screen black.
Draw the terrain with the cutout alpha sky. Depth = 1
Draw all your collision holes in black with Depth = 1000 (must put depth test to GREATER) so that the 1000 parts overwrite the 1 parts.
This effectively makes all the explosion hole pixels act as if they were never written to because they will be overwritten by:
Draw sky depth = 2. All the hole points with 1000 depth get overwritten by sky with 2 depth.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

This topic is closed to new replies.

Advertisement