【问题标题】:Making a Compass point to a particular location in Unity使指南针指向 Unity 中的特定位置
【发布时间】:2016-03-12 22:09:10
【问题描述】:

我正在尝试制作一个指向 Unity 程序中自定义位置的指南针。但是它完全关闭了,它通常指向错误的方向,我似乎无法弄清楚我的逻辑有什么问题。

我正在使用此处的弯曲几何轴承方程:http://www.yourhomenow.com/house/haversine.html

  1. 获取当前位置。
  2. 计算从当前位置到目标位置的方位。
  3. 相对于真北旋转屏幕指南针。

    // Calculate bearing between current location and target location
    // Get current GPS location
    float lat1 = Input.location.lastData.latitude;
    float lon1 = Input.location.lastData.longitude;
    float lat2 = TargetLatitude;
    float dLon = TargetLongitude - lon1;
    
    // Calculate bearing
    var y = Mathf.Sin(dLon) * Mathf.Cos(lat2);
    var x = Mathf.Cos(lat1) * Mathf.Sin(lat2) -
            Mathf.Sin(lat1) * Mathf.Cos(lat2) * Mathf.Cos(dLon);
    var brng = ((Mathf.Atan2(y, x)*(180.0f/Mathf.PI)) + 360.0f) % 360.0f;
    
    Dump.text = brng.ToString();
    
    // Rotate the to target location relative to north. Z axis pointing out of screen.
    transform.eulerAngles = new Vector3(0, 0, Mathf.MoveTowardsAngle(transform.localEulerAngles.z, Input.compass.trueHeading + brng, COMPASS_MAXDELTA));
    

如果我删除该代码中的方位偏移,它会指向足够北的数字罗盘。

【问题讨论】:

    标签: android unity3d digital-compass


    【解决方案1】:

    确保所有角度都是弧度而不是度数。

    Mathf.Sin(dLon * Mathf.Deg2Rad) 等情况下使用Mathf.Deg2Rad 应该会产生正确的结果。 Haversine 公式仅适用于弧度角。

    【讨论】:

    • 这确实是问题所在。而现在我感觉很糟糕。一定是喝咖啡的时间了。
    猜你喜欢
    • 2011-07-02
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2014-04-25
    • 2013-10-05
    相关资源
    最近更新 更多