Game Development Community

Pinch Zoom.. almost

by Justin Mosiman · in iTorque 2D · 05/26/2009 (6:26 pm) · 3 replies

Hey guys,

Does anybody have any idea of an algorithm that is similar to Apples pinch to zoom gesture? I am working on a resource for it, and I feel that I am really close, I just need to come up with a better algorithm. Currently my algorithm is really sensitive, if I move one of my fingers a few pixels it can zoom in/out all of the way.

Here's the algorithm that I have come up with:
void t2dPlaySceneWindow::pinchZoom()
{
    F32 currentPinchDistance = distance(mFirstFinger, mSecondFinger);
    F32 targetZoom = getCurrentCameraZoom();
   
    if(currentPinchDistance < mPreviousPinchDistance){
        // Zoom out
        F32 zoomLength = getCurrentCameraZoom() - mMinCameraZoomLimit;
        targetZoom -= 0.0005 * zoomLength * currentPinchDistance;      
    }else if(currentPinchDistance > mPreviousPinchDistance){
        // Zoom in
        F32 zoomLength = mMaxCameraZoomLimit - getCurrentCameraZoom();
        targetZoom += 0.0005 * zoomLength * currentPinchDistance;
    }
   
    if(targetZoom < mMinCameraZoomLimit)
        targetZoom = mMinCameraZoomLimit;
    else if(targetZoom > mMaxCameraZoomLimit)
        targetZoom = mMaxCameraZoomLimit;
   
    mPreviousPinchDistance = currentPinchDistance;
   
    setTargetCameraZoom(targetZoom);
    startCameraMove(0.0f);
}

The idea behind this algorithm is that the less space you have to zoom (zoomLength), the less a difference in the pinch difference makes. I probably could come up with an algorithm by hacking this one together and multiplying/adding different numbers to my current formulas, but I first wanted to see if the community had any elegant ideas.

Thanks,
Justin

#1
01/16/2011 (5:13 pm)
How did this turn out Justin? Have you further developed your algorithm?



#2
01/18/2011 (3:25 am)
Hey Rennie,

Yes, I ended up getting a pretty good version of it in War Evolved. I tried to create a resource for it in iTGB 1.2 a long time ago, but unfortunately nobody else could get it to work exactly like I could. I worked with Dave Calabrese to try to get it sorted out, but nothing ever came from it. I must have left out some piece of code and could never track down where it was.
#3
01/19/2011 (3:52 am)
Hmm ok. Well in a month or so I want to apply this feature to my next game so I will look into it more then. Your logic here is a good base to work on.

A couple of questions though.
-How does the device detect 2 fingers?
-What are the "F32"'s etc you have in? I am relatively new to coding so forgive my ignorance.