How to create destructible terrain using Box2D – step 2

In the second step of this tutorial we’ll see how to turn the terrain created with geometry in step 1 into a real Box2D terrain.

I am going to use b2Separator class By Antoan Angelov to render any type of polygon with Box2D.

This will allow you to quickly render any kind of polygon but won’t save us from the infamous hole bug. This happens when one or more points of the “circle” which will represents the hole lie on a segment of a polygon:

I (obviously) found a solution for this bug, and I am going to show you the final result, next time, meanwhile have a look at this step, here is the source code with every line commented and highlights on new pieces of code.

And this is the result:

Click or drag on the terrain to create a hole or dig. Watch out for the infamous bug.

Download the source code. Next time, the final prototype.

  • Thanks for the post Emanuele, very informative as usual.

  • Image is flickering after some clicks, and I see it.
    http://s24.postimg.org/v560j442t/bugbugbug.png
    it normal ? :)

  • anlizfer

    me pasa lo mismo… Same Error, on MAC and Windows :)…

  • Yeah, mine too- every other click it flips back and forth- but it’s around the hole highlighted in dark blue-

  • Pawel

    Are people reading this? There is a bug as author writes.
    Anyway im implementing this with libgdx + android, box2d part should be heavily optimized with this. Eg. Small area polygons can be removed from terrains, also for water games tiles that are above water could be destroyed etc.. Even now results are very nice and fast.

  • Hi :-)
    tanks for share this

  • Pingback: Get simpler polygons by removing collinear points - Emanuele Feronato()

  • Greg

    Awesome! Super excited for your solution!

  • Aw this is really great.

    Any idea with part 3 is coming out? I’ve implemented everything step by step but I have no idea how to approach the “infamous hole bug”. :(

  • Pingback: Create a destructible terrain with Stencyl 3.1 image API - Emanuele Feronato()

  • Augusto

    Hey Emanuele, whatever happened to part 3 of your tutorial? I was planning to use this as part of a Worms clone game I’m developing as a hobby and I’d really like to know how you’ve fixed the hole bug.

    • Emanuele Feronato

      working on it!

  • Vo Tat Linh

    Dear Sir. Emanuele Feronato
    I very exciting with this article of you. As you wrote, there was an error. Can you guide me to fix that? And you said there would be a complete article but I did not find the article.

    Thanks.