LibGDX - 限制透视相机的缩放

4

如何在LibGDX中限制透视相机的缩放是最好的方法?我有一个在太空中的星球,我需要放大/缩小它。 缩放效果很好,但我必须限制它,以防止行星离用户太近或太远。现在,我正在使用标准的CameraInputController来放大/缩小并使用以下代码进行限制:

protected boolean pinchZoom (float amount) {
    if(rho>25.f && rho<60.f){           
        return zoom(pinchZoomFactor * amount);
    }

    camera.update();
    rho = calculateRho();

    if(rho<=25.0){          
        while(rho<=25.0){
            zoom(-.1f);
            camera.update();
            rho = calculateRho();
        }           
    }

    if(rho>=60){            
        while(rho>=60.0){
            zoom(.1f);
            camera.update();
            rho = calculateRho();
        }           
    }
}

private float calculateRho(){
         return (float) Math.sqrt(Math.pow(camera.position.x, 2)+
                Math.pow(camera.position.y, 2)+Math.pow(camera.position.z, 2)); 
}

使用这段代码时,我的相机有时会微微晃动。因此,我找到了另一种方法。
1个回答

3

我已经找到了解决方案。我只需要声明一个变量来累加输入的变量amount,然后检查这个值的范围。

float currentZoom=0;
private final float maxZoom = .5f;
private final float minZoom = -2.1f;

protected boolean pinchZoom (float amount) {
    currentZoom += amount;      
    if(currentZoom>=maxZoom) currentZoom=maxZoom;
    if(currentZoom<=minZoom) currentZoom=minZoom;

    if(currentZoom>minZoom && currentZoom<maxZoom){
        return zoom(pinchZoomFactor * amount);
    }
    return false;
}

对我来说,它完美地工作!希望这个解决方案能帮助其他人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接