在android中计算给定方位角、俯仰角和滚动角的相对方向?

发布于 2024-10-05 17:19:58 字数 326 浏览 2 评论 0原文

当我在 Android 应用程序中监听方向事件时,我得到一个 SensorEvent,其中包含 3 个浮点 - 相对于现实世界轴的方位角、俯仰角和滚动角。

现在假设我正在构建一个像迷宫这样的应用程序,但我不想强迫用户通过电话并握住电话以使 xy 平面与地面平行。相反,我希望能够允许用户随心所欲地握住手机,躺下或者坐下来以一定角度握住手机。换句话说,我需要根据用户的喜好来校准手机。

我怎样才能做到这一点?

另请注意,我相信我的答案与 getRotationMatrix 和 getOrientation 有关,但我不确定如何!

请帮忙!我已经被困在这个问题上好几个小时了。

When I listen to orientation event in an android app, I get a SensorEvent, which contains 3 floats - azimuth, pitch, and roll in relation to the real-world's axis.

Now say I am building an app like labyrinth, but I don't want to force the user the be over the phone and hold the phone such that the xy plane is parallel to the ground. Instead I want to be able to allow the user to hold the phone as they wish, laying down or, perhaps, sitting down and holding the phone at an angle. In other words, I need to calibrate the phone in accordance with the user's preference.

How can I do that?

Also note that I believe that my answer has to do with getRotationMatrix and getOrientation, but I am not sure how!

Please help! I've been stuck at this for hours.

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

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

发布评论

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

评论(2

潦草背影 2024-10-12 17:19:58

对于迷宫式应用程序,您可能更关心加速度(重力)矢量而不是轴方向。在电话坐标系中,该矢量由三个加速度计测量值的组合给出,而不是旋转角度。具体来说,只有 xy 读数会影响球的运动。

如果您确实需要方向,则 3 个角度读数代表 3 个欧拉角。但是,我怀疑您可能并不真正需要角度本身,而是需要旋转矩阵 R,它由 getRotationMatrix() API 返回。一旦你有了这个矩阵,那么它基本上就是你正在寻找的校准。当您想要将世界坐标中的向量转换为设备坐标时,您应该将其乘以该矩阵的(在这种特殊情况下,inv(R) = transpose(R).

因此,按照我在文档中找到的示例,如果您想将世界重力矢量 g ([0 0 g]) 转换为设备坐标,乘以 inv(R):

g = inv(R) * g

(注意这应该为您提供与读取加速度计相同的结果)

此处可能使用的 API:矩阵类的 invertM()multiplyMV() 方法。

For a Labyrinth style app, you probably care more for the acceleration (gravity) vector than the axes orientation. This vector, in Phone coordinate system, is given by the combination of the three accelerometers measurements, rather than the rotation angles. Specifically, only the x and y readings should affect the ball's motion.

If you do actually need the orientation, then the 3 angular readings represent the 3 Euler angles. However, I suspect you probably don't really need the angles themselves, but rather the rotation matrix R, which is returned by the getRotationMatrix() API. Once you have this matrix, then it is basically the calibration that you are looking for. When you want to transform a vector in world coordinates to your device coordinates, you should multiply it by the inverse of this matrix (where in this special case, inv(R) = transpose(R).

So, following the example I found in the documentation, if you want to transform the world gravity vector g ([0 0 g]) to the device coordinates, multiply it by inv(R):

g = inv(R) * g

(note that this should give you the same result as reading the accelerometers)

Possible APIs to use here: invertM() and multiplyMV() methods of the matrix class.

心碎的声音 2024-10-12 17:19:58

我不知道有什么特定于 android 的 API,但您想要做的就是将方位角减小一定量,对吧?所以你将“原点”从 (0,0,0) 移动到他们想要的任何位置。在伪代码中:

myGetRotationMatrix:
    return getRotationMatrix() - origin

I don't know of any android-specific APIs, but all you want to do is decrease the azimuth by a certain amount, right? So you move the "origin" from (0,0,0) to whatever they want. In pseudocode:

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