2017-12-05 17 views
0

私はLibGDXでゲームを作成しており、マップシステムとしてTiledを使用しています。カメラを地図にクランプする(ズームの問題)

私は、TiledMapの範囲内にOrthographicCameraを格納しようとしています。私はこれを達成するためにMathUtils.clampを使用します。カメラが1.0fの通常のズーム状態にあるとき、それは完全に動作します。しかし、カメラをさらにズームインすると、.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)); 

私の質問:カメラが正しくクランプされるようにズーム値をクランプコードに含めるにはどうすればよいですか?何か案は?

ありがとうございました! - ジェイク

答えて

0
あなたがズームで世界のサイズを掛けてください

、ないカメラ位置:

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); 
    } 
関連する問題