简体   繁体   English

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

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

I want to get the degree of Rotation of all three axis. 我想得到所有三轴的旋转度。 I am trying to achieve with landscape mode. 我试图用横向模式实现。 When rotate device from left to right (Y-rotation), I want the degree covered once user set a left end and rotatae to right. 当从左向右旋转设备(Y旋转)时,我想要在用户设置左端和旋转到右边时覆盖度数。 So we need the degree 360 when it reached the starting point. 所以当达到起点时我们需要360度。 So for the y-rotation, using the following calculation and it is fine. 因此,对于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)) )

Ref: Getting the rotation of device around the world origin's y axis in ARKit 参考: 在ARKit中围绕世界原点的y轴旋转设备

Similarly, want the Z-rotation, for Z-rotation , no need for 360 degree rotation. 同样,想要Z旋转,对于Z旋转,不需要360度旋转。 But need accurate value when rotate about upto 90 degree This is fine if we use the 但旋转约90度时需要准确的值如果我们使用的话,这是好的

var zRotation = sceneView.pointOfView.eulerAngles.z

But this will be wrong* if we rotate the device on left to right (Y-direction ) some degree and then check the Z rotation, we will get the Z-rotation values as near to 180 degree rather than near to 0 但是这将是错误的*如果我们在某种程度上从左到右(Y方向)旋转设备然后检查Z旋转,我们将使Z旋转值接近180度而不是接近0

So tried a bad way to get the Z rotation like: 所以尝试了一种不好的方法来获得Z旋转:

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

But this is not accurate, we can see a jumb the Z-rotation value when subtracting 180. 但这不准确,我们可以在减去180时看到Z旋转值。

Could you suggest a proper method.. 你能建议一个合适的方法吗?

X-rotation value is simialr to Z-rotation, getting values near to 180 when check after rotate left to right (Y-rotation) some degree. X旋转值与Z旋转相似,在从左向右(Y旋转)旋转一定程度后检查时值接近180。

Thanks 谢谢

Edited: Sep 3, 2109 For x and y, Now I am using 编辑:2109年9月3日对于x和y,现在我正在使用

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

If you disable the landscape orientation (left & right), you will get pretty good data for x, y and z rotation. 如果禁用横向(左和右),您将获得非常好的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))")

    }

If you allow Landscape Orientation then you will get different values. 如果您允许横向方向,那么您将获得不同的值。 Once the device goes from Portrait to Landscape, you will get rotation data as if the whole coordinate space has been rotated by pi/2 radians. 一旦设备从纵向移动到横向,您将获得旋转数据,就像整个坐标空间已经旋转pi / 2弧度一样。 Then you have to tweak the data (Add or Substract pi/2) to get correct values. 然后你必须调整数据(Add或Substract pi / 2)以获得正确的值。 Is there a way to convert the coordinate system after a change to Landscape Orientation? 有没有办法在更改为横向方向后转换坐标系?

So if you don't want your device window to rotate with orientation, disselect the Landscape mode. 因此,如果您不希望设备窗口按方向旋转,请剖析横向模式。 Though you can get all orientation info to perform any UI changes: 虽然您可以获取所有方向信息来执行任何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