Sign in to follow this  
sofakng

How can I create destructible 2D terrain like Worms / Scorched Earth?

Recommended Posts

sofakng    102
I'd like to create a Worms/Scorched Earth clone and I'm not sure how do to the destructible terrain. The game will be in 2D so I'm sure that makes it easier but I still don't really know how to do it... One suggestion I've received is to create a quad tree and then modify it on-the-fly as collisions take place. One requirement I have is being able to tunnel through the terrain. That means I can't juse use a bunch of textured quads side-by-side (without getting more complicated I guess?) So does anybody have any suggestions for me? I'd like to textured my terrain using a 4000x1600 image and I'm not sure if I will need to split up that texture before displaying it. (eg. it will be overlayed onto my terrain, etc)

Share this post


Link to post
Share on other sites
Lord_Evil    680
One option would be to use the texture as the terrain and update that texture via render-to-texture or keep a copy in memory and update changed portions with glTexSubImage2D. You can then use the alpha channel or an additional texture (mask) to display the terrain and do collision detection.

For collision detection you might use a quad-tree as you mentioned or do something similar to hierarchical z-culling. That would involve having different LODs for the map (i.e. mipmaps) where each pixel represents the highest alpha / mask value (assuming a mask of 0 means 'air' and higher values mean 'more solid) from the corresponding pixels of the next finer level. Then you start at the highest LOD, check the value and go deeper if the value means 'solid'. Repeat that until you have reached the lowest (finest) level or you get a value of 0 or nearly 0 (depends on how you'd handle collisions and different 'air' types).

With this you could define different types of passable terrain ('air') like plain air, water, ectoplasma [smile] etc.
Would be cool to see a Worms-like game where you shoot your bazooka right through some ectoplasma and see some cool splatter effects if it explodes inside [rolleyes].

Share this post


Link to post
Share on other sites
Iftah    413
I think Scorched Earth used a "height map" or "voxel" style solution -
for each pixel column there was a ground height and a vertical line would be drawn.

This simple and memory effective approach will need serious extension to fit with the more modern features.

With todays memory power you can create a full bitmap for the terrain. I don't know about the performance you'd get from a 10000x10000 pixels bitmap - but it can fit in memory of modern computer so I think will be fine. With this collision checking is trivial and morphing is easy.

In worms, since they draw an outline around the terrain (to fit with their cartoon like style) I bet the somehow have polygons for the terrain, the modifying quad tree idea sounds right.

Share this post


Link to post
Share on other sites
tomhog    145
You could do it using polygons and boolean operations. I wrote a very simple (quite slow) 3d destructable terrain for my disertation at uni. I wrote a paper on the basic concept of using boolean operations on triangle. It has some good 2d example digrams that will help.

Basicaly you have a poly for the terrain with you texture applied. Then use damage polys to subtrat from the terrain poly and then update the texture coord to fit the new terrain poly.

you can get my source and disertaion from www.hogsthatroam.co.uk
The source is in the downloads section and the disertaion is in the about me section


Hope it helps.

Ive been thinking of doing a 2d one. It would be alot simpler and faster then my 3d one

Share this post


Link to post
Share on other sites
ToohrVyk    1596
Pixel maps? Large consumption of memory, which might be negligible depending on what else your game does.
Polygon approximation? Memory-friendly, but difficult to write and quite slow.

There are some solutions available from the rasterization world, such as C-Buffers, which reduce memory consumption greatly (from quadratic to linear) without the modification overhead of polygons. Using this, it's a breeze to draw only enough pixels to fill the screen, and nothing else.

Share this post


Link to post
Share on other sites
sofakng    102
How many polygons would I need for the terrain? Just one? I don't think I want the terrain to have "islands" (eg. detached terrain), so that would mean all parts of terrain would be attached to more terrain and could be represented by one huge polygon (right?).

Can anybody provide more information (eg. details) on doing this? I'm new to OpenGL and game programming :(

(...but I've been programming with Delphi, Java, etc, for several years)

Share this post


Link to post
Share on other sites
Lord_Evil    680
You should use a triangulated terrain instead of a single polygon but I second ToohrVyk:
Quote:
Polygon approximation? Memory-friendly, but difficult to write and quite slow.

Share this post


Link to post
Share on other sites
sofakng    102
By "triangulated terrain" do you mean have the terrain be composed of a bunch of triangles? Can you give me an example of what this might look it? (eg. how the triangles would be combined to form the terrain)

Also, what is a "polygon approximation"?

(sorry for the newbie questions.......)


Thanks!

- John

Share this post


Link to post
Share on other sites
shurcool    439
Quote:
Original post by sofakng
By "triangulated terrain" do you mean have the terrain be composed of a bunch of triangles? Can you give me an example of what this might look it? (eg. how the triangles would be combined to form the terrain)

Well, the main idea is that OpenGL can not render concave polygons. If your terrain is represented as a bunch of polygons, most likely they will all be concave (sooner or later, if you modify them). So you'd have to triangulate them, ie. convert to a set of triangles that represent the area covered by those polygons. That way you can render your 2D terrain filled in (to render just the outline of the terrain, you wouldn't need to do this).

But, there are many difficuilties in using this technique. It's definitely harder to code and probably slower than using a big 'bitmap' to represent your terrain, like previously said. I'd recommend you go with that approach first, if you want to worry about making the game, and not about polygons.

PS. If you do go with the polygon approach, I'd recommend using a library to perform boolean operations on the terrain to modify it (like "general polygon clipping" library by Alan Murta or PolyBoolean), unless you're willing to tackle this complicated subject yourself. Again, if your focus is creating a fun game with deformable terrain, don't go here.

Share this post


Link to post
Share on other sites
sofakng    102
Excellent resources, thank you!!

I'll try going the bitmap route and see how well that works out. If not, I'll try Alan Murta's polygon library it looks amazing! (it even has a port to Delphi which is what I'm using!)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this