- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我已经搜索 SO 和 Unity Answers 数周了,但无法解决这个问题。我在 Unity 中有一个 3d 对象,它是根据用户当前的纬度/经度(比如 41.23423,-122.87978)实例化的,然后我想在对象的位置发生变化时将该对象移动到新的纬度/经度。
所以 41.23423, -122,87978 在我的 3D 环境中变为 0,0,0 并且我应用相对于原始位置的更改。
我在 C# 中编码,当位置更改触发 Action 时,我比较两个纬度/经度并计算距离和角度,然后进行极坐标到笛卡尔的转换并将对象移动到新位置。
我的问题是更新正在移动对象,但产生了一些不稳定的结果,我猜这是我的代码中的一个问题。例如,如果我在两个位置之间切换,第一个更改将移至 (131.6, 0.0, 0.0) 但随后将其切换回移动不会移动对象。或者我也曾在测试场合看到,第一次更改会产生意外坐标,但任何后续更改(在两点之间切换)都会为每个位置产生相同的相对值。
我不能很好地处理我在这里做错了什么,所以任何帮助将不胜感激,如果这个问题太模糊或重复的问题(我的代码中的问题或者可能我没有正确处理问题)
这是我的类的代码,它根据位置移动对象。
using UnityEngine;
using System.Collections;
using System;
public class CameraMover : MonoBehaviour {
const double PIx = 3.141592653589793;
const double RADIO = 20925721.8;
public CoreLocationData lastLocationData;
public int firstUpdate = 0;
private Vector3 currentVector;
private Vector3 newVector;
// Use this for initialization
void OnEnable () {
CoreLocationManager.locationServicesDidUpdate += locationServicesDidUpdate;
}
// Update is called once per frame
void Update () {
}
public void locationServicesDidUpdate( CoreLocationData locationData )
{
if (firstUpdate == 0)
lastLocationData = locationData;
double newDistance = DistanceBetweenPlaces(lastLocationData.longitude, lastLocationData.latitude, locationData.longitude, locationData.latitude);
double angleToMove = Angle(lastLocationData.latitude, lastLocationData.longitude, locationData.latitude, locationData.longitude);
double newX = (newDistance * Math.Cos(angleToMove));
double newZ = (newDistance * Math.Sin(angleToMove));
float newXfloat = System.Convert.ToSingle(newX);
float newZfloat = System.Convert.ToSingle(newZ);
currentVector = this.gameObject.transform.position;
newVector = new Vector3(newXfloat, 0.0F, newZfloat);
this.gameObject.transform.position = Vector3.Lerp(currentVector, newVector, 1);
Debug.Log(string.Format("Distance To Move is {0}, angle is {1}", newDistance, angleToMove));
Debug.Log(string.Format("Moving from {0} to {1}", currentVector, newVector));
lastLocationData = locationData;
firstUpdate = 1;
}
public static double Radians(double x)
{
return x * PIx / 180;
}
public static double DistanceBetweenPlaces(
double lon1,
double lat1,
double lon2,
double lat2)
{
double dlon = Radians(lon2 - lon1);
double dlat = Radians(lat2 - lat1);
double a = (Math.Sin(dlat / 2) * Math.Sin(dlat / 2)) + Math.Cos(Radians(lat1)) * Math.Cos(Radians(lat2)) * (Math.Sin(dlon / 2) * Math.Sin(dlon / 2));
double angle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
return angle * RADIO;
}
public double Angle(double px1, double py1, double px2, double py2)
{
// Negate X and Y values
double pxRes = px2 - px1;
double pyRes = py2 - py1;
double angle = 0.0;
// Calculate the angle
if (pxRes == 0.0)
{
if (pxRes == 0.0)
angle = 0.0;
else if (pyRes > 0.0) angle = System.Math.PI / 2.0;
else
angle = System.Math.PI * 3.0 / 2.0;
}
else if (pyRes == 0.0)
{
if (pxRes > 0.0)
angle = 0.0;
else
angle = System.Math.PI;
}
else
{
if (pxRes < 0.0)
angle = System.Math.Atan(pyRes / pxRes) + System.Math.PI;
else if (pyRes < 0.0) angle = System.Math.Atan(pyRes / pxRes) + (2 * System.Math.PI);
else
angle = System.Math.Atan(pyRes / pxRes);
}
// Convert to degrees
angle = angle * 180 / System.Math.PI;return angle;
}
}
编辑
正确实现:
using UnityEngine;
using System.Collections;
using System;
public class CameraMover : MonoBehaviour
{
const double RATIO = 20902231.0029;
public CoreLocationData lastLocationData;
public int firstUpdate = 0;
private Vector3 currentVector;
private Vector3 newVector;
// Use this for initialization
void OnEnable ()
{
CoreLocationManager.locationServicesDidUpdate += locationServicesDidUpdate;
}
// Update is called once per frame
void Update () { }
public void locationServicesDidUpdate( CoreLocationData locationData )
{
if (firstUpdate == 0)
{
lastLocationData = locationData;
}
double newDistance = DistanceBetweenPlaces(lastLocationData.longitude, lastLocationData.latitude, locationData.longitude, locationData.latitude);
double angleToMove = AngleBetweenPlaces(lastLocationData.latitude, lastLocationData.longitude, locationData.latitude, locationData.longitude);
double angleToMoveRadians = Deg2Rad(angleToMove);
double newX = (newDistance * Math.Sin(angleToMoveRadians));
double newZ = (newDistance * Math.Cos(angleToMoveRadians));
float newXfloat = System.Convert.ToSingle(newX) * -1;
float newZfloat = System.Convert.ToSingle(newZ) * -1;
Debug.Log(string.Format("new x coordinate should be {0} and z should be {1}", newXfloat, newZfloat));
currentVector = this.gameObject.transform.position;
this.gameObject.transform.position = new Vector3((currentVector.x + newXfloat),0, (currentVector.z + newZfloat));
lastLocationData = locationData;
firstUpdate = 1;
}
public static double DistanceBetweenPlaces(
double lon1,
double lat1,
double lon2,
double lat2)
{
double phi1 = Deg2Rad(lat1);
double phi2 = Deg2Rad(lat2);
double deltaLamdba = Deg2Rad(lon2 - lon1);
double d = Math.Acos(
(Math.Sin(phi1) * Math.Sin(phi2)) +
(Math.Cos(phi1) * Math.Cos(phi2) * Math.Cos(deltaLamdba)));
return d * RATIO;
}
public static double Deg2Rad(double degrees)
{
return degrees * (Math.PI / 180.0);
}
public static double Rad2Deg(double radians)
{
return radians * (180.0 / Math.PI);
}
public double AngleBetweenPlaces(double lat1, double lon1, double lat2, double lon2)
{
var newLat1 = Deg2Rad(lat1);
var newLat2 = Deg2Rad(lat2);
var dLon = Deg2Rad(lon2) - Deg2Rad(lon1);
var y = Math.Sin(dLon) * Math.Cos(newLat2);
var x = Math.Cos(newLat1) * Math.Sin(newLat2) - Math.Sin(newLat1) * Math.Cos(newLat2) * Math.Cos(dLon);
var brng = Math.Atan2(y, x);
return (Rad2Deg(brng) + 360) % 360;
}
}
最佳答案
有时您使用 System.Math.PI
,有时您使用 PIx = 3.141592653589793
- 这是什么原因?另外,RADIO
是什么,根据它的使用,我认为它是地球的平均半径(以公里为单位),但该值已经过时了。
DistanceBetweenPlaces
方法也有一些错误,无论如何,请尝试以下操作,如果您对准确性有疑问,那么我会使用 Vincenty's formulae ,否则坚持使用简单的余弦球面定律。
// Earths Mean Radius in Kilometres?
public const double RADIO = 6371;
public static double DistanceBetweenPlaces(
double lon1,
double lat1,
double lon2,
double lat2)
{
double phi1 = Deg2Rad(lat1);
double phi2 = Deg2Rad(lat2);
double deltaLamdba = ConvertDegreesToRadians(lon2 - lon1);
double d = Math.Acos((Math.Sin(phi1) * Math.Sin(phi2)) + (Math.Cos(phi1) * Math.Cos(phi2) * Math.Cos(deltaLamdba)));
return d * RADIO;
}
public static double Deg2Rad(double degrees)
{
return degrees == 0 ? degrees : (degrees * Math.PI / 180.0);
}
另外,查看这个用于处理测地线数据的重要资源
关于ios - 位置变化时极地到笛卡尔的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9271042/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!