我正在尝试实现与场景编辑器相同的旋转但是使用代码使对象始终围绕所选轴旋转,但是如果我在编辑器中查看 angles(x,y,z)它们会随机变化
![本地节点轴][1]
我试图使用四元数,但无法使其正常工作
PS。我的坏是使用旋转属性而不是方向 SCNVector4,已正确阅读文档)
看起来你真的很接近,你必须在GLKQuaternionMultiply调用中交换参数。我使用https://stackoverflow.com/a/39813058/4124265中的解决方案仅通过Z轴实现旋转:
GLKQuaternionMultiply
Z
let orientation = modelNode.orientation var glQuaternion = GLKQuaternionMake(orientation.x, orientation.y, orientation.z, orientation.w) // Rotate around Z axis let multiplier = GLKQuaternionMakeWithAngleAndAxis(0.5, 0, 0, 1) glQuaternion = GLKQuaternionMultiply(glQuaternion, multiplier) modelNode.orientation = SCNQuaternion(x: glQuaternion.x, y: glQuaternion.y, z: glQuaternion.z, w: glQuaternion.w)
要旋转 arround Y:
Y
// Rotate around Y axis let multiplier = GLKQuaternionMakeWithAngleAndAxis(0.5, 0, 1, 0) glQuaternion = GLKQuaternionMultiply(glQuaternion, multiplier)
通过设置旋转属性,改变对象的绝对旋转,而不是相对旋转。
这是一些伪代码
计算四元数表示相对旋转。我会使用GLKQuaternionMakeWithAngleAndAxis函数来这样做。
GLKQuaternionMakeWithAngleAndAxis
将旋转应用于 orientation 属性:let initial_object_orientation = rotateNode.orientation; new_orientation = GLKQuaternionMultiply(rotation_quaternion, initial_object_orientation)
let initial_object_orientation = rotateNode.orientation; new_orientation = GLKQuaternionMultiply(rotation_quaternion, initial_object_orientation)
指定新方向rotatNode.orientation = new_orientation希望能帮助到你。
rotatNode.orientation = new_orientation
2 回答
看起来你真的很接近,你必须在
GLKQuaternionMultiply
调用中交换参数。我使用https://stackoverflow.com/a/39813058/4124265中的解决方案仅通过Z
轴实现旋转:要旋转 arround
Y
:通过设置旋转属性,改变对象的绝对旋转,而不是相对旋转。
这是一些伪代码
计算四元数表示相对旋转。我会使用
GLKQuaternionMakeWithAngleAndAxis
函数来这样做。将旋转应用于 orientation 属性:
let initial_object_orientation = rotateNode.orientation; new_orientation = GLKQuaternionMultiply(rotation_quaternion, initial_object_orientation)
指定新方向
rotatNode.orientation = new_orientation
希望能帮助到你。