ios - 从 CMRotationMatrix 获取俯仰、偏航、滚动

标签 ios opengl-es core-motion

我有一个 CMRotationMatrix *rot,我想从矩阵中获取俯仰、偏航和滚动。 我有什么想法可以做到这一点吗?

谢谢

最佳答案

使用四元数比使用欧拉角更好....滚转、俯仰和偏航值可以使用以下公式从四元数导出:

roll  = atan2(2*y*w - 2*x*z, 1 - 2*y*y - 2*z*z)
pitch = atan2(2*x*w - 2*y*z, 1 - 2*x*x - 2*z*z)
yaw   =  asin(2*x*y + 2*z*w)

它可以实现为:

CMQuaternion quat = self.motionManager.deviceMotion.attitude.quaternion;
myRoll = radiansToDegrees(atan2(2*(quat.y*quat.w - quat.x*quat.z), 1 - 2*quat.y*quat.y - 2*quat.z*quat.z)) ;
myPitch = radiansToDegrees(atan2(2*(quat.x*quat.w + quat.y*quat.z), 1 - 2*quat.x*quat.x - 2*quat.z*quat.z));
myYaw = radiansToDegrees(asin(2*quat.x*quat.y + 2*quat.w*quat.z));

其中 radianstoDegrees 是一个预处理器指令,实现为:

#define radiansToDegrees(x) (180/M_PI)*x

这样做是为了将公式给出的弧度值转换为度数。

有关转换的更多信息可在此处找到: tinkerforge在这里:Conversion between Quaternions and Euler angles .

关于ios - 从 CMRotationMatrix 获取俯仰、偏航、滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9478630/

相关文章:

ios - 无法在以编程方式创建的自定义 View 中更改 UILabel 文本值

ios - Xcode 11 Beta 5 未显示 Swift 5.1

ios - PAN 与 Linux、iOS、蓝牙、Bonjour、GameKit——可能吗?

android - 如何在 opengl Android 中进行对象检测?

ios - 如何使用 Core Motion 通过 SwiftUI 输出磁力计数据?

iphone - 如何使这个 CoreMotion 代码在视网膜 iPod touch 设备上工作?

ios - NSTimeInterval 作为倒数计时器,将其作为单独的可重用类应用

opengl-es - 如何读取 WebGL 中的深度缓冲区?

iOS OpenGL 问题

Swift - 在 Firebase 数据库中存储计步器的日期