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.

  • Daniel

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

  • Jesse Nicholson

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

  • Complex

    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.