将设备3D方向与太阳位置进行比较 [英] Compare device 3D orientation with the sun position

查看:176
本文介绍了将设备3D方向与太阳位置进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一款应用程序,要求用户将iPhone瞄准太阳以触发特殊事件。

I am working on an app that requires the user to aim his iPhone to the sun in order to trigger a special event.

我可以检索设备3D方向基于陀螺仪和CoreMotion框架的四元数,从中我可以获得偏航,滚转和俯仰角度。
我还可以根据当前日期和时间(GMT)以及纬度和经度计算太阳的方位角和天顶角。
我接下来要弄清楚的是如何比较这两组值(手机方向和太阳位置)以准确检测设备与太阳的对齐。

I can retrieve the device 3D orientation quaternion based on the gyroscope and CoreMotion framework, from this I can get the yaw, roll and pitch angles. I can also compute the sun's azimuth and zenith angle based on the current date and time (GMT) and the latitude and longitude. What I am trying to figure out next is how to compare these two sets of values (phone orientation and sun position) to accurately detect the alignment of the device with the sun.

关于如何实现这一点的任何想法?

Any ideas on how to achieve that?

推荐答案

我终于设法解决了这个问题,

I finally managed to solve the problem,

为实现这一目标,我使用了Apple开发人员资源中使用的增强现实示例代码:
pARK

To achieve this I used a sample code using augmented reality available on Apple developer ressources: pARk

这个想法是首先将太阳的球面坐标转换为笛卡尔坐标,以便将其在天空中的位置作为一个简单的向量:{x,y,z}。
公式可在维基百科上找到:球面坐标。由于与太阳的距离(球面坐标的半径)在这里并不重要,我使用了1。

The idea was first to convert the sun's spherical coordinates to cartesian coordinates in order to get its position in the sky as a simple vector: {x, y, z}. The formula is available on Wikipedia : Spherical coordinates. As the distance from the sun (radius in spherical coordinates) doesn't really matter here, I used 1.

使用设备陀螺仪和CoreMotion框架我能够得到iPhone旋转矩阵。
从'pARk'代码示例中,使用几何函数,我可以计算出相机投影矩阵。我将旋转矩阵与它相乘,然后将得到的矩阵与太阳的矢量坐标相乘。

Using the device gyroscope and CoreMotion framework I was then able to get the iPhone rotation matrix. From the 'pARk' code sample and using the geometry functions I could then compute the camera projection matrix. I multiply the rotation matrix with it and then multiply the resulting matrix with the sun's vector coordinates.

这给出了一个带有太阳屏幕坐标的简单矢量。通过显示带有这些x和y值的UIView,我可以在移动屏幕时看到太阳移动。

And that gives a simple vector with screen coordinates for the sun. By displaying a UIView with these x and y values I can finally see the sun moving around as I move the screen.

此功能的代码可在我的 Github ,随时可以使用和分享!

The code for this feature is available on my Github, feel free to use and share!

这篇关于将设备3D方向与太阳位置进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆