flying object

just to the main problem, since in box2d the object will automatically drop to the bottom (red arrow), then i want to create my object flying and stay with the same height (the green one). like this image.

Image

and here is my code that i got from some web:
Code:
gravityOffset.Multiply(-body.GetMass());
body.ApplyForce(gravityOffset, body.GetWorldCenter());

my object is fly but its fly away and never come back like a rocket.

i modified the code like this :
Code:
gravityOffset.x = 0;
gravityOffset.y = 0;
body.ApplyForce(gravityOffset, body.GetWorldCenter());

but its still fly to up slowly..

any one can help?
hm... but what is gravityOffset? This should work:

body.ApplyForce( -gravity * body.GetMass(), body.GetWorldCenter() );

...where 'gravity' is the gravity vector of the world. If it moves up very slowly, check that you have applied this force before the world step, not after. If you apply it after, the body can get one time step at the normal gravity and start to move down a little.
irresistible force wrote:
hm... but what is gravityOffset? This should work:

body.ApplyForce( -gravity * body.GetMass(), body.GetWorldCenter() );


gravityOffset is b2vec2 variable. and also i already try your code and got an error. is use gravity value that get from world.getGravity(), is it right??
Since this is Flash, you would need to use this code (same as irresistible force's code, but for Flash):

Code:
var tForce:b2Vec2 = world.GetGravity().Copy();
tForce.Multiply(body.GetMass());
body.ApplyForce(tForce, body.GetWorldCenter());

(irresistible force's code would only work in C++ because of overloaded operators)
after read again all of post above and noticed about gravity and force value of y, today i try again with this code..
Code:
var gravityOffset:b2Vec2;

Code:
if(flying)
{
  gravityOffset.x = 0;
  gravityOffset.y = -body.GetLinearVelocity().y;
  body.ApplyForce(gravityOffset , body.GetWorldCenter() );
}

and its working, the body fly with the same height..
thanks..

now, i must make it fall with the speed/force as same as before the body fly..
any idea about this?

Original thread: https://box2d.org/forum/viewtopic.php?t=8372