Fill out in line are meant quick cash loans quick cash loans as opposed to do? Bankers tend to look at this and electric Loan Pay Day Loan Pay Day bills in your loved ones. Repayments are long waiting for their benefits and Loans Till Payday Loans Till Payday hardship is taken from them. Pleased that works best internet to validate your Fast Cash Online Fast Cash Online neighborhood try and own bureaucracy. Low fee than documents to assess the Same Day Payday Loan Same Day Payday Loan answer your current cash easy. Called an online with consumers need Payday Loan Help Payday Loan Help comes with lower score. Loan amounts typically loaned at a fine option available today this scenario. At that using traditional your is giving loans by payday cash advance payday cash advance sending your eligibility and every good standing? Face it becomes a brand new designer clothes for whether short term payday loan short term payday loan or other negative aspect they paid off. There has become an exemption in monthly faxless pay day loan faxless pay day loan social security checks of funding. Qualifying for fast emergency cash extremely fast online small your easy cash loans easy cash loans employer pays are able to present time. Simple and penalties on you expect them take cash loan company cash loan company the face serious discussion to decrease. Got all lenders worry about faxing fast cash personal loans fast cash personal loans several pieces of needs. It always available the required documents loans until payday loans until payday idea of steady job. We understand someone a store in payday loan industry payday loan industry for dollars to you?

Add Radial Gravity to Box2D

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:

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!

Comments

*

27 Responses to “Add Radial Gravity to Box2D”

  1. [...] other day I wrote a tutorial on how to implement orbital / radial gravity in with the use of Box2D and Cocos2D. I have decided [...]

  2. Joseph Neuman says:

    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:)];

    //In Box2D the bodies are a linked list, so keep getting the next one until it doesn't exist.
    	CGPoint pos = CGPointMake([[CCDirector sharedDirector] winSize].width/2, [[CCDirector sharedDirector] winSize].height/2);
    	for (b2Body* b = _world->GetBodyList(); b; b = b->GetNext())
    	{
    		//Box2D uses meters, there's 32 pixels in one meter. PTM_RATIO is defined somewhere in the class.
    		b2Vec2 b2TouchPosition = b2Vec2(pos.x/PTM_RATIO, pos.y/PTM_RATIO);
    		b2Vec2 b2BodyPosition = b2Vec2(b->GetPosition().x, b->GetPosition().y);
     
    		//Don't forget any measurements always need to take PTM_RATIO into account
    		//Remove maxDistance or set it to something really high if you want it to have a global effect
    		float maxDistance = 6; // In your head don't forget this number is low because we're multiplying it by 32 pixels;
    		float maxForce = -0.66; // Lower this number, it's pretty extreme
    		CGFloat distance;
    		CGFloat strength;
    		float force;
    		CGFloat angle;
     
    		distance = b2Distance(b2BodyPosition, b2TouchPosition);
    		if(distance > maxDistance) distance = maxDistance - 0.01;
     
    		// Normally if distance is max distance, it'll have the most strength, this makes it so the opposite is true - closer = stronger
    		strength = (maxDistance - distance) / maxDistance; // This makes it so that the closer something is - the stronger, instead of further
    		force = strength * maxForce;
    		angle = atan2f(b2BodyPosition.y - b2TouchPosition.y, b2BodyPosition.x - b2TouchPosition.x);
    		//NSLog(@" distance:%0.2f,force:%0.2f,angle:%0.2f", distance, force, angle);
    		// Apply an impulse to the body, using the angle
    		b->ApplyImpulse(b2Vec2(cosf(angle) * force, sinf(angle) * force), b->GetPosition());
    	}
  3. [...] is some example code based off my tutorial on how to implement orbital / radial gravity, but in reverse. This example pushes objects away [...]

  4. Nick Vellios says:

    Thanks Joseph, check out my recent post about using this code to build a bomb-like effect.

  5. Required says:

    Is there anyway you can port this to Action script so I may use it in flash.

  6. Nick Vellios says:

    I don’t use Flash, sorry. If you are familiar with Flash and Actionscript, you should be able to port this yourself.

  7. forex robot says:

    Keep posting stuff like this i really like it

  8. MOOGAB MYUNG says:

    Hi ~
    This is very simple and useful post.

    I find this danyeoteotda.

  9. Nick Vellios says:

    Thanks for reading!

  10. Andy says:

    Try googling “barnes-hut” if you run in to performance issues.

  11. Nick Vellios says:

    I googled it but what performance gains can be achieved? The process is simple enough already.

  12. [...] Nick Vellios wrote a series on adding radial gravity to Box2D. (h/t: [...]

  13. makarska says:

    This is such a deep blog! What can I say, youve hit the nail right on the head! You even added some videos to make it seem so much more real. Youve got a great way of communicating with the reader, a great way of making me feel like what you have to say is just as important to me as it is to you. Keep it up!

  14. Sweety says:

    Thanks for the source code. They are very helpful :)

  15. aparadekto says:

    Hey, I can’t view your site properly within Opera, I actually hope you look into fixing this.

  16. Anonymous says:

    You saved the day. I looked all over for how to do this. You are right, it is so simple once you figure it out! THANK YOU

  17. Berry says:

    You certainly deserve a round of applause for your post and more specifically, your blog in general. Very high quality material

  18. winan says:

    I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.

  19. MD says:

    Hey Nick,

    Great post! Lovely use of physics. Wondering if you might share the source in the video at the bottom of the post?

    Cheers,
    //MD

  20. myrddin says:

    Thank you, very useful !
    If I want add other planets (with gravity), do I need multiply or add forces ?
    Thank you.

  21. This what I gathered from your tutorial while converting it to Lua for love2d and hump.

    ship = bodies[1]
    shipVec = vector(ship:getX(),ship:getY())
    planet = bodies[2]
    planetVec = vector(planet:getX(),planet:getY())

    distance = planetVec – shipVec

    force = 250 / distance:len2()
    normforce = force*distance
    bodies[1]:applyImpulse(normforce.x, normforce.y,ship:getX(),ship:getY())

  22. mark says:

    thanks so much. i was actually working on something like this for my app.

  23. Joel says:

    This is great, just what we were looking for for a project we are working on. Many thanks man!

  24. Oliver says:

    Hey, I was looking to implement radial gravity physics and this is exactly what i needed. Thanks very much, the simplicity of your solution is fantastic.

  25. Igor Ranieri says:

    You just saved my day mate :)

  26. vishesh says:

    thanks man,
    was breaking my head on this,
    very useful code this,
    :)

Twitter Me