Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libgdx two-finger pan instead of single finger?

I've overridden the LibGdx Pan gesture method for a different functionality (a selector). However, I want the pan functionality to use two fingers(pointers) instead. Is this possible?

It's the same question as this post, however his is specific to iPhone and not LibGdx: How to implement the traditional imageView-inside-a-scrollView pan gesture with two fingers instead of one?

the pan() method will only fire with one finger, not two. I was thinking of keeping track of number of fingers used, by setting a variable in touchDown() using the int pointer variable, however the pan() method will not fire when there are 2 fingers in use.

Any suggestions? Cheers

like image 465
Jammo Avatar asked Jun 12 '26 11:06

Jammo


1 Answers

After 3+ years of research I finally made it.

@Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
    // Calculate distances
    float initialDistance = initialPointer1.dst(initialPointer2);
    float distance = pointer1.dst(pointer2);
    // Calculate pinch coordinates
    float initialPinchX = (initialPointer1.x + initialPointer2.x) / 2;
    float initialPinchY = (initialPointer1.y + initialPointer2.y) / 2;
    float pinchX = (pointer1.x + pointer2.x) / 2;
    float pinchY = (pointer1.y + pointer2.y) / 2;
    // This to avoid first time zooming or panning horrible behavior
    if (lastZoomDistance == 0) {
        lastZoomDistance = initialDistance;
    }
    if (lastPinchX == lastPinchY && lastPinchX == 0) {
        lastPinchX = initialPinchX;
        lastPinchY = initialPinchY;
    }
    // Zoom
    float distanceDifference = distance - lastZoomDistance;
    camera.zoom -= distanceDifference / 300;
    // Pan
    float deltaX = (pinchX - lastPinchX) * camera.zoom;
    float deltaY = (pinchY - lastPinchY) * camera.zoom;
    camera.translate(-deltaX, deltaY);
    // We need to update these for future calculations
    lastZoomDistance = distance;
    lastPinchX = (pointer1.x + pointer2.x) / 2;
    lastPinchY = (pointer1.y + pointer2.y) / 2;
    return false;
}
like image 133
Luis Fernando Frontanilla Avatar answered Jun 14 '26 01:06

Luis Fernando Frontanilla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!