4

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

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?

4

1 回答 1

6

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

为了实现这一点,我使用了一个示例代码,该示例代码使用了 Apple 开发人员资源中提供的增强现实: pARk

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

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

这给出了一个简单的矢量,其中包含太阳的屏幕坐标。通过显示具有这些 x 和 y 值的 UIView,我终于可以在移动屏幕时看到太阳在四处移动。

此功能的代码在我的Github上,请随意使用和分享!

于 2014-02-04T10:33:18.243 回答