Libgdx播放器旋转搞乱了

时间:2016-07-20 11:10:23

标签: java android libgdx

我试图编写一个向鼠标旋转并以一定速度向鼠标移动的播放器。我正在使用正交相机,但是只要我将相机的位置设置为等于玩家位置,玩家旋转就会停止工作并遍布整个地方。我相信它可能与使用屏幕的鼠标坐标有关,而不是与世界x和y有关。我已经尝试取消投影Gdx.inout.getY / X无法取胜。任何帮助深表感谢。

我正在使用此代码关注播放器:

    cam.position.x = player.getX();
    cam.position.y = player.getY();
    cam.update();

And this code for rotation:
float mouseX = Gdx.input.getX();
                float mouseY = (480 - Gdx.input.getY());

                float originX = getWidth() / 2 + position.x;
                float originY = getHeight() / 2 + position.y;

                double angle = Math.atan2(mouseY - originY, mouseX - originX) * (180/Math.PI) + 90;

                if (angle < 0) 
                      angle += 360;

                rotation = (float) angle;

                direction.x = mouseX - position.x;
                direction.y = mouseY - position.y;

                double hyp = Math.sqrt(direction.x * direction.x + direction.y * direction.y);
                direction.x /= hyp;
                direction.y /= hyp;

                position.x += direction.x * 2;
                position.y += direction.y * 2;

1 个答案:

答案 0 :(得分:1)

您是否尝试过投射(或取消投影)积分?

ViewPort viewPort = new ... // which ever ViewPort you're using

Vector2 worldPoint = new Vector2(x, y);
Vector2 projected = viewPort.project(worldPoint);

Vector2 screenPoint = new Vector2(x, y);
Vector2 unprojected = viewPort.unproject(screenPoint);