# Destructable terrain-Bomb Craters

I am a beginner to Solar2D. I am trying to make a game where you dodge falling bombs, which make craters in the ground. I’m having trouble thinking of a way to to implement the crater formation. Does corona support any type of destructible terrain?

Not in the way you’re probably thinking, but … kind of

Here is a link to a zip containing 3 progressively more detailed examples:
http://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/Tips_and_Tricks/scorchedEarth.zip

1 Like

I once wrote a project where I handled destructible terrain using good old math. I couldn’t seem to find the project folder anywhere, but I’ll take another look later today.

Essentially, I created a table full of Boolean values for the map. I used this data to draw the initial ground by calculating a smoothed shape for one big polygon. I also gave this polygon a repeating texture fill and chain physics body. The core limitation was this approach was that it couldn’t create levels with hollow ground, i.e. each tunnel or hole had to be connected to the surface, there couldn’t be closed holes in the level.

Then, during gameplay, when an explosive collided with the ground, I treated the explosion as a circle (with its size depending on the type of explosive) and then I just calculated the difference between the level and the explosion (see Boolean operations on polygons).

Following the operation, I updated the level’s shape, created a new polygon below the existing one and then removed the previous polygon.

I don’t think it’s the most elegant solution, but if you are familiar with Boolean operations on polygons, it’s not too difficult to implement.

2 Likes

You might consider using chain bodies if you only care about is the surface contact. You can use the same set of vertices to generate the polygon and and the chain body. I’ve had success with this method.

If you are handy with trig, you can use the radius for the displacment - otherwise you can use a ratio to approximate the displacement

4 Likes

This is pretty much Boolean operations on polygons, but explained in a more aesthetic way.

I took a look at my old external hard drive, but seems like my project is lost somewhere.

But, to @pkrg, there are plenty of ways of accomplishing this, but there aren’t any that are ready out of the box, you’ll need to implement the solution yourself. This isn’t a particularly easy topic for a beginning programmer in general, but don’t get discouraged.

1 Like

@XeduR - Boolean operations on polygons would make a great plugin . . . just sayin’

3 Likes

@sporkfin Indeed it would… and I think that @StarCrunch had/has a plugin that does these sort of operations.

I only have the union operation set up somewhere in Lua. Not sure in which project and/or where.

@sporkfin @XeduR Yes, I have bindings to Clipper, to do CSG operations. Also libtess2 to boil the results down to a form Solar can use. These are in the free plugins directory. Sample here.

Possibly also of interest, say if the terrain were backed by a bitmap (as in 0s and 1s, nothing graphical), is msquares, which has a sample as well. One of the examples will make one or more physics bodies from what you draw.

In either case you’d presumably have to destroy one body and swap in its replacement. I’ve only done this with sensors, so I can’t say if Box2D will handle this gracefully, e.g. if other objects had already settled on the terrain bit in question.

1 Like

@StarCrunch, it takes some effort but Box2D can gracefully handle terrain swaps. One trick is to generate the new terrain (under the old if visually necessary) and then wait one frame before destroying the old terrain. Of course, this doesn’t work if an object is coming too quickly toward the terrain and ends up on the other side of the new chain-body.

One solution for the velocity problem is to manually pause and resume the moving objects’ velocity every other frame but it can look a little choppy. A better solution is calculating if the object will bypass the chain body and, if so, reverse its velocity (bounce).

I use tricks like this to make curved and rounded physics bodies that move and interact dynamically with their surroundings (think bezier curves as a physics object).

1 Like