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

Breaking objects with Box2D – the realistic way

It all started with the Box2D slice engine, which evolved in the Box2D explosion engine.

Now it’s time to break objects in the realistic way using the same concept.

This is what I made:

Click on the stage to shoot a sphere in a random direction and watch how it breaks the wooden pole. I made it with “bullet time” so you can see how it works, and also because I sometime experience some problems when running it at full speed, but it’s just a prototype at the moment.

Anyway, it works this way:

* When the sphere touches the wooden pole, my custom contact listener detects it.

* At this time, I know sphere position and velocity. Assuming the sphere could cut the wooden pole, I can determine the raycast according to sphere position and direction

* I apply the raycast to the wooden pole, splitting it in two

* Finally I clone the sphere, creating another one with the same speed and direction of the old one, the one which hit the wooden pole, to recreate the impact on the recently added wooden pole slices.

And that’s it.

I am posting the fully commented source code:

and this is the custom contact listener:

I hope this will give you fresh ideas for some original game design. Download the source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (14 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 3 comments

  1. Daniel

    on March 5, 2012 at 11:13 pm

    Dude, so cool, you should have used a katana slashing a bamboo tree instead of a circle! :D, im pretty sure that it will look cooler with more gravity, but it would be more difficult to see the effect, nice work!!

  2. Jesse Nicholson

    on March 14, 2012 at 1:28 pm

    I’ve been working on nearly the exact same thing in C++ but I’m trying to do more advanced destruction, like cratering. For the cratering it’s basically a matter of adding overlapping vertices from the colliding body to the impacted body and removing any vertices from the impacted body that may be within the impacting body. Only time it gets complex is when you need to detect if your “cratering” has eventually broken through the impacted body/fixture. Maybe you could try something like this next in flash. :)

  3. Complex

    on March 18, 2012 at 8:21 pm

    How about complex objects (with more than one shape)?
    I’ve been following the cutting tutorials but everything seems to be a single shape, which is certainly not the case always.

    Example: a breaking sword. You can’t make that great a sword without a hilt, handle, etc. but having only the blade break would look great.

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

×