Install Circle Chain on your iPhone for free and get the source code!! 645 downloads to go - last updated: April 21, 2012

Working on a Box2D slice/split/cut engine

A long time ago (2 years) Guillaume Pommey aka pompom showed us a working version of a Box2D slice/split/cut engine, using the old distribution of the physics library, now deprecated.

I wanted to port it to 2.1a version, but once I start coding I followed another path and this is what I have:

Give me a couple of days to clean the code, and I’ll come with a detailed tutorial about slicing objects with the latest Box2D distribution.

Meanwhile, give me your suggestions/feedback.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (10 votes, average: 4.40 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 13 comments

  1. Ari

    on June 9, 2011 at 11:42 pm

    I really like what you are doing. Have you tried it with the Clipper library though? I was interested to see if it could slice a polygon with a single line (tvo vertices) and it was not possible, however if I turned the line into a one pixel thin rectangle polygon it worked perfectly. I timed it and it measured only 2ms cutting a simple square.

    Clipper: http://flassari.is/2011/05/as3-polygon-clipper/
    It is an alchemy port, so the first time it is called it takes a little longer while the lib is initializing.

    Here’s the code I used to test it:
    package
    {
    import com.flassari.geom.ClipType;
    import com.flassari.geom.Clipper;

    import flash.display.Sprite;
    import flash.geom.Point;

    public class SlicePrototype extends Sprite
    {
    public function SlicePrototype()
    {
    // The square
    var subject:Array = [new Point(10, 10), new Point(100, 10), new Point(100, 100), new Point(10, 100)];
    // The clipper line, a rectangle with 1 px width
    var clip:Array = [new Point(0, 0), new Point(200, 200), new Point(200, 201), new Point(0, 1)];

    var polygons:Array = Clipper.clipPolygon(subject, clip, ClipType.DIFFERENCE);
    }
    }
    }

    The code will return two polygons.
    This can be used to cut the most complex polygons (concave, hollow) I can think of and still perform well. Also, this can be used to make the cutting line itself as thick as one wants.

    -Ari-

  2. Chris Moeller

    on June 9, 2011 at 11:48 pm

    Awesome, I’m looking forward to it!

    I had a problem with converting new shapes to counter clockwise polygons, and breaking up the shape into two new convex shapes. It worked like 80% of the time, but crashed flash when it didn’t work (infinite loop somewhere) – but it was based on someone elses code, and I wasn’t completely clear on how they did both steps (how do you programmatically figure out if points are counter clockwise? How do you figure out if the shape is convex by just knowing the points?)

  3. Kirtimaan

    on June 10, 2011 at 4:05 am

    While you are talking about Box2D, there is a question related to framework. Is it possible to have curved shaped objects in Box2D? Say a mountain on which a player object moving. All the examples I have seen have rectangle, circle or squares.

  4. csomak

    on June 10, 2011 at 8:42 am

    I’d love to see a feature that can handle worms-like maps and explosions.. so that holes can be created on the ground. if you create a floating island, it would fall down cause of gravity.. how awesome would be that?

  5. Emanuele Feronato

    on June 10, 2011 at 10:01 am

    @Ari: nice library, I am taking a look at it

    @chris: cutting squares, and any polygon generated by cutting a square, will always give convex shapes. If you store the initial vertices order, then it won’t be difficult to have counter clockwise polys

    @Kirtiman: no, it’s not possible, you should approximate the circle with a polygon with an high number of vertices

    @csomak: awesome but quite difficult, I’ll try to do something similar

  6. csomak

    on June 10, 2011 at 11:04 am

    i will love to help :D
    I wanted to build a real time Worms (no waiting for other player, just the reload time) with box2d, but I lack-ed that. I’d love to help with that ;)

  7. Fardeen

    on June 10, 2011 at 11:17 pm

    Use http://code.google.com/p/quickb2/ . The demos are impressive !

  8. Emanuele Feronato

    on June 10, 2011 at 11:37 pm

    I don’t like the way it splits polys. Hundreds of lines.

  9. Ari

    on June 11, 2011 at 1:41 pm

    I tried the quickb2 slicing when I was trying to make my own polygon clipper, the newest version had a lot of bugs in it and diving trough the svn revisions didn’t help.

  10. MC

    on June 11, 2011 at 10:44 pm

    I suppose you had to create two different polygons from the intersection points, but i always wondered how they split the textures inside the polygons? (in box2d)

  11. Emanuele Feronato

    on June 11, 2011 at 11:36 pm

    Draw a sprite with the same shape and use it as a mask

  12. santosh

    on June 13, 2011 at 8:22 am

    nice development!!!!!!!!!!!

  13. whoop

    on January 9, 2012 at 4:08 pm

    Please upload your source code