计算Unity3D中两个向量之间的实际角度

时间:2013-10-30 06:53:13

标签: vector unity3d

有没有办法计算Unity中两个3D矢量之间的实际角度? Vector3.Angle给出两个向量之间的最短角度。我想知道以顺时针方式计算的实际角度。

3 个答案:

答案 0 :(得分:17)

这应该是你需要的。 ab是要为其计算角度的向量,n将是您的平面的法线,以确定您将称为“顺时针/逆时针”的内容

float SignedAngleBetween(Vector3 a, Vector3 b, Vector3 n){
    // angle in [0,180]
    float angle = Vector3.Angle(a,b);
    float sign = Mathf.Sign(Vector3.Dot(n,Vector3.Cross(a,b)));

    // angle in [-179,180]
    float signed_angle = angle * sign;

    // angle in [0,360] (not used but included here for completeness)
    //float angle360 =  (signed_angle + 180) % 360;

    return signed_angle;
}

为简单起见,我重复使用Vector3.Angle,然后根据平面法线na和{{1}的叉积(垂直向量)之间的角度大小计算符号}。

答案 1 :(得分:1)

如果您只使用加法和减法,这很容易。看看这个:

/Degrees
  float signedAngleBetween (Vector3 a, Vector3 b, bool clockwise) {
  float angle = Vector3.angle(a, b);

  //clockwise
  if( Mathf.Sign(angle) == -1 && clockwise )
    angle = 360 + angle;

  //counter clockwise
  else if( Mathf.Sign(angle) == 1 && !clockwise)
    angle = - angle;
  return angle;
}

这就是我的意思: 如果我们是顺时针方向并且角度是负的,例如-170 为了使它180,你使用这个等式。 “180- |角度| +180” 你已经知道角度是负的,所以,使用“180 - ( - angle)+180”并添加180的“360 +角度”。 然后,如果它是顺时针, CONTINUE ,但如果是逆时针,则使角度为负,这是因为360度角的另一部分(360 +角度的补充)是“360 - (360) +角度)“或 “360 - 360 - 角度”或“(360 - 360) - 角度”,再次, OR “ - 角度”。所以,我们去...你的完成角度。

答案 2 :(得分:-6)

尝试使用Vector3.Angle(targetDir, forward);

由于你想要它们之间的最短角度,那么如果返回的值超过180度,只需用这个值减去360度。

查看http://answers.unity3d.com/questions/317648/angle-between-two-vectors.html

相关问题