iPhone方向-如何确定向上方向? [英] iPhone orientation -- how do I figure out which way is up?

查看:154
本文介绍了iPhone方向-如何确定向上方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在屏幕上始终朝上的方向绘制2D图像。如果用户正在旋转手机,则我想确保2D对象不会随设备一起旋转。它应始终垂直站立。我想补偿用户向左或向右倾斜,而不是向自己或自己倾斜的情况。

I'm trying to draw a 2D image on the screen that is always facing "up". If the user is rotating their phone, I want to ensure my 2D object does not rotate with the device; it should always "stand vertical". I want to compensate for the user tilting left or right, but not tilting away or towards themselves.

我正在使用CoreMotion从设备获取俯仰,横滚和偏航,但我不明白如何将这些点向上转换为方向,尤其是在用户旋转设备时。理想情况下,我可以将这三个数字转换为一个值,该值始终告诉我向上的方向,而不必重新学习所有三角函数。

I'm using CoreMotion to get Pitch, Roll and Yaw from the device, but I don't understand how to translate the points into the direction up, especially as the user rotates the device. Ideally, I can translate these 3 numbers into a a single value that will always tell me which way is up, without having to relearn all of Trigonometry.

3D茶壶示例,但这无济于事,因为此示例是2D,并且我不需要朝着倾斜/倾斜。此外,我也不想使用指南针/磁力计,因为它需要在iPod Touch上运行。

I've looked at the 3D teapot example but it doesn't help because this example is 2D and I don't need tilt away/tilt towards. Also, I don't want to use the compass/magnetometer because this needs to run on the iPod Touch.

推荐答案

查看图片以更好地理解我的意思:

Look at image to better understand what I'm saying:

所以您只对XY平面感兴趣。加速度计始终会测量设备相对于自由落体的加速度。因此,当您将设备固定在图像上时,加速度值为(0,-1,0)。将设备顺时针倾斜45度时,该值为(0.707,-0.707,0)。您可以通过计算当前加速度值和某个参考轴的点积来获得角度。如果我们使用向上向量,则轴为(0,1,0)。因此点积

So you are only interested in XY plane. Accelerometer always measures the acceleration of the device relative to freefall. So when you're holding device as it is on image the acceleration value is (0, -1, 0). When you tilt device 45 degrees clockwise the value is (0.707, -0.707, 0). You can get the angle by calculating the dot product of current acceleration value and some reference axis. If we are using an up vector the axis is (0,1,0). So the dot product is

0*0.707 - 1*0.707 + 0*0 = -0.707

哪个正好是acos(-0.707)= 45度。因此,如果您希望图像静止不动,则需要反向旋转图像,即在XY平面中旋转-45度。如果要忽略Z值,则仅采用X和Y轴:(X_ACCEL,Y_ACCEL,0)。您需要将该向量重新归一化(它的大小必须为1)。然后按照我的解释计算角度。

Which is exactly acos(-0.707) = 45 degrees. So if you want your image to stay still you need to rotate it in reverse side, i.e. -45 degrees in XY plane. If you want to ignore the Z value then you take only X and Y axes: (X_ACCEL, Y_ACCEL, 0). You need to renormalize that vector (it must gave magnitude of 1). Then calculate an angle as I explained.

这篇关于iPhone方向-如何确定向上方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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