iPhone 三角函数用于处理方向

发布于 2024-10-05 19:38:01 字数 820 浏览 6 评论 0原文

我正在尝试编写一个增强现实应用程序。我使用指南针、陀螺仪和 GPS 坐标来创建叠加层。除了用户离开纵向模式外,一切都运行良好。应该只是一个我无法弄清楚的简单三角问题。

为了确定坐标是否在视野中,我使用在程序开始时计算的 iPhone 中心点的方位角和倾角值。方位角定义为手机正面朝上时的左右移动,倾角定义为手机正面朝上时的上下移动。然后,我在更新时使用陀螺仪和罗盘数据更新该值。因此,如果我正确更新中心点的倾角和方位角,一切都应该正常。

问题是,当 iPhone 旋转时,其方位角和倾角的定义相对于手机保持不变。因此,即使手机是横向的,倾斜度仍然定义为到达手机顶部。

我尝试做的是这样的事情:

self.centerPoint.azimuth = (degToRad(angleX)*cosZ) + (degToRad(angleY)*sinZ);
self.centerPoint.inclination = (degToRad(angleY)*cosZ) + (degToRad(angleX)*sinZ);

其中 degToRad(x) 从角度转换为弧度,cosZ 和 sinZ 分别是陀螺仪的 z 角的 cos 和 sin,angleX 和 angleY 是来自陀螺仪的 x 和 y 角度陀螺仪。陀螺仪的 X 角度测量上下角度,Y 角度测量左右角度,Z 角度测量手机从纵向到横向的旋转。

似乎不起作用。当我将手机旋转到一侧时,倾斜度急剧上升。有没有三角人士可以提供帮助?

我应该补充一点,通常,当仅考虑纵向方向时,我使用:方位角 = degToRad(angleX) 和倾角 = degToRad(angleY)。这适用于纵向。我只需将其乘以某个因子即可考虑不同的角度。

I am trying to write an augmented reality application. I am using the compass, gyroscope, and GPS coordinates to create overlays. Everything is working great, except for when the user leaves portrait mode. Should just be a simple trig problem that I can't figure out.

To determine if a coordinate is in the field of view, I use an azimuth and inclination value of the center point of the iPhone that is calculated at the start of the program. Azimuth is defined as left and right movement when the phone is facing up, and inclination is up and down movement when the phone is facing up. I then update this value with gyroscope and compass data as it updates. So if I update the inclination and azimuth of the center point correctly, everything should work.

The problem is that when the iPhone is rotated, its definition of azimuth and inclination stay the same, with respect to the phone. So inclination is still defined as up to the top of the phone, even though the phone is sideways.

What I tried was doing something like this:

self.centerPoint.azimuth = (degToRad(angleX)*cosZ) + (degToRad(angleY)*sinZ);
self.centerPoint.inclination = (degToRad(angleY)*cosZ) + (degToRad(angleX)*sinZ);

where degToRad(x) converts from degrees to radians, cosZ and sinZ are the cos and sin of the z-angle from the gyroscope respectively, and angleX and angleY are the x and y angles from the gyroscope. The X-angle from the gyroscope measures up-down angle, Y-angle measures left-right, and Z-angle measures rotation of the phone from portrait to landscape.

Doesn't seem to be working. When I rotate the phone to the side, the inclination shoots up dramatically. Any trig people out there who can help?

I should add that normally, when only considering portrait orientation, I used: azimuth = degToRad(angleX) and inclination = degToRad(angleY). This works for portrait orientation. I should just have to multiply this by some factor to account for the different angle.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各空 2024-10-12 19:38:20

自从我解决此类问题以来已经有一段时间了,但如果这只是从纵向移动到横向的问题,你不能根据需要交换方位角和倾角值吗?

毕竟,它们只是以相同方式计算的角度,而真正使它们不同的只是标签。您可能会做得更好,只需将它们标记为 angle1angle2,然后根据手机的方向在其他计算中使用其中之一。


更新:

再次查看此问题,我认为您的问题是您使用了错误的传感器。陀螺仪数据返回弧度/秒,是您不想要的旋转速率的度量。

相反,我认为您需要使用 CMAttitude。 CMAttitude 类提供滚动、俯仰和偏航的数据,这将允许您计算设备的物理方向。方法 multiplyByInverseOfAttitude: 看起来它将提供任意两个样本之间设备方向的变化。

Been a long while since I solved this kind of problem but if its just a question of moving from portrait to landscape, couldn't you just swap azimuth and inclination values as needed?

After all, they are both just angles calculated the same way and it is only the label that really makes them different. You might do better just to label them angle1 and angle2 and then use one or the other in other calculations based on the phone's orientation.


Update:

In looking at this again, I think your problem is that your are using the wrong sensor. The gyroscope data returns radians/sec and is a measure of the rate of rotation which you don't want.

Instead, I think you need to use the attitude measurement provided by CMAttitude. The CMAttitude class provide data for roll, pitch and yaw, which will allow you to calculate the physical orientation of the device. The method multiplyByInverseOfAttitude: looks like it will provide the change in device orientation between any two samples.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文