gpt4 book ai didi

ios - 沿世界轴旋转

转载 作者:行者123 更新时间:2023-11-28 10:54:57 26 4
gpt4 key购买 nike

我正在做一个 SceneKit 项目,我想在三个轴(世界轴)上旋转一个对象,但是一旦我旋转了这个对象,下一次旋转是相对于新的旋转轴完成的。

想象一个立方体,您应该能够使用相对于世界的三个按钮在绝对 x、y 和 z 轴上旋转。

据我所知,SCNAction 有以下选项:

  • SCNAction.rotateTo(x: 1.0, y: 0.0, z: 0.0, duration: 0.5,使用最短单元弧:真)
  • SCNAction.rotateTo(x: 1.0, y: 0.0, z: 0.0, duration: 0.5)
  • SCNAction.rotateBy(x: 1.0, y: 0.0, z: 0.0, duration: 0.5)
  • SCNAction.rotate(by: 0.0, around: SCNVector3, duration: 0.5)
  • SCNAction.rotate(toAxisAngle: SCNVector4, 持续时间: 0.5)

不幸的是,这些都不是绝对的,它们都依赖于之前的旋转。

你知道有什么方法可以实现真正的绝对世界轴旋转吗?

提前感谢您的帮助!

最佳答案

要围绕世界轴旋转节点,请将节点相乘 worldTransform与旋转矩阵。

我还没有找到 SCNAction 的解决方案的,但使用 SCNTransaction非常简单。

func rotate(_ node: SCNNode, around axis: SCNVector3, by angle: CGFloat, duration: TimeInterval, completionBlock: (()->())?) {
let rotation = SCNMatrix4MakeRotation(angle, axis.x, axis.y, axis.z)
let newTransform = node.worldTransform * rotation

// Animate the transaction
SCNTransaction.begin()
// Set the duration and the completion block
SCNTransaction.animationDuration = duration
SCNTransaction.completionBlock = completionBlock

// Set the new transform
node.transform = newTransform

SCNTransaction.commit()
}

如果节点的父节点具有不同的变换,这将不起作用,但我们可以通过将生成的变换转换为父坐标空间来解决此问题。

func rotate(_ node: SCNNode, around axis: SCNVector3, by angle: CGFloat, duration: TimeInterval, completionBlock: (()->())?) {
let rotation = SCNMatrix4MakeRotation(angle, axis.x, axis.y, axis.z)
let newTransform = node.worldTransform * rotation

// Animate the transaction
SCNTransaction.begin()
// Set the duration and the completion block
SCNTransaction.animationDuration = duration
SCNTransaction.completionBlock = completionBlock

// Set the new transform
if let parent = node.parent {
node.transform = parent.convertTransform(newTransform, from: nil)
} else {
node.transform = newTransform
}

SCNTransaction.commit()
}

您可以在 this 中尝试这个 swift Playground 。

希望这就是您要找的。

关于ios - 沿世界轴旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44186179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com