【问题标题】:Find Angle Between Two Vectors求两个向量之间的角度
【发布时间】:2014-08-29 04:32:51
【问题描述】:

我已经阅读了一些关于两个向量之间角度的重复答案,但我仍然被我的问题所困扰。我有两个向量,我希望它们之间的角度始终为 90 度。为了实现这一点,我需要找到它们之间的角度,这样我就可以减去或添加正确的度数,使它们之间的角度始终为 90 度。

图片说明了一个精灵和两个向量。我如何找到它们两个之间的角度A?我曾尝试使用此代码来获取两个向量之间的角度,但我一定错过了一些东西,因为我没有得到正确的结果:

        public float GetAngleBetween (Vector2 A, Vector2 B)
    {
        float DotProd = Vector2.Dot (A, B);
        float Length = A.Length () * B.Length ();
        return (float)MathHelper.ToDegrees ((float)Math.Acos (DotProd/Length));
    }

欢迎任何意见,并提前感谢您的任何回答。

【问题讨论】:

标签: c# xna angle


【解决方案1】:

以弧度表示的实际角度为

Math.ACos(Vector2.Dot(a, b));

确保 a 和 b 是归一化向量,否则结果会变得很奇怪。

【讨论】:

    【解决方案2】:

    我想你可能正在寻找Vector2.Dot 方法,它用于计算两个向量的乘积,并且可以用于角度计算。

    例如:

    // the angle between the two vectors is less than 90 degrees. 
    Vector2.Dot(vector1.Normalize(), vector2.Normalize()) > 0 
    
    // the angle between the two vectors is more than 90 degrees. 
    Vector2.Dot(vector1.Normalize(), vector2.Normalize()) < 0 
    
    // the angle between the two vectors is 90 degrees; that is, the vectors are orthogonal. 
    Vector2.Dot(vector1.Normalize(), vector2.Normalize()) == 0 
    
    // the angle between the two vectors is 0 degrees; that is, the vectors point in the same direction and are parallel. 
    Vector2.Dot(vector1.Normalize(), vector2.Normalize()) == 1 
    
    // the angle between the two vectors is 180 degrees; that is, the vectors point in opposite directions and are parallel. 
    Vector2.Dot(vector1.Normalize(), vector2.Normalize()) == -1 
    

    这是您要找的,还是您需要确切的角度?

    【讨论】:

    • 感谢您的回答,但我确实需要准确的角度。
    • Vector2.Dot(vector1.Normalize(), vector2.Normalize()) 应该给出角度的 cos...
    • 我读到 XNA 计算角度的参考点是 (0,0)。所以如果我的精灵在屏幕的左上角,向量 1 和向量 2 之间的角度是 90 度。但是,如果我根据参考点围绕角度变化移动精灵。是否可以更改此参考点?因为这两个点和我的精灵中心之间的角度仍然是90度,即使根据参考点计算出的角度也是另外一回事。
    • 在寻找上面的答案时,我看到不同的论坛有同样的问题。当要求更改参考点时,没有给他们任何解决方案。
    【解决方案3】:

    如果我了解您的问题图表和 cmets,Dot 产品和 Acos 并不是您需要的唯一信息。您还需要考虑精灵何时不在 (0,0) 处。

    float angleInRadians = (float) Math.Acos(Vector2.Dot(Vector2.Normalize(vector1 - spritePosition), Vector2.Normalize(vector2 - spritePosition)));
    
    int angleInDegrees = MathHelper.ToDegrees(angleInRadians);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-17
      相关资源
      最近更新 更多