Game Programming and Development Tools

destructible terrain – dXter

dXter

Member

Posts: 59
From: Texas, the US of A
Registered: 09-26-2006
Is there any "best" way to make destructible terrain like in Worms? I looked for articles and stuff on the web but I didn't find very much on this topic
I found two methods, though:

1) to make the terrain and background made up of large tiles, say 128x128, and when there's an explosion, all tiles that touch the explosion divide into 4 parts, making 4 64x64 tiles. The smaller tiles that are still touching the explosion divide further and the process repeats until you have very small tiles that are touching the explosion. These are then destroyed.

2) to have a huge array holding all the pixels, and when there's an explosion, all pixels affected are changed.

I like the first method better, but are there any other ways of doing it, maybe better ways?

------------------
Jesus looked at them and said, "With man this is impossible, but with God all things are possible."
--Matt. 19:26

"Time is an excellent teacher, but eventually it kills all of its students."

Lava
Member

Posts: 1905
From:
Registered: 01-26-2005
2D Terrain right?

A 3rd method could be make the ground like a sprite, and then make different frames of what the ground will look like, like when it's destroyed alot or specific areas.

------------------

dXter

Member

Posts: 59
From: Texas, the US of A
Registered: 09-26-2006
Yeah, but I mean code-generated terrain destruction, like taking out a chunk of dirt where there was an explosion. [url]http://www.deviantart.com/deviation/39769589/[url] shows exactly what I'm talking about.

------------------
Jesus looked at them and said, "With man this is impossible, but with God all things are possible."
--Matt. 19:26

"Time is an excellent teacher, but eventually it kills all of its students."

Cohort X

Member

Posts: 126
From: The Great Pacific Northwest
Registered: 09-16-2006
Make two pictures . a background and a terrain picture. wherever you have an explosion slap down a picture of a circle the same color as the background. making new ground able two be walked on might be a bit of a challenge with this method.
CoolJ

Member

Posts: 354
From: ny
Registered: 07-11-2004
Maybe store a bit mask of the non-terrain(open area) and open area created by destoying terrain. Then you could use this mask for both bounds checking and XORing with the terrain to remove your destructed terrain.
HanClinto

Administrator

Posts: 1828
From: Indiana
Registered: 10-11-2004
Games like Worms, Liero, Gusanos, and other 2d games with destructible terrain often have a single bitmap image that is the "collidable ground". This is usually the dirt layer. The worms all use something called "pixel-perfect collision detection", which lets the worms follow the terrain of the ground on a pixel-by-pixel basis. To deform the terrain, you can either paint on it (to add ground when generating a level), or you can clear it (like when there's an explosion) -- usually by setting to a keyed color or by nixing the alpha channel.

Does this make sense? I'm happy to explain it more, but this is a basic overview.

--clint

dXter

Member

Posts: 59
From: Texas, the US of A
Registered: 09-26-2006
Thanks clint, I'd seen pixel perfect collision detection in the GTGE game engine but I didn't really know what it was. I was just wondering, when the worms are walking, do they check for a steep slope that makes them stop by checking if there are a certain number of vertical pixels right in front of them, like say 2 or 3? Sorry, I don't know anything about this kind of stuff.

------------------
Jesus looked at them and said, "With man this is impossible, but with God all things are possible."
--Matt. 19:26

"Time is an excellent teacher, but eventually it kills all of its students."

Realm Master

Member

Posts: 1971
From: USA
Registered: 05-15-2005
GAA! I posted something like Clint's repsonse (LIKE, Not CLint's response) but it didn't show And I don't want to type it again...

------------------
yeah, im a little crazy

Awaiting Avatar Image Size rechange...

HanClinto

Administrator

Posts: 1828
From: Indiana
Registered: 10-11-2004
quote:
Originally posted by dXter:
I was just wondering, when the worms are walking, do they check for a steep slope that makes them stop by checking if there are a certain number of vertical pixels right in front of them, like say 2 or 3?

Yup! You're exactly right! Good job figuring that out.

Basically, when you do test rect-hits to determine if you move or not, you'll move your test rectangle incrimentally up to see if you can move there.

Bah, it's hard to explain with a description, here's some pseudocode:


// PlayerSprite is the player's sprite image for this example.
// Even though the ground uses pixel-perfect collision,
// we use a simple bounding box for the player's sprite.

dX = 0;

if (MoveRight) {
dX = 1;
} else if (MoveLeft) {
dX = -1;
}

if (dX != 0) // If we're trying to move...
{
canMove = false;

// Make a copy of the player's rectangle for casting about for test collisions
HitTestRect = new Rect(PlayerSprite.Rect);

HitTestRect.X += dX;

maxSlope = 5;
for (int cnt = 0; cn < maxSlope; cnt++) {
if (TestForGroundCollision(HitTestRect) == false) {
// If our test rectangle doesn't hit anything, then it's clear to move.
canMove = true;
break; // Break out of the for loop so that we don't need to continue hit-testing.
} else {
HitTestRect.Y += 1;
}
}

if (canMove) {
// Move the player if the coast is clear.
PlayerSprite.Rect = HitTestRect;
}
}

I hope that's readable pseudocode, but that might give you some clue as to how something like this would work out in a real situation. Feel free to ask for any clarifications or follow-ups -- I really enjoy games with deformable terrain.

Cheers!

In Christ,
clint

quote:
Originally posted by Realm Master:
GAA! I posted something like Clint's repsonse (LIKE, Not CLint's response) but it didn't show And I don't want to type it again...

Bummer. I hate it when that happens. :-\

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
I coded one which did created the 'terrain' as a big array. I did this like 2-3 years ago, and it started to run like crap over time (obviously ). But I went back and looked at it about 3 months ago and checked out the code. It wasn't at all optimized. So I spent 1 hour fixing it up and speeding it it, and it now runs fine. If you want I can get you a demo in a day or too... PM me your email (MSN would be prefered as then I could chat with you), and then I'll email the demo to you.

It's in C/C++ and OpenGL. You move the mouse around and click on the terrain, and it'll go and delete a circle out of the terrain, and the terrain will fall until it collides with other terrain, or with the ground.

Using an array isn't too bad, if you can speed the rest of it up, it'll be fine... I would most likely do it now by having the terrain as a bitmap. And then when you click on it, you delete (make them a certain 'colour key' colour, which you'll use as a sort of 'alpha') those pixels. You could check over all the pixels in the bitmap on each update and if they can move down one pixel (if the pixel below them is that colour key), then move it down one pixel (change the colour of the pixel below the current pixel) and then set the pixels previous position (up one) to the colour key value.

------------------
www.auran.com

dXter

Member

Posts: 59
From: Texas, the US of A
Registered: 09-26-2006
quote:
Originally posted by HanClinto:
Yup! You're exactly right! Good job figuring that out.

Wow.... I actually guessed something right about this kinda stuff for once. Usually when I try to figure out something like this and then check in a book or online to see if I'm right it goes into this whole other thing and says that other, simpler methods (the kind that I think of) are inaccurate or slow or something. Thanks for the explanation, Clint

------------------
Jesus looked at them and said, "With man this is impossible, but with God all things are possible."
--Matt. 19:26

"Time is an excellent teacher, but eventually it kills all of its students."

Lazarus

Member

Posts: 1668
From: USA
Registered: 06-06-2006
quote:

Wow.... I actually guessed something right about this kinda stuff for once. Usually when I try to figure out something like this and then check in a book or online to see if I'm right it goes into this whole other thing and says that other, simpler methods (the kind that I think of) are inaccurate or slow or something.

I know exactly what you mean! That happens to me all the time.

Laz

buddboy

Member

Posts: 2220
From: New Albany, Indiana, U.S.
Registered: 10-08-2004
yeah, happens to me like all the time too.

------------------
that post was really cool ^

|
[|=D) <---|| me

Jari

Member

Posts: 1471
From: Helsinki, Finland
Registered: 03-11-2005
Maybe you could learn from a source of the clonk planet. It uses something called "mass mover" for moving water and sand particles and should have all the code you need. The latest version of it is not free but the version before that is.

------------------
1Jo 2:9-10 He that saith he is in the light, and hateth his brother, is in darkness even until now. 10 He that loveth his brother abideth in the light, and there is none occasion of stumbling in him.
Joh 17:26 And I have declared unto them thy name, and will declare it: that the love wherewith thou hast loved me may be in them, and I in them.

[VoHW] (Help needed) [Blog] - Truedisciple (mp3)