Build 10 classic Flash games and learn game development along the way with this ultra-fast paced game development course.

If you love this blog, this is the book for you.

Buy the book

Get the source code of 12 commercial Flash games, which have been loaded more than 50 million times!

Learn from real world successful examples.

Get it now

Box2D for Flash Games teaches you how to make Flash physics games from scratch with the most advanced features.

Create the new Flash game smashing hit.

Buy the book

Create non-convex, complex shapes with Box2D

If you are working with Box2D for a while, probably you have already met the problem you can’t create non-convex shapes. This is a problem Box2D faces for years, as you can see from this post, and due to Box2D’s structure itself, we will never be able to create concave shapes.

That’s why some 3rd party libraries like PhysicsEditor allow us to break a concave shape into a set of convex shapes and make possible the creation of complex bodies.

Antoan Angelov, the author of the optimized version of the Box2d slicing engine, strikes back with another great Box2D class called b2Separator.

« The class is called b2Separator, and yes – it is made to work specifically with Box2D! What does it do, you ask? Well, we all hate when we have to make a more complex, non-convex shape for our b2Body – because we have to manually make many b2Fixtures, and make those really confusing and time-wasting calculations to see how each fixture is supposed to fit in with the others. Well, our troubles are now over, because what b2Separator does is separate a non-convex shape into smaller, convex shapes in a very optimized way and add them as b2Fixtures to a b2Body. All you need to do is give it the vertices of the shape you want! »

This is the fully commented class:

And this is what you can do with it:

Download the source code and all required libraries.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (15 votes, average: 5.00 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 16 comments

  1. Danny Phantom

    on September 12, 2011 at 12:22 pm

    wow. Amazing. I don’t work with Box2D – But I think I’m gonna try it :). Thanks for helping

  2. Scott Mitchell

    on September 12, 2011 at 1:09 pm

    Hey Emanuele, Thanks for this tut, was great. I contribute to your web traffic everyday ;0 By checking everyday for new tuts. I was wondering if you were to carry on with the plant vs zombies or tiny wings game? Would be great to know its coming :)

    Many thanks Scott

  3. mmankt

    on September 12, 2011 at 1:13 pm

    WIN, thx Anton!

  4. Bruce Isabelle

    on September 12, 2011 at 10:36 pm

    nice tutorial, thanks, i did created many shapes using Box2D..

  5. Andreas Burg

    on September 12, 2011 at 11:38 pm

    Nice, now I can stop pushing shapes through PhysicsEditor only for that very purpose. :)

    Many thanks!

  6. Ben Reynolds

    on September 13, 2011 at 5:21 am

    As always, a great tutorial. I’m not the most comfortable with box2d, it’s always useful to see applications like this :)

  7. MC

    on September 13, 2011 at 8:07 pm

    I remember that in the WCK (world construcion kit from box2D alchemy port) you can create shapes, even non-convex shapes :s

  8. Ari

    on September 14, 2011 at 6:32 pm

    Like MC mentioned, the alchemy version of box2d actually has this built in.

    This is in the b2PolygonShape class:

    /// Decompose a concave polygon into a bunch of convex ones. Pass it vector of vertices
    /// in [x1, y1, x2, y2...] format. Returns a vector list of b2PolygonShapes that represent
    /// the decomposition.
    public static function Decompose(v:Vector.):Vector. {…

  9. Chris Moeller

    on September 14, 2011 at 6:49 pm

    Awesome! I had gotten hung up on this step for a game in the past. Soon as I get more time put aside, need to get back to it :/

  10. Olli

    on September 16, 2011 at 10:19 am

    Excellent tool! Now one question. Given a bitmap image what would you suggest I should do to dynamically create a body for it? Is it even feasible?

  11. Cool Stuff with the Flash Platform - 9/19/2011 | Remote Synthesis

    on September 21, 2011 at 10:13 pm

    [...] Feronato introduces b2Separator, a Box2d class written by Antoan Angelov to assist in creating non-convex, complex shapes with Box2D by automatically separating a non-convex shape into smaller convex [...]

  12. Eric

    on November 30, 2011 at 7:03 pm

    Why do you only support CW(Clockwise Winding), according to the Box2D manual you must create polygons using CCW(Counter Clockwise windind).

    See the manual for explenation:
    http://box2d.org/manual.pdf

  13. ToneTone

    on February 20, 2012 at 6:53 pm

    This is a great class – I’ve converted it to C++ (for use with the standard box2d library) and also to C# (for use in Unity). If anyone wants a copy tweet me @physmotone

  14. tian

    on January 2, 2013 at 8:48 am

    Hello,sir, can you covert the code to c++? if you can, thank you very much!

  15. Jarad

    on January 14, 2013 at 3:55 pm

    This is a fantastic class! I’ve also converted it to C++ for use with Cocos2D-X. If anyone would like to fork it, be my guest =) http://github.com/delorenj

  16. Jacob

    on May 9, 2013 at 6:14 am

    This is an old post but… You have an error in your code. There is a semicolon at the end of line 19 which declares the public class. If you delete that the rest seems to be fine.