Create REAL explosions with Box2D – adding textures

Ok, you asked it, now you have it. Now the Box2D explosion prototype features bitmap textures and a commented source code.

I have to say a big thank you to Antoan Angelov for his version of the slicing engine as he allowed me to manage textures with no hassle.

So now basically an explosion is a number of cuts at a random angle, all passing for the same point, which is the origin of the explosion. Then, every resulting slice will have a linear velocity according to the distance between the center of the slice mass and the origin of the explosion.

So this is what you’ll get:

Click on a crate to make it explode.

And this is the full, commented source code:

and this is userData class:

And now let me see what kind of games can you make using this concept.

Download the source code.

  • I am not a flash developer…but still i am following you coz i get ideas from you. i also enjoy ur posts and demos. I like to play with ur demos ;) can u add “Reply” button to demos ?? coz i need to refresh the page, if i want to play with demo again and again :(

  • Simon

    Wow – that is pretty awesome. I wan’t to make a building demolition game with this. The only problem is my inability to finish projects. But thank you for this, as well as all your other inspiring articles.

  • MC

    Great!! i hope to learn something here, thanks :)

  • NICE

    Whoah nice job there.

    Although I noticed that it lags after blasting boxes for awhile buuut… prototype.


  • Michael

    Great job Erin & Antoan! True source of inspiration for game developers!

    I look forward to try and making this work on the iPhone

  • MC

    Emanuele, have you ever considered to create a small game framework in as3? Or a small customizable library with useful functions like: create/delete, explode and slice box2d objects?

  • Vilash V

    gr8! work..I always enjoy going through your posts and the way you come up with new ideas.

  • Emanuele, ever tried HaXe?
    Would be great to see Box2d and Starling running under HaXe.

  • Awesome, very super cool!

    I’ll be trying this out with my Android game- even being able to do one such explosion would add a ton of realism to some of the levels.

    You’re giving us tools/ideas to make awesome games!

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

  • Hey just trying to implement this in something, im trying to get it to work when something collides against the box, however if i change clickedBody to be a passed in body, it doesn’t work and i cannot see why. It gets an error due to removal =s

  • Molano

    is this make sense ? what do u think @emanuele

    bodyDef1.type = b2Body.b2_staticBody;
    color_block = new cblock();

    bodyDef1.userData = {assetName:”cblock”,assetSprite:color_block};
    color_block.x = posx;
    color_block.y =posy;

  • A lot of bitching has been done about Emanuele on his coding style, books and source code for sales. There are at least one more coding style than there are developers in the world and the “Not invented here” signal strike me too. You could take this code more OOP or TDD and DI, but the fact is that the algoritm is there! The solution is not obscured by a choice of methodology or usage of advanced tricks. I’m using bits and peaces in a C++ application and I’m guessing this is AS3 and Flash? I couldn’t care less, since everything is laid out and I’m happy people like Emanuele spend their time putting stuff on the web. I’m happy that people like Erin write open source like Box2D making bird companies rich.

  • gd

    Hey, noob question. very new to box2d. where exactly do I put my flash sprites into the code, been trying to do it for 2 days now. no luck.

    any help would be awesome.

  • Graham

    This is really something. It also works in the latest Box2D.

    I implemented it into a situation with a bunch of overhead and the box seems to freeze in mid-air and then pause a second, then explode.

    This would be fantastic to get to happen to objects in motion, and somehow trim the processing power.

    I’ll see what I can come up with.

  • gabriele betti

    What’s inside the .fla ?
    I’m on Flex .