Hey everyone, I'm somewhat new to Box2D and I'm trying to utilize it in a game project. I'm working to simulate orbital gravity using an example posted by Nick Vellios here http://www.vellios.com/2010/06/06/box2d-and-radial-gravity-code/. I switched the syntax to AS3, but I'm getting weird results. Objects are drawn to the 'planets' at first, but then seem to sling shot around them:
http://gravitygame.pbworks.com/f/Orbital_Physics2.mov
http://gravitygame.pbworks.com/f/Orbital_Physics3.mov
I'm not sure why the objects won't just fall to the planet and stay there. Here's the code that I have in my update loop that handled the orbital gravity.
Any suggestions?
http://gravitygame.pbworks.com/f/Orbital_Physics2.mov
http://gravitygame.pbworks.com/f/Orbital_Physics3.mov
I'm not sure why the objects won't just fall to the planet and stay there. Here's the code that I have in my update loop that handled the orbital gravity.
Code:
for (var b:b2Body = m_world.GetBodyList();b;b = b.GetNext())
{
b.SetLinearDamping(10);
var ground:b2Body = planetFixture.GetBody();
var circle:b2CircleShape = (b2CircleShape)(planetFixture.GetShape());
var center:b2Vec2 = ground.GetWorldPoint(circle.GetLocalPosition());
var position:b2Vec2 = b.GetPosition();
center.Subtract(position);
var distance:b2Vec2 = center;
var force:Number = 150.0 / distance.LengthSquared();
distance.Normalize();
var F:b2Vec2 = new b2Vec2(distance.x*(force),distance.y*(force));
//trace(position.x+" and "+position.y);
b.ApplyForce(F,position);
}
{
b.SetLinearDamping(10);
var ground:b2Body = planetFixture.GetBody();
var circle:b2CircleShape = (b2CircleShape)(planetFixture.GetShape());
var center:b2Vec2 = ground.GetWorldPoint(circle.GetLocalPosition());
var position:b2Vec2 = b.GetPosition();
center.Subtract(position);
var distance:b2Vec2 = center;
var force:Number = 150.0 / distance.LengthSquared();
distance.Normalize();
var F:b2Vec2 = new b2Vec2(distance.x*(force),distance.y*(force));
//trace(position.x+" and "+position.y);
b.ApplyForce(F,position);
}
Any suggestions?