2013-08-21 49 views
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); 
    } 
+0

我现在无法测试代码,无法绕过'Vector3.Cross'包装。我明白它做了什么以及你想做什么,但我无法测试它以确定发生了什么问题。您可以尝试在代码中的'ROTATE RIGHT/LEFT'之前添加'Debug.Log(“Crossvalue:”+ cross)“。这可能会让你对发生什么事情和发生错误有所了解。 – Joetjah

+0

我认为你可以找到一个方法来做到这一点与 [此帖] [1] [1]:http://stackoverflow.com/questions/18579902/uiimage-which-follow-touches ++ –

+0

可能重复[如何确定多边形点的列表是否按顺时针顺序?](http://stackoverflow.com/questions/1165647/how-to-determine-if-a-list -of-多边形点-是式时针顺序) –

回答

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