首页 > 解决方案 > SCNNode 方向而不是 eulerAngles

问题描述

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

我已经阅读了很多答案,尤其是来自 ARGeo 的答案:SCNNode Z 旋转轴保持不变,而 X 和 Y 轴在节点旋转时发生变化,并且我了解eulerAngles云台锁定的问题。

但我不明白如何正确使用orientationant 那个w组件。这里有成功使用UIRotationGestureRecognizerandorientation而不是的人eulerAngles吗?

标签: iosswiftscenekitaugmented-realityarkit

解决方案


这是如何实现的示例之一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())
    }
}

希望这可以帮助。


推荐阅读