Search code examples
androidlibgdxbox2dgame-physics

LibGdx Setting Velocity


How can I set Linear Velocity in only X direction without effecting the motion of Y Direction to a body in LIBGDX Box2D. I applied an impulse to the body to make it jump now I want to move it towards right or left I tried applying the following method:

setLinearVelocity(Vector2)

But it is stopping the vertical motion. Thanks


Solution

  • Just retrieve the current velocity via the getter, manipulate it and set the new one.

    Vector2 velocity = body.getLinearVelocity().cpy();
    velocity.set(500, velocity.y);
    body.setLinearVelocity(velocity);