【问题标题】:Rotating and moving SCNNode in world space在世界空间中旋转和移动 SCNNode
【发布时间】:2021-10-17 23:38:50
【问题描述】:

我正在尝试旋转和移动 SCNNode。就好像我仍在本地空间而不是世界空间中旋转一样。如果我旋转项目然后施加力,它会沿与以前相同的方向移动,只是指向一个新的方向,而不是通过旋转重新定义“向前”。我究竟做错了什么?谢谢。

func applyForce(to node: SCNNode) {
    let newTransform = SCNMatrix4Translate(node.worldTransform, force.x, force.y, force.z)
    node.setWorldTransform(newTransform)
}
    
func applyRotation(to node: SCNNode, radians: Float) {
    let newTransform = SCNMatrix4Rotate(node.worldTransform, radians, 0, 1, 0)
    node.setWorldTransform(newTransform)
}

【问题讨论】:

  • 您是否有更多关于您的问题的详细信息?我几乎不明白你的问题。你的场景设置如何?你使用动态物理体吗? (如果是,有一些默认命令可以施加力和旋转,称为扭矩)。看来,您想实现自己的力和旋转功能。您是希望您的对象移动特定时间还是要立即应用效果? (你的代码是什么样的) - 或者你为每个渲染帧都做这个?像 updateAtTime 函数吗?
  • 嗨,不,我没有使用物理实体。我不是在寻找动画,只是离散的操作。是的,我会为每个渲染帧执行一次。谢谢。我实际上已经想出了一个我会发布的答案。

标签: scenekit


【解决方案1】:

我找到了答案。我在局部空间中执行旋转并在世界空间中平移节点。 simdConvertVector 中的 nil 参数转换为世界。这实现了我的目标。

```
func applyForce(_ force: SCNVector3, to node: SCNNode) {
        let force = SIMD3<Float>(force)
        let translation = SIMD3<Float>(1, 1, 1) * force
        let newPosition = node.simdConvertVector(translation, to: nil)
        node.simdPosition += newPosition
}

func applyRotation(_ radians: Float, to node: SCNNode) {
    let newTransform = SCNMatrix4Rotate(node.transform, radians, 0, 1, 0)
    node.transform = newTransform
}
```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-11
    • 2021-10-04
    • 2013-02-06
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    相关资源
    最近更新 更多