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

我从ARGeo那里读到了很多答案,例如SCNNode Z-rotation axis stays constant, while X and Y axes change when node is rotated,我知道eulerAngles和云台锁定的问题。

但是我不明白如何正确使用orientation组件的w和。这里有人成功使用UIRotationGestureRecognizerorientation而不是eulerAngles吗?

最佳答案

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

我从How can I rotate an SCNNode.... SO post复制了它。

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())
    }
}

希望这可以帮助。

10-08 05:24