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

Simulate radial gravity (also known as “planet gravity”) with Box2D as seen on Angry Birds Space

With the launch of Angry Birds Space I am sure you are wondering how to simulate planet gravity with Box2D.

And guess what… the basics are very easy.

First, in space there’s no gravity, so you will create a b2World world without gravity this way:

Then, it’s just a matter of applying Forces according to bodies and planets position.

Look at this script:

The whole code just create static bodies (planets) and let you place dynamic bodies (debris) with the click of the mouse.

The only interesting part of the script is the for loop in the update function, which I’ll explain line by line:

Loop which scans for all debris previously stored in debrisVector Vector declared at line 14 and updated at line 54

Gets debris position

Loop which scans for all planets previously stored in planetVector Vector declared at line 13 and updated at line 38

I need to know the mass of the planet because the bigger the mass, the more intense the gravity attraction. Unfortunately Box2D static bodies do not have mass, so I need to get the circle shape of the planet…

… and get its radius. So in this case the bigger the radius, the more intense the gravity attraction

Gets planet position

Creates a new b2Vec2 variable which will store the distance between the planet and the debris

Adds debris coordinates, then…

… subtracts planet coordinates

Calculates the distance between the planet and the debris

Checks if the debris should be affected by planet gravity (in this case, the debris must be within a radius of three times the planet radius)

Inverts planet distance, so that the force will move the debris in the direction of the planet origin

Gets the sum of distance vector components. I will need this to make gravity attraction weaker when the debris is far from the planet, and stronger when the debris is getting close to the planet

This is the final formula to make the gravity weaker as we move far from the planet

And finally the force can be applied to debris

This is the result:

Click to create debris.

Download the source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (25 votes, average: 4.96 out of 5)
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 18 comments

  1. Pierre Chamberlain

    on March 28, 2012 at 6:31 pm

    Nice tutorial Emanuel! I was just looking at some video trailers of Angry Birds Space today actually, great timing on your part ;)

    Any guess if Rovio used Box2D in the game?

  2. Emanuele Feronato

    on March 28, 2012 at 6:53 pm

    yes, it does

  3. Asr JW

    on March 29, 2012 at 4:05 pm

    great tutorial!!.
    if you could help me?! Can you make a tutorial to make a dress up game to be like the game MapleStory ^_^

  4. Phil Harvey

    on March 29, 2012 at 6:58 pm

    Code works nicely, I converted it to use ND2D and build mobile (iOS and Android) builds, and they run at a very nice frame rate.

  5. Luis

    on March 30, 2012 at 4:13 pm

    Excellent tutorial!!! Emanuel I would like to know how to use something similar to this: making the hero walk on little planets or something like that, and be able to walk upside down.

  6. Behrouz

    on April 10, 2012 at 8:41 am

    very Nice tutorial about Box2d
    thanks a lot.

  7. Fede

    on April 13, 2012 at 5:49 pm

    Grazie Emanuele!

    What happens if the “planet” gets destroyed in the tick method? I get some strange behaviours from the “debris” bodies in a similar game but wanted to check if your version does the same.

  8. ??????Box2D???????? | ???

    on April 18, 2012 at 10:52 am

    […] is the result:?Source?emanueleferonato? ???? QQ?? ???? ??? […]

  9. Niverse

    on April 23, 2012 at 9:09 am

    Might put some of this into my flash game. Very Interesting stuff!

  10. Angry Birds

    on April 23, 2012 at 10:31 pm

    Very good tutorial ;-) i make a clone of angry birds space thanks

  11. Angry Birds

    on April 23, 2012 at 10:44 pm

    Is possible create this tutorial for stencyl, construct 2 or multimedia fusion 2 ??

  12. TNT

    on April 25, 2012 at 6:30 pm

    Hi there…
    I am not really sure if this setup is the only thing to get it as accurate as possible. If you shoot such a box, (just simple apply force) it is very easy for the box to escape the planet. Instead, what I would want to try is; how can you get the box orbiting around the planet and land softly on the surface. This way, the box will orbit around the planet and the distance between surface and box is getting smaller and smaller.
    How can you do something like that? Any ideas would be very helpful!

  13. No, this is not going to be Angry Birds Space v2 ;-) « MV Fusion

    on April 27, 2012 at 6:34 pm

    […] my research to create some very cool radial outer space gravity stuff I came across this link from Emanuele Feronato. That example was the basic to create this video, but I have added a lot of stuff to re-create that […]

  14. TNT

    on April 27, 2012 at 6:43 pm

    I think, I got something….
    check it out here:
    What do you think?

  15. Make a Flash Game

    on May 7, 2012 at 2:11 pm

    coo,l totrial

  16. shanshan

    on August 4, 2012 at 12:31 pm

    sorry im very entry level, this could be a totally stupid question, but I could what’s this initializing doing here:
    private var planetVector:Vector.=new Vector.();

    So is planetVector an array?
    If so: why not just declare it as an array?
    If not: whats the difference between using Vector. and an array of b2body?

  17. xlen

    on December 8, 2012 at 11:34 pm

    can you make turtorial for game like angry birds making

  18. MoonlightOwl

    on October 31, 2014 at 3:59 pm

    Pretty simple, but damn cool effect!
    Thank you.

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