SCNNode方向而不是eulerAngles

时间:2019-04-19 12:49:40

标签: ios swift scenekit augmented-reality arkit

我正在尝试将SCNNodeUIRotationGestureRecognizer一起旋转,并更改eulerAngles。我在绕y和绕z轴旋转之间切换。当我只旋转其中一个时,一切都很好。但是当我两个都旋转时出现问题。它不再绕轴自身旋转,而是随机旋转。

我从ARGeo那里读了很多答案,例如:SCNNode Z-rotation axis stays constant, while X and Y axes change when node is rotated,而且我了解eulerAngles和万向节锁定的问题。

但是我不知道如何正确使用orientation组件。这里是成功使用wUIRotationGestureRecognizer而非orientation的人吗?

1 个答案:

答案 0 :(得分:1)

这是如何实现UIRotationGestureRecognizer的示例之一。

我从How can I rotate an SCNNode....帖子中复制了它。

private var startingOrientation = GLKQuaternion.identity
private var rotationAxis = GLKVector3Make(0, 0, 0)

@objc private func handleRotation(_ rotation: UIRotationGestureRecognizer) {
    guard let node = sceneView.hitTest(rotation.location(in: sceneView), options: nil).first?.node else {
        return
    }
    if rotation.state == .began {
        startingOrientation = GLKQuaternion(boxNode.orientation)
        let cameraLookingDirection = sceneView.pointOfView!.parentFront
        let cameraLookingDirectionInTargetNodesReference = boxNode.convertVector(cameraLookingDirection,                                                                        
                                                                                 from: sceneView.pointOfView!.parent!)
        rotationAxis = GLKVector3(cameraLookingDirectionInTargetNodesReference)
    } else if rotation.state == .ended {
        startingOrientation = GLKQuaternionIdentity
        rotationAxis = GLKVector3Make(0, 0, 0)
    } else if rotation.state == .changed {
        let quaternion = GLKQuaternion(angle: Float(rotation.rotation), axis: rotationAxis)
        node.orientation = SCNQuaternion((startingOrientation * quaternion).normalized())
    }
}

希望这会有所帮助。