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

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.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (33 votes, average: 4.76 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 31 comments

  1. James

    on August 5, 2011 at 4:21 pm

    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…

  2. mmankt

    on August 5, 2011 at 6:02 pm

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

  3. Ben

    on August 5, 2011 at 7:19 pm

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

  4. Hasan

    on August 6, 2011 at 11:06 am

    simply AWESOME..

  5. ????

    on August 8, 2011 at 5:15 am

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

  6. MC

    on August 9, 2011 at 2:07 am

    Very impressive, good post!

  7. Guy

    on August 9, 2011 at 10:51 am

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

  8. MC

    on August 11, 2011 at 4:48 pm

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

  9. Antoan Angelov

    on August 12, 2011 at 4:59 pm

    @MC
    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 (http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#blendMode), 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.

  10. mmankt

    on August 19, 2011 at 1:27 pm

    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.

  11. Pablo

    on August 21, 2011 at 5:29 pm

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

  12. zoe

    on September 6, 2011 at 6:31 am

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

    http://www.0000park.com/1741/cutcutzombie/

  13. JJ Smith

    on September 7, 2011 at 4:00 pm

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

  14. sogetsu

    on October 9, 2011 at 11:58 pm

    Really nice pos it on wonderfl.net XD

  15. All you need to know about how to write Box2D game | Artits Rastas

    on February 16, 2012 at 4:33 pm

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

  16. Dev

    on March 15, 2012 at 7:03 pm

    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.

  17. bht

    on March 21, 2012 at 7:37 pm

    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))

  18. sengu

    on March 21, 2012 at 11:58 pm

    superb but it shows some errs

  19. Anrew

    on March 28, 2012 at 5:02 pm

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

  20. How To Make A Game Like Fruit Ninja With Box2D and Cocos2D Part 1 | Ray Wenderlich

    on June 19, 2012 at 5:02 pm

    […] the foundations for the project this tutorial is based on. He was responsible for porting this flash-based slicing demo into Cocos2D, and also for porting CCBlade and PRKit to Cocos2D […]

  21. How To Make A Game Like Fruit Ninja With Box2D and Cocos2D Part 1 | | Hacker ColonyHacker Colony

    on June 20, 2012 at 8:34 pm

    […] the foundations for the project this tutorial is based on. He was responsible for porting this flash-based slicing demo into Cocos2D, and also for porting CCBlade and PRKit to Cocos2D […]

  22. mohit parihar

    on July 30, 2012 at 12:21 pm

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

  23. ???? Box2D ? Cocos2D ????Fruit Ninja??? Part 1 | ??

    on September 2, 2012 at 6:15 pm

    […] ????Rick Smorawski ???????????????flash-based slicing demo ?Cocos2D???????CCBlade?PRKit?Cocos2D 2.0??????????????????????????????????? […]

  24. Tetsuken

    on September 3, 2012 at 3:54 am

    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?

  25. ????Box2D?Cocos2D?????Fruit Ninja?????-?1??

    on September 24, 2012 at 3:36 am

    […] Smorawski??????????????????????flash-based slicing demo????Cocos2D????CCBlade?PRKit????Cocos2D […]

  26. Cómo hacer un juego como Fruit Ninja utilizando Box2D y Cocos2D – Parte 1

    on October 5, 2012 at 5:24 pm

    […] bases del proyecto en el que está basado este tutorial. Fue el responsable de portar este demo de cortes basado en flash a Cocos2D, y también por portar CCBlade y PRKit a Cocos2D […]

  27. ????Box2D?Cocos2D?????Fruit Ninja?????-?1??

    on October 6, 2012 at 5:25 am

    […] Smorawski??????????????????????flash-based slicing demo????Cocos2D????CCBlade?PRKit????Cocos2D […]

  28. ????Box2D?Cocos2D?????Fruit Ninja?????-?1??

    on October 6, 2012 at 5:26 am

    […] Smorawski??????????????????????flash-based slicing demo????Cocos2D????CCBlade?PRKit????Cocos2D […]

  29. yann

    on March 1, 2013 at 7:12 pm

    Hello

    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.

    Bye

  30. ????Box2D?Cocos2D?????Fruit Ninja?????-?1?? | ??????????????

    on September 3, 2013 at 7:20 am

    […] Smorawski??????????????????????flash-based slicing demo????Cocos2D????CCBlade?PRKit????Cocos2D […]

  31. luky

    on March 24, 2014 at 2:52 pm

    excellent thanks :D

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

×