确定手指旋转方向

3
我正在尝试确定用户在屏幕上以圆形运动方式绕指旋转的方向。我目前尝试使用叉积并使用z分量来确定用户旋转的方向。这产生的结果对于旋转的下半部分有效,并且在旋转的上半部分上是相反的。
有人能否解释一下我做错了什么?
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);
    }

我现在无法测试代码,也无法理解Vector3.Cross。我知道它的作用以及你想要做什么,但我无法测试以确保出了什么问题。你可以尝试在代码中的ROTATE RIGHT/LEFT之前添加Debug.Log("Crossvalue: " + cross);。这可能会让你对发生了什么和出了什么问题有一些见解。 - Joetjah
我认为您可以通过这篇帖子找到一种解决方法。 - Jordan Montel
1个回答

0

我以前做过类似的东西,看起来你离成功很近了。这是我使用过的代码,用于确定用户是否在一个方向上完成了完整的 360 度旋转。请注意,这是 C++ 代码,但这个思路应该会有所帮助。

            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;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接