Reputation: 7028
What is the best way to restrict zooming of perspective camera in LibGDX? I have a planet in space and I need to zoom in/out it. Zooming works great, but I have to limit it to prevent the planet to be so close to the user and far away from him. Now, I'm using standart CameraInputController
to zoom in/out and restrict it by using the following code:
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));
}
Using this code my camera shakes sometimes a little bit. So, I find another way.
Upvotes: 4
Views: 781
Reputation: 7028
I've found the solution. I just declare variable which sums input variable amount
and then I check range of this value.
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;
}
It works perfect for me! I hope this solution helps someone else.
Upvotes: 3