【问题标题】:LibGDX - restrict zooming of perspective cameraLibGDX - 限制透视相机的缩放
【发布时间】:2014-09-20 19:40:09
【问题描述】:

在 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)); 
}

使用此代码,我的相机有时会稍微抖动。所以,我找到了另一种方法。

【问题讨论】:

    标签: android camera libgdx opengl-es-2.0 perspectivecamera


    【解决方案1】:

    我找到了解决方案。我只是声明了对输入变量 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;
    }
    

    它非常适合我!我希望这个解决方案对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-05-27
      • 2015-07-24
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多