Managing multiple gravities with Box2D
2009-10-20 00:00:00 来源:WEB开发网The only interesting lines are line 17 where I declare the gravity vector and lines 64 and 74 where I assign names to the objects… box for boxes and circle for circles
And this is the result:
As you can see, the world is ruled by standard gravity, but as I said I want boxes to be ruled by normal gravity and circles to be ruled by inverted gravity
Now, let’s see the first method, called (by myself)….
The antagonist forces method
As the name means, the principle is applying an antagonist force to circles in order to simulate a reverse gravity. Since the default gravity is (0,10), I am going to apply a (0,-20) force to all circles.
This is the updated Update function:
public function Update(e:Event):void {
var ant_gravity = b2Vec2;
m_world.Step(m_timeStep, m_iterations);
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next) {
if (bb.GetUserData()!=null) {
if (bb.GetUserData().name=="circle") {
ant_gravity = new b2Vec2(0.0,-20.0*bb.GetMass());
bb.ApplyForce(ant_gravity,bb.GetWorldCenter());
}
}
}
}
更多精彩
赞助商链接