Build 10 games and learn game development along the way in this fast paced game development course

Get the source code of 12 commercial games, loaded 50+ million times, and learn the secrets of game design

Learn how to make a successful Flash game from a real world example. Fully commented source code

Create realistic Flash physics games from scratch with advanced features, from Angry Birds to Crush the Castle

Triangulation of polygons with holes: poly2tri class

I am always been very interested in a solid script to handle triangulation of polygons with holes, because it’s the first step to the generation of a physics destructible terrain with Box2D, something I always wanted to do.

Some time ago I published my attempt to triangulate a polygon but my script did not handle holes.

Now I found a 2D constrained Delaunay triangulation library called poly2tri originally written in C++ and Java but already ported in AS3 which seems to work with holes.

At the moment it needs some changes to perfectly fit to my needs, but it’s a great start to achieve my destructible terrain.

Look at this script:

We have a square as big as the stage, divided into two triangles. Try to click with the mouse inside to create a square hole and see the computed triangulation.

There is no limit to the amount of holes you can do, as long as a hole:

1) is entirely inside the big square

2) does not overlap any other hole

These two points are the ones I am working on now, meanwhile here is the script I made:

As you can see, it’s just a matter of addRectangle and addHole methods provided by VisiblePolygon class.

Download the source code and give it a try.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)
Loading...Loading...
Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 2 comments

  1. Nicusor Vasilescu

    on June 10, 2013 at 4:19 pm

    hmm… new ideeas come to mind with this . thx.

  2. Ivan

    on June 13, 2013 at 9:00 am

    Error: Sweep.edgeEvent: Collinear not supported!
    at org.poly2tri::Sweep/edgeEventByPoints()
    at org.poly2tri::Sweep/edgeEventByPoints()
    at org.poly2tri::Sweep/edgeEventByEdge()
    at org.poly2tri::Sweep/sweepPoints()
    at org.poly2tri::Sweep/triangulate()
    at org.poly2tri::VisiblePolygon/_triangulateOnce()
    at org.poly2tri::VisiblePolygon/get triangles()
    at org.poly2tri::VisiblePolygon/drawShape()
    at Main/doHole()

Thank you for the download!!

I hope you will find it useful and make something interesting out of it

To keep up to date with the blog, why don't you like my Facebook page and follow me on Twitter?

Want to learn more? Don't miss this:

Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now

×