Compare device 3D orientation with the sun position

假如想象 提交于 2019-12-04 10:24:43

I finally managed to solve the problem,

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

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.

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!