我试图用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
和。这里有人成功使用UIRotationGestureRecognizer
和orientation
而不是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())
}
}
希望这可以帮助。