Radial gravity, orbital gravity, orbital mechanics, whatever you want to call it there is no denying it can be useful for many kinds of games. Unfortunately, neither Box2D nor Chipmunk Physics Engines for the iPhone had support for it. I had a hell of a time trying to figure this out but in the end it was so simple. Here is how to do it.
First, create a new Cocos2D / Box2D Hello World project from the supplied Cocos2D templates. Add the following code to HellowWorldScene.mm and HelloWorldScene.h:
HelloWorldScene.mm
-(id) init { //... Add at end of init after body->CreateFixture(&fixtureDef); // Create our static "Planet" b2CircleShape shape; shape.m_radius = 1.0f; shape.m_p.Set(8.0f, 8.0f); b2FixtureDef fd; fd.shape = &shape; planet = groundBody->CreateFixture(&fd); } -(void) tick: (ccTime) dt { int32 velocityIterations = 8; int32 positionIterations = 1; world->Step(dt, velocityIterations, positionIterations); for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) { b2Body* ground = planet->GetBody(); b2CircleShape* circle = (b2CircleShape*)planet->GetShape(); // Get position of our "Planet" b2Vec2 center = ground->GetWorldPoint(circle->m_p); // Get position of our current body in the iteration b2Vec2 position = b->GetPosition(); // Get the distance between the two objects. b2Vec2 d = center - position; // The further away the objects are, the weaker the gravitational force is float force = 250.0f / d.LengthSquared(); // 150 can be changed to adjust the amount of force d.Normalize(); b2Vec2 F = force * d; // Finally apply a force on the body in the direction of the "Planet" b->ApplyForce(F, position); if (b->GetUserData() != NULL) { CCSprite *myActor = (CCSprite*)b->GetUserData(); myActor.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO); myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()); } } }
HelloWorldScene.h
@interface HelloWorld : CCLayer { b2World* world; GLESDebugDraw *m_debugDraw; b2Fixture* planet; // This will be a static body }
And here is the result!
Enjoy!
Download Source Code for this tutorial:
Radial Gravity Box2d & Cocos2d Example Source Code – 692k
Random Posts:
- iPhone Programming Contest
- New code syntax highlighting
- Hunting trip pictures
- Planning My 2010 Hunting Trip
- Game Concept Progress
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: box2d, C++, cocos2d, code, iPhone, Objective-C, orbital gravity, radial gravity, tutorial, Xcode
You can leave a response, or trackback from your own site.

[...] other day I wrote a tutorial on how to implement orbital / radial gravity in with the use of Box2D and Cocos2D. I have decided [...]
interesting.
i did something similar to implement “suns” in my space pack
here is my code: (just the physics part, i obviously added a sprite/particle effect over it)
THIS IS ALL WITHIN A UPDATE LOOP THAT RUNS AS FOLLOWS: [self schedule:@selector(tick:)];
[...] is some example code based off my tutorial on how to implement orbital / radial gravity, but in reverse. This example pushes objects away [...]
Thanks Joseph, check out my recent post about using this code to build a bomb-like effect.
Is there anyway you can port this to Action script so I may use it in flash.
I don’t use Flash, sorry. If you are familiar with Flash and Actionscript, you should be able to port this yourself.
[...] How To Add Radial Gravity To Box 2D Radial Gravity w/ Box2D SOURCE [...]
Keep posting stuff like this i really like it
Hi ~
This is very simple and useful post.
I find this danyeoteotda.
Thanks for reading!