2D Destructible terrain

BlitzMax Forums/BlitzMax Programming/2D Destructible terrain

Yahfree(Posted 2010) [#1]
Hey, I'm interested in creating a game like Worm, or Scorched Earth.

I'm not interested in the terrain "falling" as terrain below it is destroyed, so hovering terrain is fine. The game would take place on a single screen(no scrolling, or wrapping), two players would take turns shooting each other. So I'm keeping this as basic as possible for now.


I found this:
http://www.blitzmax.com/Community/posts.php?topic=72736

And I think I understand the concept behind it. But what I don't understand is how these primitive colors turn into images. In other words, how do I texture this destructible terrain? Especially if I go this "tiled" route. Do I create a large pixmap "texture it" and then split it into tiles? For now, I only plan on having the terrain flat when starting. Should I just stick with one pixmap(because it's not going to be very large, just a rectangle below the players)?

Also how would collision work? How would I decide if terrain in front of the player is too steep to climb?


ImaginaryHuman(Posted 2010) [#2]
Keep doing searches on the forums for posts containing terms like destructible, worms, landscape, scorch, fractal, etc


Yahfree(Posted 2010) [#3]
I have, and I find things explaining how the destructible terrain works, not how it's rendered though. (how images are drawn over it.)


plash(Posted 2010) [#4]
An easy way to do dynamic terrains is to use a pixmap (you have a super fast collision system with Max2D and you only have to convert the pixmap to an image when a part of it is destroyed; addition/removal from the terrain is also really fast and simple, if done correctly).

I wrote a somewhat iffy implementation using pixmaps for osmax (now dead), which used OpenGL framebuffers to draw textures (unnecessary, really, but it was just a placeholder) onto the pixmap (map decoration).
It should be really easy to do with proper area-deletion on the pixmap (it was rather rudimentary in osmax).


Yahfree(Posted 2010) [#5]
Okay.. Well so far I have this for taking rectangular chunks out of an image:



How would I go about taking circle chunks out? I need to find all pixels within R distance of point x,y. How do I do this?


plash(Posted 2010) [#6]
First off, you should implement an interface to the pixmap to remove pixels from the pixmap by using the pixel pointer (it's much, much faster this way, especially if you do it in chunks, see below). In osmax we generated an array of lines for terrain (de)construction from images (on the hard drive) - which would then be used to either add or remove from the terrain pixmap (again, using pointers to set the pixels).

Here is the osmax code. It has a flawed implementation for pixel assignment, but you should be able to get the general idea from it.


therevills(Posted 2010) [#7]
Hi Yahfree... I knocked this up using matibee's code:

http://www.blitzbasic.com/Community/posts.php?topic=89848#1021308


Yahfree(Posted 2010) [#8]
Genius! I'll take a look.


_Skully(Posted 2010) [#9]
I wish the TileMAX TileSystem was ready... I'd give it to you to run with... It already has destructible terrain. The TileMap references an original tile until it is modified.. then becomes a unique tile. Unique tiles get stored in a cache file if you save.

Ho hum... soonish.


Yahfree(Posted 2010) [#10]
Sounds cool, I'll be sure to check it out when it's released.

Therevills:
Everything seems to work except:

Global terrainshape:hshape = hshape.GenShape("Media/terrain.png")
terrainshape.Position(0, 384)

my terrain image is 1024x384, (half of the screen) so I want to lower it by half the screen(384) but when I use the above code, it seems to erase the image.


therevills(Posted 2010) [#11]
Genius!

Matt is! ;)

but when I use the above code, it seems to erase the image.



Ive altered the code to fix this issue:

terrainLines.bmx



beanage(Posted 2010) [#12]
Would it be effective to implement something like a Sparse Pixel Quadtree? You know, the same magic they are doing with Sparse Voxel Octrees, just 2D? If you find that as fascinating as I do, why not try a prototype implementation? If not.. oh well.. just ignore my comment :)


Yahfree(Posted 2010) [#13]
Okay two questions:

1.) How would you go about randomly generating textured landscapes?
2.) When adding "dirt"(I have a weapon that drops a wall of dirt) the line segments stack... is there a way to combine these and make it more efficient?

Here's my code for adding a wall:
	Method Detonate()
		Select Weapon
			Case 1
				New TExplosion.Create(x + 2, y + 2)
			Case 2
				For Local ty:Int = y - 100 To y
					terrainshape.AddLine(x - 5, x + 5, ty, True)
				Next
		End Select
	End Method


I also added a true/false value to hline so I can tell if the line was part of the original landscape or not. If it's not, for now I have it drawn in white. How would I texture these lines?

BeAnAge:
I don't know what a "Quadtree" is.. I don't know what "Sparse" means in that context either. I only know the middle terms "Voxel", and "Pixel"

EDIT: research showed this:

http://www.torquepowered.com/community/forums/viewthread/56363

Seems to be interesting.


Yahfree(Posted 2010) [#14]
This quadtree stuff is interesting, I'll run some speed tests.

Here's a Quadtree object I wrote:


Now I'll check out how to do pixel checks on a pixmap. Then I believe the idea is to:

1. Check each node, see what it's pixel contents are to create a Quadtree map of the level:
------1. If all solid, then mark as "solid"
------2. If all void, then mark as "empty"
------3. If the quadnode is mixed(contains both void and solid) then subdivide the node and check the subdivisions(repeat to pixel level if needed)
2. If a object is colliding with a "solid" node then it's colliding with terrain, otherwise it's not.

So then, my questions are: Would this be faster than the line method? Would it be possible to combine both methods? How would you punch circles out of the terrain, then recalculate the QuadNodes?


Yahfree(Posted 2010) [#15]
I'm working on generating a Quadtree for an image.

Basically, I need to cycle through the nodes of the tree and decide if they need to be sub divided, and further sub divided after that.



I get the feeling I'm doing this wrong, can anyone point me in the right direction? I'm not sure how to check if a pixel is solid or transparent. I'm also confused by what loop structure to use.

The process I wrote down is this:
1. Create Quadtree root
2. Subdivide root
3. Loop through the 4 nodes, checking the pixels in each
	1. If both types of pixels are read
		1. Subdivide this node
		2. Loop through each child of this node
			1. Repeat steps 3 through 3.1.2
				1. If a node is either pure solid or transparent
					1. Stop and go up a level

Am I doing this wrong? :o