0

我正在 LibGDX 中创建游戏,并使用 Tiled 作为我的地图系统。

我试图在我的 TiledMap 范围内包含一个 OrthographicCamera。我使用 MathUtils.clamp 来实现这一点。当相机处于 1.0f 的正常变焦时,它工作得很好。然而,当相机进一步放大时,比如说 0.75f,相机被夹在错误的位置,因为它没有缩放值的信息。

position.x = MathUtils.clamp(position.x * (gameScreen.gameCamera.camera.zoom), gameScreen.gameCamera.camera.viewportWidth / 2, gameScreen.mapHandler.mapPixelWidth - (gameScreen.gameCamera.camera.viewportWidth / 2));
position.y = MathUtils.clamp(position.y * (gameScreen.gameCamera.camera.zoom), (gameScreen.gameCamera.camera.viewportHeight / 2), gameScreen.mapHandler.mapPixelHeight - (gameScreen.gameCamera.camera.viewportHeight / 2));

我的问题:如何在我的钳位代码中包含缩放值,以便正确钳位相机?有任何想法吗?

谢谢!- 杰克

4

1 回答 1

1

您应该乘以缩放世界大小,而不是相机位置:

    float worldWidth = gameScreen.mapHandler.mapPixelWidth;
    float worldHeight = gameScreen.mapHandler.mapPixelHeight;
    float zoom = gameScreen.gameCamera.camera.zoom;
    float zoomedHalfWorldWidth = zoom * gameScreen.gameCamera.camera.viewportWidth / 2;
    float zoomedHalfWorldHeight = zoom * gameScreen.gameCamera.camera.viewportHeight / 2;

    //min and max values for camera's x coordinate
    float minX = zoomedHalfWorldWidth;
    float maxX = worldWidth - zoomedHalfWorldWidth;

    //min and max values for camera's y coordinate
    float minY = zoomedHalfWorldHeight;
    float maxY = worldHeight - zoomedHalfWorldHeight;

    position.x = MathUtils.clamp(position.x, minX, maxX);
    position.y = MathUtils.clamp(position.y, minY, maxY);

请注意,如果可见区域可以小于世界大小,那么您必须以不同的方式处理此类情况:

    if (maxX <= minX) {
        //visible area width is bigger than the worldWidth -> set the camera at the world centerX
        position.x = worldWidth / 2;
    } else {
        position.x = MathUtils.clamp(position.x, minX, maxX);
    }

    if (maxY <= minY) {
        //visible area height is bigger than the worldHeight -> set the  camera at the world centerY
        position.y = worldHeight / 2;
    } else {
        position.y = MathUtils.clamp(position.y, minY, maxY);
    }
于 2017-12-05T09:59:54.097 回答