LibGDX平移有时会跳跃

时间:2014-11-23 22:38:38

标签: android camera libgdx gesture

我正在研究基于磁贴的游戏,它应该是可滚动的,但只能在世界范围内。所以我设置了pan()方法,如同几个示例/教程所示,但它并不总是正常工作。有时它会跳回到最后一个手势的起点,或者只是移动得很慢。此外,边界也不起作用。也许有人可以指出我犯的错误。

public void pan(float x, float y, float deltaX, float deltaY) { 
    moveCamera(x, y);   
}   

private void moveCamera(float x, float y) {
    Vector3 new_position = getNewCameraPosition((int) x, (int)y);

    if(!cameraOutOfLimit(new_position)) 
        this.getViewport().getCamera().translate(new_position.sub(this.getViewport().getCamera().position));

    lastTouchDown.set(x, y, 0);
}

private Vector3 getNewCameraPosition(int x, int y) { 
    Vector3 newPosition = lastTouchDown;        
    newPosition.sub(x, y, 0);   
    newPosition.y = -newPosition.y;     
    newPosition.add(this.getViewport().getCamera().position);

    return newPosition;
}

private boolean cameraOutOfLimit( Vector3 position ) {
    int x_left_limit = (int) (Global.SCREEN_WIDTH / 2);
    int x_right_limit = (int) (Global.COLS_OF_TILES * Global.TILE_WIDTH - (Global.SCREEN_WIDTH / 2));
    int y_bottom_limit = (int) (Global.SCREEN_HEIGHT / 2);
    int y_top_limit = (int) (Global.ROWS_OF_TILES * Global.TILE_HEIGHT - Global.SCREEN_HEIGHT / 2);

    if( position.x < x_left_limit || position.x > x_right_limit )
        return true;
    else if( position.y < y_bottom_limit || position.y > y_top_limit )
        return true;
    else
        return false; 
}

1 个答案:

答案 0 :(得分:1)

上面的代码对我来说似乎很复杂,我真的不明白你为什么修改一个名为lastTouchPosition的向量,其方式与触摸位置无关。

我会做这样的事情。无论何时想要移动相机,这些方法都会锁定目标X和Y位置。

float clampCamTargetX(float x) {
    x = Math.max(x, (int) (Global.SCREEN_WIDTH / 2));
    x = Math.min(x, (int) (Global.COLS_OF_TILES * Global.TILE_WIDTH - (Global.SCREEN_WIDTH / 2)));
    return x;
}

float clampCamTargetY (float y){
    y = Math.max(y,(int)(Global.SCREEN_HEIGHT/2));
    y = Math.min(y,(int)(Global.ROWS_OF_TILES*Global.TILE_HEIGHT-Global.SCREEN_HEIGHT/2));
    return y;
}

然后,如果你想要平移它,你会做这样的事情:

void panCamera(float deltaX, float deltaY) {
    Camera camera = this.getViewport().getCamera();
    Vector3 camPosition = camera.position;
    camPosition.x = clampCamTargetX(camPosition.x + deltaX);
    camPosition.y = clampCamTargetY(camPosition.y + deltaY);
    camera.update();
}

或者,如果您想要一个完整的解决方案,将相机平稳地移动到最后触摸的位置,请尝试以下操作:

float startXCam, startYCam, targetXCam, targetYCam;
float elapsedTimeCam;
boolean panningCam = false;
static final float CAM_PAN_DURATION = 0.4f;

public void render (){
    //...
    panCameraToTouchPoint(Gdx.graphics.getDeltaTime());
    //...
}

void panCameraToTouchPoint (float deltaTime){
    Camera camera = getViewport().getCamera();
    Vector3 camPosition = camera.position;
    if (Gdx.input.justTouched()) {
        startXCam = camPosition.x;
        startYCam = camPosition.y;
        targetXCam = clampCamTargetX(Gdx.input.getX());
        targetYCam = clampCamTargetY(Gdx.input.getY());
        elapsedTimeCam = 0;
        panningCam = true;
    }

    if (panningCam){
        elapsedTimeCam += deltaTime;
        float alpha = elapsedTimeCam / CAM_PAN_DURATION;
        if (alpha >= 1){
            alpha = 1;
            panningCam = false;
        }
        camPosition.x = Interpolation.pow2Out.apply(startXCam, targetXCam, alpha);
        camPosition.y = Interpolation.pow2Out.apply(startYCam, targetYCam, alpha);
        camera.update();
    }
}