1

First of all, this is the first post I've written here on StackOverflow, and I'm actually new to programming, so if I say anything wrong, I'm sorry.

I tried and tried to make this work - I want to simulate a body that stays still on the air, and for that I use SetLinearVelocity(new b2Vec2(0, 0)).

As I read in other posts, seems that the gravity vector is important: it's a b2Vec2(0, 30); and I have timestep of 1/30 seconds.

The bit of code that handles the mechanic of that particular body is the following:

this.clocktick = function(deltaT) {
    this.body.SetLinearVelocity(new b2Vec2(0, 0));

    if(this.body.GetLinearVelocity().y != 15){
        console.debug(this.body.GetLinearVelocity().y);
    }
}

I find it funny that the body is always descending and I already had a situation that even though it's descending, the debug returns a value of 0.

I'd just like to hear from a solution or some other suggestion to simulate what i intend to simulate.

Thx

SetFreeByTruth
  • 819
  • 8
  • 23

1 Answers1

2

Rather than setting the velocity, you need to apply a force to cancel gravity. If you just set the velocity, gravity can still push it down a tiny bit every time step. The force needs to be the same as what gravity would apply, so it should take into account the gravity vector and the mass of the object:

body.ApplyForce( body.GetMass() * -world.GetGravity(), body.GetWorldCenter() );

... and you need to do this before every time step.

iforce2d
  • 8,194
  • 3
  • 29
  • 40