繁体   English   中英

iOS ARKit - 计算设备在所有三个方向上的旋转程度

[英]iOS ARKit - Calculate the degree of rotation of device in all three direction

我想得到所有三轴的旋转度。 我试图用横向模式实现。 当从左向右旋转设备(Y旋转)时,我想要在用户设置左端和旋转到右边时覆盖度数。 所以当达到起点时我们需要360度。 因此,对于y旋转,使用以下计算,它很好。

let q = sceneView.pointOfView.orientation
let yRotation = Float.pi - atan2f( (2*q.y*q.w)-(2*q.x*q.z), 1-(2*pow(q.y,2))-(2*pow(q.z,2)) )

参考: 在ARKit中围绕世界原点的y轴旋转设备

同样,想要Z旋转,对于Z旋转,不需要360度旋转。 但旋转约90度时需要准确的值如果我们使用的话,这是好的

var zRotation = sceneView.pointOfView.eulerAngles.z

但是这将是错误的*如果我们在某种程度上从左到右(Y方向)旋转设备然后检查Z旋转,我们将使Z旋转值接近180度而不是接近0

所以尝试了一种不好的方法来获得Z旋转:

var zRotation = sceneView.pointOfView.eulerAngles.z
if abs(sceneView.pointOfVieweulerAngles.z.toDegree) > 90 {
    zRotation = (abs(cameraNode.eulerAngles.z.toDegree) - 180).toRadian
}

但这不准确,我们可以在减去180时看到Z旋转值。

你能建议一个合适的方法吗?

X旋转值与Z旋转相似,在从左向右(Y旋转)旋转一定程度后检查时值接近180。

谢谢

编辑:2109年9月3日对于x和y,现在我正在使用

let xRotation = sceneView!.session.currentFrame!.camera.eulerAngles.x
let zRotation = sceneView!.session.currentFrame!.camera.eulerAngles.z

如果禁用横向(左和右),您将获得非常好的x,y和z旋转数据。

if let q = self?.sceneView.pointOfView?.orientation {

            let heading = atan2f( (2 * q.y * q.w) - (2 * q.x * q.z), 1 - (2 * pow(q.y,2)) - (2 * pow(q.z,2)) )
            let attitude = asinf( (2 * q.x * q.y) + (2 * q.z * q.w) )
            let bank = atan2f( (2 * q.x * q.w) - (2 * q.y * q.z), 1 - (2 * pow(q.x,2)) - (2 * pow(q.z,2)) )

            print("X: \(attitude * (180.0 / .pi)) Y: \(heading * (180.0 / .pi)) Z: \(bank * (180.0 / .pi))")

    }

如果您允许横向方向,那么您将获得不同的值。 一旦设备从纵向移动到横向,您将获得旋转数据,就像整个坐标空间已经旋转pi / 2弧度一样。 然后你必须调整数据(Add或Substract pi / 2)以获得正确的值。 有没有办法在更改为横向方向后转换坐标系?

因此,如果您不希望设备窗口按方向旋转,请剖析横向模式。 虽然您可以获取所有方向信息来执行任何UI更改:

if UIDevice.current.orientation == UIDeviceOrientation.faceUp {
                print("faceUp")
            } else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {
                print("landscapeRight")
            } else if UIDevice.current.orientation == UIDeviceOrientation.portrait {
                print("portrait")
            } else if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown {
                print("portraitUpsideDown")
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM