5

I'm trying to achieve same rotation as in scene editor but with code so object always rotate around selected axis, however if I look at angles(x,y,z) in editor they changes quite randomly

![Local node axis][1]

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

PS. my bad was using rotation property instead of orientation both SCNVector4, have read doc properly)

2 Answers2

8

Seems you was really close, you had to swap parameters in GLKQuaternionMultiply call. I used solution in https://stackoverflow.com/a/39813058/4124265 to achieve rotation only by 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)
Anton Plebanovich
  • 1,296
  • 17
  • 17
0

By setting the rotation property, the absolute rotation of the object is changed, rather than the relative rotation.

Here's some pseudo code

  1. Compute the quaternion represents the relative rotation. I would use the GLKQuaternionMakeWithAngleAndAxis function to do so.
  2. Apply the rotation to the orientation property:
    let initial_object_orientation = rotateNode.orientation; new_orientation = GLKQuaternionMultiply(rotation_quaternion, initial_object_orientation)
  3. Assign the new orientation
    rotatNode.orientation = new_orientation
    Hope it helps.
  • Rewritten code with your assumptions. Looks more friendly but still working weird – Андрей Первушин Dec 21 '16 at 09:29
  • Privet ! We can separate the problem in two. 1- You need to [position the camera on a sphere](http://mathinsight.org/spherical_coordinates). The radius of the sphere will vary with the zoom level you want (unless you use the camera FOV to zoom) 2- You can use [SCNLookAtConstraint](https://developer.apple.com/reference/scenekit/scnlookatconstraint) so the camera always points (rotates) towards the selected node. – Karl Sigiscar Dec 21 '16 at 12:34
  • Sorry, this looks like not about my issue – Андрей Первушин Dec 21 '16 at 13:17
  • The selected node would be the center of the imaginary sphere used to position the camera. The camera would always point inwards toward the selected node. – Karl Sigiscar Dec 22 '16 at 15:17
  • Still can't get your idea. i'am not positiong Camera, please read attentively or explain what do you mean with code and example. – Андрей Первушин Jan 10 '17 at 10:52