SceneKit.如何围绕其轴旋转SCNNode

Анд*_*шин 5 axis rotation scenekit scnnode

我正在尝试实现与场景编辑器相同的旋转,但是使用代码使对象始终围绕所选轴旋转,但是如果我在编辑器中查看角度(x,y,z),它们会随机变化

![本地节点轴] [1]

我试图使用四元数,但无法使其正常工作

PS.我的坏是使用旋转属性而不是方向SCNVector4,已正确阅读文档)

Ant*_*ich 6

看起来你真的很接近,你不得不在GLKQuaternionMultiply调用中交换参数.我在/sf/answers/2786914091/中使用了解决方案,仅通过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)
Run Code Online (Sandbox Code Playgroud)

旋转arround Y:

    // Rotate around Y axis
    let multiplier = GLKQuaternionMakeWithAngleAndAxis(0.5, 0, 1, 0)
    glQuaternion = GLKQuaternionMultiply(glQuaternion, multiplier)
Run Code Online (Sandbox Code Playgroud)