Slicing, splitting and cutting objects with Box2D – part 4: using real graphics

The fourth part of this tutorial comes from Antoan Angelov and not only allows us to slice any kind of bitmap image mapped on our Box2D objects, but also improves the code making it faster and more robust.

This is the final result:

Use the mouse to slice objects.

And this is the fully commented and explained code:

userData class is as follows:

The result is really amazing so I think we’ll see some new game concepts using slicing.

Download the source code.

  • That is so cool. I’m sure it won’t take long for someone to develop a game that revolves around Sharks with lazer beams on their heads…

  • nice post but hm.. u’re using the easy way. you should do this with uv mapping of polygons

  • Ben

    This is great! Nice follow-up to the previous samples, love the code comments.

  • Hasan

    simply AWESOME..

  • real graphics,wow,it’s really i want ~~
    thx very much!!!

  • MC

    Very impressive, good post!

  • Guy

    Prehaps next time you could look at playerio and make a tutorial because everybody likes playin a mulitpalyer game

  • MC

    I noticed this developer used beginBitmapFill insted of masking to show the texture… Is this better / optimal than masking in cpu terms?

  • Antoan Angelov

    I think this is the best way to do it. If you make a sprite which holds the texture image and a mask, its probably slower because the CPU has to render both the sprite and the mask. You could use an alpha mask on a DisplayObject (MovieClip, Sprite, Bitmap, etc.) – you do it by using BlendMode.ALPHA (, but I think its slower too, because the CPU renders two display objects here as well. The beginBitmapFill() method does not require additional sprites, thus it makes is easier for the CPU to render.

  • as i said the right way to do this is to have polygons and map them using uv data. when fp 11 comes along we’ll be only doing this.

  • Pablo

    Nice example, but I wonder why this is performing so much slower FPS than the original one.

  • zoe

    MM, split engine is fun.
    And I made a Cut Cut Zombie game inspired by ninja fruit:)

  • JJ Smith

    Hey your source files flash movie seems to have a glitch!

  • Really nice pos it on XD

  • Pingback: All you need to know about how to write Box2D game | Artits Rastas()

  • Dev

    Thank you for such a nice tutorial, But in my case I am using physics editor for editing physics parameters and getting vertices of a complex object. Physics editor gives me shape using a combination of different shapes. and eventually a single body will have different fixtures associated with it. This algorithm assumes a body will have single shape,fixture associated with a body.
    so how can we slice an object which is composed of different shapes.

    @mmankt, you are saying that the best way of doing this is using uv mapping of polygon. Do you have any reference/blog post regarding this or can you please provide me hints on this.

  • bht

    I’ve tried to port it to iOS, and most of the time its quite ok, but there are a lot of b2Asserts unfortunatelly… (Assertion failed: (m_I > 0.0f))

  • superb but it shows some errs

  • Anrew

    @bht: any chance to see the iOS port on github o similar? maybe I can help you to improve\fix.

  • Pingback: How To Make A Game Like Fruit Ninja With Box2D and Cocos2D Part 1 | Ray Wenderlich()

  • Pingback: How To Make A Game Like Fruit Ninja With Box2D and Cocos2D Part 1 | | Hacker ColonyHacker Colony()

  • mohit parihar

    sir its possible to change this code into j2me for mobile, if it possible then give me j2me version of this source code.

  • Pingback: ???? Box2D ? Cocos2D ????Fruit Ninja??? Part 1 | ??()

  • Tetsuken

    I’m new on game programming and begginer on programming and i found your site and its been very usefull as knowledge source, i’m here to ask if is possible use that slicing functions on STENCYL?

  • Pingback: ????Box2D?Cocos2D?????Fruit Ninja?????-?1??()

  • Pingback: Cómo hacer un juego como Fruit Ninja utilizando Box2D y Cocos2D – Parte 1()

  • Pingback: ????Box2D?Cocos2D?????Fruit Ninja?????-?1??()

  • Pingback: ????Box2D?Cocos2D?????Fruit Ninja?????-?1??()

  • yann


    how you made ??to raise the limit on the number of vertices in a polygon.

    because in java the limit is 8 vertices per polygon.

    In other words your code if a cut was creating a polygon with 9 sides my application crach.


  • Pingback: ????Box2D?Cocos2D?????Fruit Ninja?????-?1?? | ??????????????()

  • luky

    excellent thanks :D