Libgdx Box2D平滑角度旋转

时间:2014-07-30 14:16:31

标签: rotation libgdx box2d smooth

我正在开发libgdx中的飞行游戏,您可以使用鼠标光标控制鸟的旋转。此时鸟会立即旋转到光标位置。我的目标是让鸟类旋转稍微滞后一点,让飞行看起来更加平滑。旋转阻尼不起作用,因为我使用的是setTransform()方法。我会对任何建议感到高兴,这是控制旋转的代码,它在render方法中被称为每一帧:

    //get cursor pos
    cursorPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
    camera.unproject(cursorPos);
    //get bird pos
    birdPos.set(body.getPosition().x, body.getPosition().y, 0);
    //birdpos-cursorpos
    birdPos.sub(cursorPos);
    //new vector which is pointing from bird to cursor
    direction.set(birdPos.x, birdPos.y);
    //get current linear velocity
    movement.set(body.getLinearVelocity());
    //apply rotation to bird if cursor is not too close
    if (Math.sqrt(Math.pow(direction.x, 2) + Math.pow(direction.y, 2)) > 1) {
        body.setTransform(body.getPosition().x, body.getPosition().y,   direction.angleRad());
        }

2 个答案:

答案 0 :(得分:0)

不是跟踪你的鸟的xSpeed和ySpeed,而是给它一个标题(以度为单位)和一个速度。每当您需要移动鸟时,使用基本触发将该标题和速度转换为xSpeed和ySpeed。

然后让你的鸟儿走得更顺畅,找出从鸟到目标的“目标前进”(再次,以度为单位)。随着时间的推移改变你的鸟的头部,直到它与目标标题匹配。

答案 1 :(得分:0)

我假设此代码在您的更新循环中。

创建实例变量angleTarget。

每个更新循环:

  1. 将angleTarget设置为鼠标角度。

  2. 使用setPosition将实际的身体角度设置为:body.angle = body.angle +(angleTarget - body.angle)* 0.05

  3. 通常将该表达式放在一个名为lerp的函数中,该函数代表线性插值。查找'lerp function'以了解更多信息。