Calculate correct impluse or force to move a Box2D body to a specific position - Box2D

前端 未结 2 394
终归单人心
终归单人心 2021-01-07 07:12

i have a question about moving a Box2D body to a specific position without using this for example.

body->SetTransform(targetVector,body->GetAngle())
<         


        
2条回答
  •  無奈伤痛
    2021-01-07 08:14

    There is a way for single-time applied force to move by given distance (previous answer suggests that you can correct error in calculation by additional force in future frames):

    public static void applyForceToMoveBy(float byX, float byY, Body body) {
        force.set(byX, byY);
        force.sub(body.getLinearVelocity());
        float mass = body.getMass();
        force.scl(mass * 30.45f);
        body.applyForceToCenter(force, true);
    }
    

    Known limitations:

    1) LinearVelocity effect was not tested;
    2) calculation was tested with body linear damping = 0.5f. Appreciate, if somebody know how to add it into formula;
    3) magic number 30.45f - maybe this could be fixed by point 2 or by world frame delta.
    

提交回复
热议问题