我正在尝试将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
的人吗?
答案 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())
}
}
希望这会有所帮助。