3

I'm trying to determine which way a user is rotating their finger around the screen in a circular motion. I'm currently trying to use the cross product and using the z component to determine which way the user is rotating. This is producing results which are working for the bottom half of the and are reversed on the top half of the rotation.

Can anyone shed some light on what I'm doing incorrectly?

if ( ( Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved && GameStateManager.CurrentGameState == GameState.Minigame) )
    {
        Vector3 touchPos = Vector3.zero;
        if( Camera.mainCamera != null )
        {
            touchPos = Camera.mainCamera.ScreenToWorldPoint( new Vector3( Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, Camera.mainCamera.nearClipPlane ));
        }

        if( prevTouchPos == Vector2.zero )
        {
            prevTouchPos = new Vector2( touchPos.x, touchPos.y );
            return;
        }

        //need angle between last finger position and this finger position
        Vector2 prevVec = new Vector2( prevTouchPos.x - transform.position.x, prevTouchPos.y - transform.position.y );
        Vector2 currVec = new Vector2( touchPos.x - transform.position.x, touchPos.y - transform.position.y );

        float ang = Vector2.Angle(prevVec, currVec);
        Vector3 cross = Vector3.Cross( new Vector3(prevVec.x, prevVec.y, 0), new Vector3(currVec.x, currVec.y, 0));

        Debug.Log(cross.normalized);
        if (cross.z < 0)
        {
            Debug.Log("Prev Vec: " + prevVec);
            Debug.Log("Curr Vec: " + currVec);

            Debug.Log("ROTATE RIGHT");
            transform.Rotate( 0, 0, ang);
        }
        else
        {
            Debug.Log("Prev Vec: " + prevVec);
            Debug.Log("Curr Vec: " + currVec);
            Debug.Log("ROTATE LEFT");
            transform.Rotate( 0, 0, -ang);
        }

        //Debug.Log(ang);
        //Debug.Log( "TouchPos: " + touchPos );




        prevTouchPos = new Vector2( touchPos.x, touchPos.y);
    }
Willie Wight
  • 171
  • 1
  • 6
  • I can't test code right now and I can't wrap my mind around the `Vector3.Cross`. I understand what it does and what you are trying to do, but I can't test it to make sure what is going wrong. You could try to add `Debug.Log("Crossvalue: " + cross);` right before `ROTATE RIGHT/LEFT` in your code. That might give you some insight on what is happening and going wrong. – Joetjah Aug 22 '13 at 08:18
  • I think you can find a way to do that with [this post][1] [1]: http://stackoverflow.com/questions/18579902/uiimage-which-follow-touches ++ – Jordan Montel Sep 13 '13 at 09:57
  • possible duplicate of [How to determine if a list of polygon points are in clockwise order?](http://stackoverflow.com/questions/1165647/how-to-determine-if-a-list-of-polygon-points-are-in-clockwise-order) – Nick Udell Jun 05 '14 at 11:50

1 Answers1

0

I've done stuff similar to this in the past, it looks like your pretty close, here is the code that i've used, which determines if a user has done a full 360 in one direction or another. Note this is in C++, but the idea should help.

            thumbDir.Cross( thumbCur, thumbPrev );
    float z = clamp( thumbDir.z, -1.0f, 1.0f );
    float theta = asin( z );

    if ( z > 0.0f )
    {
        // clockwise
        if ( fDegrees > 0.0f )
            fDegrees = -theta; // Reset angle if changed
        else
            fDegrees -= theta;
    }
    else if ( z < 0.0f )
    {
        // counter-clockwise
        if ( fDegrees < 0.0f )
            fDegrees = -theta;  //Reset angle if changed
        else
            fDegrees -= theta;
    }