Box2D Forums

It is currently Mon May 20, 2013 8:11 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sat Mar 17, 2012 9:46 pm 
Offline

Joined: Fri Mar 09, 2012 1:43 pm
Posts: 69
Can I simulate the gravitational field of a planet so that if I make a box2d body pass close enough to a massive object it will be pulled in or affected by that planet's field?


Top
 Profile  
 
PostPosted: Sun Mar 18, 2012 12:03 am 
Offline
Site Admin

Joined: Thu Sep 06, 2007 12:34 am
Posts: 2931
Have you seen this?

http://space.angrybirds.com/announcement/


Top
 Profile  
 
PostPosted: Sun Mar 18, 2012 12:34 am 
Offline

Joined: Fri Mar 09, 2012 1:43 pm
Posts: 69
Yes...but what does it mean with respect to my question?


Top
 Profile  
 
PostPosted: Sun Mar 18, 2012 9:09 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
You can apply a force every time step, calculated from the masses of the two bodies and how far apart they are.
http://en.wikipedia.org/wiki/Newton's_l ... ravitation
http://stackoverflow.com/questions/6758 ... sing-box2d


Top
 Profile  
 
PostPosted: Thu Mar 22, 2012 9:27 am 
Offline

Joined: Thu Mar 22, 2012 9:15 am
Posts: 2
Hello, this may not be exactly what you are looking for, but you can try this:

// Position of Planet
b2Vec2 planetPos = planetLocation;
// Position of Body
b2Vec2 bodyPos = body->GetWorldCenter();

/***** To get the vector from the body to the planet, you subtract the planet position from the body position. For example, if the body's position is at the screen center, (160, 240), and the planet is at the upper right (300, 450), then subtracting the body position from the planet's position results in (300 - 160, 450 - 240) = (140, 210). ******/
b2Vec2 bodyToPlanetDirection = planetPos - bodyPos;

// Get the distance and store it, you'll see why later.
float distance = bodyToPlanetDirection.Length();
// Normalize turns your vector into a unit vector, which is of length 1 - or one unit. This allows you to multiply it with a fixed factor later on, to create a constant force vector pointing in the direction of the planet.
bodyToPlanetDirection.Normalize();

// Real gravity falls off by the square over distance
float distanceSquared = distance * distance;
// These numbers are arbitrary, feel free to tweak them until you get the result you want.
b2Vec2 force = ((1.0f / distanceSquared) * 20.0f) * bodyToPlanetDirection;
body->ApplyForce(force, body->GetWorldCenter());

Hope this helps!

Regards,
Gabriel Lim


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group