从 CoreMotion 的姿态旋转矩阵中隔离并移除水平旋转
我正在制作类似增强现实应用程序的东西,其中有一个 OpenGL 场景,无论 iOS 设备如何移动,我都希望它与重力保持一致。我以为我已经用 CMDeviceMotion.attitude.pitch 设置得很好,直到我发现将 iPhone 侧向倾斜会扰乱该数字。因此,我从 pARk* 示例中获取了一些代码,现在我正在尝试隔离围绕垂直访问的旋转。我正在绘制的场景并不关心用户面向哪个方向,人物将始终与观看者保持一定的距离。我认为,当我计算出垂直轴旋转分量时,我可以将其反转并将其与旋转矩阵相乘,以便在用户更改标题时保持 OpenGL 图形静止。
这是我的代码:
CMDeviceMotion *d = motionManager.deviceMotion;
if (d != nil) {
CMRotationMatrix r = d.attitude.rotationMatrix;
transformFromCMRotationMatrix(cameraTransform, &r);
mat4f_t projectionCameraTransform;
multiplyMatrixAndMatrix(projectionCameraTransform, projectionTransform, cameraTransform);
GLKMatrix4 rotMatrix = GLKMatrix4Make(projectionCameraTransform[0],
projectionCameraTransform[1],
projectionCameraTransform[2],
projectionCameraTransform[3],
projectionCameraTransform[4],
projectionCameraTransform[5],
projectionCameraTransform[6],
projectionCameraTransform[7],
projectionCameraTransform[8],
projectionCameraTransform[9],
projectionCameraTransform[10],
projectionCameraTransform[11],
projectionCameraTransform[12],
projectionCameraTransform[13],
projectionCameraTransform[14],
projectionCameraTransform[15]);
}
然后我像往常一样在 OpenGL 中使用 rotMatrix。
想法、建议?提前致谢。
*公园示例代码在真实空间中设置了几个点,计算出用户的位置和这些点的相对方向,并将它们绘制在屏幕上,以便它们看起来漂浮在指向其位置的地平线上。
I'm making something like an augmented reality app, in which I have an OpenGL scene that I want to stay aligned with gravity no matter how the iOS device moves. I thought I had it set up fine with a CMDeviceMotion.attitude.pitch, until I discovered that leaning an iPhone sidewards messes with that number. So I took some code from the pARk* example and I'm now trying to isolate out the rotation around the vertical access. The scene I'm drawing doesn't care which heading the user is facing, the figures will always be drawn a set distance from the viewer. I think that when I figure out the vertical axis rotation component I can reverse it and multiply it out of the rotation matrix so keep the OpenGL figures still when the user changes heading.
Here's my code:
CMDeviceMotion *d = motionManager.deviceMotion;
if (d != nil) {
CMRotationMatrix r = d.attitude.rotationMatrix;
transformFromCMRotationMatrix(cameraTransform, &r);
mat4f_t projectionCameraTransform;
multiplyMatrixAndMatrix(projectionCameraTransform, projectionTransform, cameraTransform);
GLKMatrix4 rotMatrix = GLKMatrix4Make(projectionCameraTransform[0],
projectionCameraTransform[1],
projectionCameraTransform[2],
projectionCameraTransform[3],
projectionCameraTransform[4],
projectionCameraTransform[5],
projectionCameraTransform[6],
projectionCameraTransform[7],
projectionCameraTransform[8],
projectionCameraTransform[9],
projectionCameraTransform[10],
projectionCameraTransform[11],
projectionCameraTransform[12],
projectionCameraTransform[13],
projectionCameraTransform[14],
projectionCameraTransform[15]);
}
I then use the rotMatrix as usual in OpenGL.
Thoughts, suggestions? Thanks in advance.
*The pARk sample code sets up a few points in real space, works out the user's location and the relative direction of those points and draws them onscreen so the appear to float at the horizon pointing towards their location.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只是根据设备屏幕方向绕 Z 轴旋转方向。这不是最漂亮的,但它似乎完全符合我的需要,无需去欧拉和返回(因此,避免了健身房锁问题)
I just rotate the orientation around the Z axis depending on device screen orientation. This isn't the prettiest but it seems to do exactly what I need, without going to euler and back (and thus, avoiding the gymbal lock problems)
这里有一些代码来阐明如何使用 Attitude.rotationMatrix
Here's some code to clarify how to use the attitude.rotationMatrix
我从这个答案中得到了一些提示并提出了一个解决方案:
https://stackoverflow.com/questions/5328848/simulated-an-image-floating-effect-using-coremotion-devicemotion-on-the-iphone/5442962#5442962
然而,当手机接近垂直时,这种情况就会变得混乱。所以我还在寻找。
I've taken a some hints from this answer and come up with a solution:
https://stackoverflow.com/questions/5328848/simulating-an-image-floating-effect-using-coremotion-devicemotion-on-the-iphone/5442962#5442962
However this haywire when the phone is near vertical. So I'm still looking.