【问题标题】:SCNNode orientation instead of eulerAnglesSCNNode 方向而不是 eulerAngles
【发布时间】:2019-04-19 12:49:40
【问题描述】:

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

我已经阅读了很多答案,尤其是来自 ARGeo 的这个答案:SCNNode Z-rotation axis stays constant, while X and Y axes change when node is rotated,我了解eulerAngles 和云台锁的问题。

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

【问题讨论】:

  • 在 SO 上找到它。

标签: ios swift scenekit augmented-reality arkit


【解决方案1】:

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

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

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

希望这会有所帮助。

【讨论】:

  • 感谢您的快速回答,因为我使用对我来说至关重要的按钮切换旋转轴是来自您的链接的问题,我从未从文档中意识到它可以像 SCNQuaternion(0, 0, 1, newRotation ) 在 z 轴上旋转它并 SCNQuaternion(0, 1, 0, newOrientation) 在 y 轴上旋转它...谢谢!
猜你喜欢
  • 2023-04-07
  • 2018-03-19
  • 2014-12-05
  • 2015-01-05
  • 2018-03-02
  • 2016-01-18
  • 2021-06-26
  • 2012-06-23
  • 2018-08-24
相关资源
最近更新 更多