Skip to content

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

Description

@bycross028

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
    }

       ...
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions