Here is a demo on how to apply different gravity on a different body. This is really very simple. If you want to want a body to act like it is in a zero-gravity environment just apply a force equal to the force acting on it due to gravity in the opposite direction. This is a really neat feature to implement in a game. Check it out.

But remember this force must be applied on the body before the step() because if you apply the force before time step function the body will move in normal gravity for one time step and then the force will be cancelled. The body will continue to fall down slowly even if you cancel the force.

function update() {
    var fx = body.GetMass() * world.GetGravity().x;
    var fy = body.GetMass() * world.GetGravity().y;
    body.ApplyForce(new b2Vec2(-fx,-fy), body.GetWorldCenter());
    // 'body' is the name of the body whose gravity is to be cancelled
    // force applied is equal to product of mass & acceleration due to gravity
    // see that the force is applied before world.Step() is called

    world.Step(1 / 60, 10, 10); 

Here's the link to the demo.

By administrator at 12:52:44 PM 0 Comment(s)


There are no comments yet. Be the first one to add a comment!

Add a Comment

Please enter the email address.Invalid format. (Won't be Displayed)
Notify me of followup comments
Enter the displayed Code: captcha