lukehutch / android-multitouch-controller

Automatically exported from code.google.com/p/android-multitouch-controller
69 stars 24 forks source link

Fix: Rotation relative to the midpoint of the two touches #20

Open bycross028 opened 9 years ago

bycross028 commented 9 years ago

MultiTouchController.java:

public class MultiTouchController<T> {
     ...
     float mStartAbsoluteAng; // <----------
     ...
    private void anchorAtThisPositionAndScale() {
        ...
        /* add at the end of the function */
        mStartAbsoluteAng = mCurrXform.angle;  // <----------
    }

   ...
   private void performDragOrPinch() {
        /* Don't do anything if we're not dragging anything*/
        if (selectedObject == null)
            return;

        /* Calc new position of dragged object*/
        float currScale = !mCurrXform.updateScale ? 1.0f : mCurrXform.scale == 0.0f ? 1.0f : mCurrXform.scale;
        extractCurrPtInfo();

        float newPosX = mCurrPtX - startPosX * currScale;
        float newPosY = mCurrPtY - startPosY * currScale;
        float newScale = startScaleOverPinchDiam * mCurrPtDiam;
        float newScaleX = startScaleXOverPinchWidth * mCurrPtWidth;
        float newScaleY = startScaleYOverPinchHeight * mCurrPtHeight;
        float newAngle = startAngleMinusPinchAngle + mCurrPtAng;

        /*
        * Fix: Rotation relative to the midpoint start
        */

        float[] deltaPosition = new float[2];
        deltaPosition[0] =  newPosX - mCurrPtX;
        deltaPosition[1] =  newPosY - mCurrPtY;

        float deltaAngle = newAngle - mStartAbsoluteAng;

        float[] deltaRotate = new float[2];
            deltaRotate[0] = (float)( deltaPosition[0] * Math.cos(deltaAngle)) -
                (float) ( deltaPosition[1] * Math.sin(deltaAngle));
        deltaRotate[1] = (float)( deltaPosition[0] * Math.sin(deltaAngle)) +
                (float) ( deltaPosition[1] * Math.cos(deltaAngle));

        newPosX = mCurrPtX + deltaRotate[0];
        newPosY = mCurrPtY + deltaRotate[1];

        /*
         * Fix: Rotation relative to the midpoint end
         */

        // Set the new obj coords, scale, and angle as appropriate (notifying the subclass of the change).
        mCurrXform.set(newPosX, newPosY, newScale, newScaleX, newScaleY, newAngle);

        boolean success = objectCanvas.setPositionAndScale(selectedObject, mCurrXform, mCurrPt);
        if (!success)
            ; // If we could't set those params, do nothing currently
    }

       ...
}