SceneKit. How to rotate SCNNode around its axis

I try to achieve the same rotation as in the scene editor, but with the code, so the object always rotates around the selected axis, however, if I look at the angles (x, y, z) in the editor, they change quite randomly

! [Local axis node] [1]

I tried to use quaternions, but I can't get it to work

PS. my bad one used the rotation property instead of orientation like SCNVector4, I read the document correctly)

+5
source share
2 answers

It seems that you were very close, you had to change the parameters in the call to GLKQuaternionMultiply . I used the solution fooobar.com/questions/1261569 / ... to achieve rotation only with the Z axis:

  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) 

To rotate arround Y :

  // Rotate around Y axis let multiplier = GLKQuaternionMakeWithAngleAndAxis(0.5, 0, 1, 0) glQuaternion = GLKQuaternionMultiply(glQuaternion, multiplier) 
+3
source

By setting the rotation property, the absolute rotation of the object changes, not the relative rotation.

Here is some pseudo code

  • The calculation of a quaternion is a relative rotation. For this, I would use the GLKQuaternionMakeWithAngleAndAxis function.
  • Apply rotation to orientation property:
    let initial_object_orientation = rotateNode.orientation; new_orientation = GLKQuaternionMultiply(rotation_quaternion, initial_object_orientation)
  • Assign a new orientation
    rotatNode.orientation = new_orientation
    Hope this helps.
0
source

Source: https://habr.com/ru/post/1261567/


All Articles