7
\$\begingroup\$

I found the example code for the GestureListener class, but I don't understand the zoom method:

private float initialScale = 1;

public boolean zoom (float originalDistance, float currentDistance) {
    float ratio = originalDistance / currentDistance; //I get this
    camera.zoom = initialScale * ratio; //This doesn't make sense to me because it seems like every time you pinch to zoom, it resets to the original zoom which is 1. So basically it wouldn't 'save' the zoom right?
    System.out.println(camera.zoom); //Prints the camera zoom
    return false;
}

Am I not interpreting this right?

\$\endgroup\$
  • 1
    \$\begingroup\$ Shouldn't it be camera.zoom ×= ratio; ? \$\endgroup\$ – bornander Oct 5 '15 at 15:36
  • \$\begingroup\$ I think this interface and the GestureDetector class is not very well thought through. I'm having the same problem, and I'm now trying to figure out a good solution. \$\endgroup\$ – Adam Toth May 29 '16 at 16:19
  • \$\begingroup\$ I think the interface would need a zoomStart(float distance) and a zoomStop(float initialDistance, float finalDistance) method \$\endgroup\$ – Adam Toth May 29 '16 at 16:23
1
\$\begingroup\$

Each time you put your fingers on the screen the originalDistance / currentDistance will result in the value 1.

If you use this value directly the zoom would always start at zoom = 1.0f.

To solve this issue you should store the current zoom factor at the start of the zoom and multiply this with the new calculated ratio. So: on touchDown take the current zoom: initialScale = Camara.zoom;.

\$\endgroup\$
0
\$\begingroup\$

initialScale looks like a constant used to scale everything in the game.

zoom function calculates originalDistance / currentDistance value, applies it to Camera and prints it out.

It's hard to tell more without seeing other affected code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.