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

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 (12 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 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.