applyLinearImpulse不工作(Box2d)

时间:2014-10-27 21:57:11

标签: java android libgdx box2d

我有一个物体使用与台球相同的物理。这是完整的代码:

    BodyDef bddDef = new BodyDef();
    bddDef.type = BodyType.DynamicBody;
    bddDef.position.x = Position.x;
    bddDef.position.y = Position.y;

    bddDef.linearDamping = .03f;
    bddDef.angularDamping = .09f;
    bddDef.gravityScale = 10F;

    bdBox = wWorld.createBody(bddDef);

    CircleShape csCircle = new CircleShape();

    csCircle.setRadius(this.SIZE_RADIUS);
    csCircle.getPosition().x = bddDef.position.x;
    csCircle.getPosition().y = bddDef.position.y;

    FixtureDef fxtDef = new FixtureDef();
    fxtDef.density = 20.00f; 
    fxtDef.friction = 0f;
    fxtDef.restitution = 0.3f;
    fxtDef.shape = csCircle;

    bdBox.setBullet(true);

    bdBox.resetMassData();

    this.fxtSensorFixture = bdBox.createFixture(fxtDef);
    csCircle.dispose();

像这样设置applyLinearImpulse

public void update(float x, float y){
    if(distance(x,y)<=SIZE_RADIUS)
        return;
    Vector2 vec =  new Vector2(x,y);
    getBody().applyLinearImpulse(this.localToStageCoordinates(vec), getBody().getPosition(), true);
}

但物体没有移动或移动速度非常慢。我做错了什么?

1 个答案:

答案 0 :(得分:1)

找到解决方案。如果有人遇到同样的问题,你可以尝试

    public void update(float x, float y){
    Vector2 pos = bdBox.getPosition();
    bdBox.applyLinearImpulse(x*bdBox.getMass(), y*bdBox.getMass(), pos.x, pos.y, true);
}