Here is some example code based off my tutorial on how to implement orbital / radial gravity, but in reverse. This example pushes objects away instead of pulling inward. It makes for a nice explosion effect. Much work would need to be done to make it realistic, but you get the general idea of how to blow things up. You need to have Cocos2D and Box2D installed. Box2D comes packaged with the latest version if Cocos2D.

Download Source Code for this tutorial:
Bomb Demo Box2d & Cocos2d Example Source Code – 799k
Random Posts:
- Hunting Pictures
- UIImage & The Retina display
- Thanks kwigbo.com!
- SpaceBubble OpenSourced!
- SpaceBubble Released for iPhone
If you found this useful, shoot me a small donation or at the very least leave a comment, every bit of encouragement helps keep me motivated to update with more content on a regular basis!
Tags: bomb, box2d, C++, cocos2d, code, explosion, iPhone, Objective-C, orbital gravity, radial gravity, source code, tutorial, Xcode
You can skip to the end and leave a response. Pinging is currently not allowed.
Thanks for this… I modified it a little to make a more realistic explosion. I added a ‘bomblife’ variable that turns off the bomb gravity after a set period of ticks. This makes the falling blocks fall without being pushed away from the circles.
Like this:
Funny you posted this. I was just thinking yesterday how I wanted to implement a timer to turn off the force. Thank you! I will be updating this in the next week or so to implement something like this.
[...] Nick Vellios wrote a series on adding radial gravity to Box2D. (h/t: [...]
Thank you for this tutorial! I also add the new bomblife too and it works great! I have a question… How would you change your code to NOT explode but IMPLODE? I kind of newbie
Thanks Dan! This is great!
I’m having some trouble iterating on this though. I’d love to be able to delete the bomb’s b2Body and let the user create an additional bomb.
Every time that I remove the body though, I lose the ability to add additional bombs. Do I need to add the bombs to some sort of list before I remove their b2Body? How would I go about that?
Thanks so much!
Very cool. Thanks for posting this. After playing around a bit I decided to use ApplyLinearImpulse instead of ApplyForce. Then you can turn it off after one tick and get essentially the same result.