views:

1004

answers:

4

Hi, I want to prototype an idea for a game I have. The idea for this game is that the player will dig through the ground, creating tunnels and finding treasure.

I'm looking to create 'worms style' terrain, with collision detection for the player wandering and jumping around the tunnels. Examples of this type of dynamic terrain can be seen in these pictures. Example 1 Example 2

My question is how is the best way to implement this type of destructible terrain? I am using XNA game studio.

Thanks, James.

+13  A: 

There's some nice code at the bottom of the this thread. Also, this is a great step-by-step tutorial on achieving a destructible terrain.

keyboardP
If I could vote you up any more I could. Great answer!
I remember trying to achieve the same thing when I was messing around with XNA, so I just had to try and find this link in my bookmarks (which really needs organising at some point :) )
keyboardP
+1 for linking to gamedev :)
zebrabox
Any game query, Gamedev is the key :D
keyboardP
+2  A: 

There are two possibilties, eighter one work, but it depends on XNA how easy it is to implement:

  • Like others suggested, bitmap masking. You keep a bitmap of all "ground" pixels and all "air" pixels. If you have gravity; a character can't fall through ground, while it can fall through air.
  • Vector geometry. You start with a rectangular "ground", and hold a list of "removed" ground, which creates air. This geometry is very simple to edit: just append a circle or similar to the list, and update the graphics. I don't know whether XNA has vector & shape abilities; but it should be easy to create geometry with these two things, and later apply a texture or similar. Also, this can be easy to optimize, by keeping a cached version of the rendered landscape, and only update the cache of the bounding-box of the newly added "air". This solution requires a bit more math though.
Pindatjuh
My original idea was to try vector geometry. Triangulating the geometry so I can draw it using the graphics card is a bit of a pain, though.
+1  A: 

Can't resist posting classic Dig Dug here. This is not an answer but merely another (really old circa 1982) example of destructible terrain.

Dig Dug

alt text

John K
On the C=64 port, at least, the Pookas and Fygars were sprites, and the terrain was multicolor bitmap mode (160x200), thus enabling the use of the VIC-II's collision detection ... I'm not sure if XNA offers similar functionality ...
Ken
C=64 makes me always say +1... but heck, it's not an answer :(
Pindatjuh
+5  A: 

Riemer has a similar approach to a game he developed in XNA, cannon shells cause the terrain to be "destroyed" and any props that were previously on said terrain are moved accordingly.

It needs to be refactored and improved for your needs but it's a start.

Riemer has a good selection of tutorials for other things too.

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2D/Adding_craters.php

Jamie Keeling