【问题标题】:Unity3d: Angle between two points not consistentUnity3d:两点之间的角度不一致
【发布时间】:2019-07-19 23:26:24
【问题描述】:

所以,我有两个游戏对象P1P2,这两个游戏对象分别与我左腿和右腿上的跟踪器相关联。

所以,我需要找到它们之间的角度。

所以,这就是我的P1P2 如果我面朝前并把我的右腿放在前面的样子。

private double calculateAngle(double P1X, double P1Y, double P2X, double P2Y,
        double P3X, double P3Y)
{

    double numerator = P2Y * (P1X - P3X) + P1Y * (P3X - P2X) + P3Y * (P2X - P1X);
    double denominator = (P2X - P1X) * (P1X - P3X) + (P2Y - P1Y) * (P1Y - P3Y);
    double ratio = numerator / denominator;

    double angleRad = Math.Atan(ratio);
    double angleDeg = (angleRad * 180) / Math.PI;

    if (angleDeg < 0)
    {
        angleDeg = 180 + angleDeg;
    }

    return angleDeg;
}

    calculateAngle(LeftLegController.position.x, LeftLegController.position.z,
              RightLegController.position.x, RightLegController.position.z, 
        LeftLegController.position.x, RightLegController.position.z))

因此,它应该始终是直角三角形。

所以,这是我用来计算P1P2 之间角度的代码,

当我面向前方时,我的两个游戏对象之间的角度不同,而当我面向左侧并将右腿向前移动时(看起来像这样)

我的Angle 1Angle 2 将完全不同。那么,找到角度的更好方法是什么(忽略y轴,就好像点投影在地面上一样)

【问题讨论】:

    标签: unity3d math vector virtual-reality angle


    【解决方案1】:

    我相信您正在寻找的方法是Vector3.SignedAngle()

    var A = Vector3.ProjectOnPlane(P3.position - P1.position, Vector3.up);
    var B = Vector3.ProjectOnPlane(P2.position - P1.position, Vector3.up);
    print(Vector3.SignedAngle(A, B, Vector3.up));
    

    【讨论】:

      【解决方案2】:

      记住两个向量 A 和 B 的点积是 |A| |B| cos theta。所以拿你的点积,除以每个向量的大小,然后用 arccos 得到你的角度。 (事实上​​你可能还是想要cos!)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多