Search code examples
javalibgdxbox2dgame-enginegame-physics

Move Body in Box2D upwards


I want to write a game like Flappy Bird again. So I had an body, with the bird and set his position at the start in the middle of the screen with the sprite on top of it. Now when the User tap on the screen, I want to move the bird upwards. But how can I make it? Body.setTransform(Vector, angle) is not what I want. Can you help me to solve this problem?


Solution

  • From the Box2D User Manual (v2.3.0):

    Forces and Impulses

    You can apply forces, torques, and impulses to a body. When you apply a force or an impulse, you provide a world point where the load is applied. This often results in a torque about the center of mass.

    void ApplyForce(const b2Vec2& force, const b2Vec2& point);
    void ApplyTorque(float32 torque);
    void ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point); 
    void ApplyAngularImpulse(float32 impulse);
    

    The manual has a lot of information for how short it is...it is definitely worth reading if you are going to use the engine.