Search code examples
iosswiftscenekitaugmented-realityarkit

SCNNode Z-rotation axis stays constant, while X and Y axes change when node is rotated


I have a node that the user can rotate via a pan gesture. The user can select either X, Y, or Z axis and pan, and the node will rotate around that axis.

The Issue: The node's front is facing the camera. Let's say the user pans to the right and rotates the node around the Y axis. The node's front is now facing the right. If the user switches to the X axis and pans down, the node's front will rotate downward (or clockwise from the user's perspective) from it's initial right-facing orientation. This is desired behavior. The problem arises when the user switches to the Z rotation. If the user switches to Z axis rotation and pans right, the node will rotate down (clockwise from the user's perspective).

Essentially, the Z axis of the node is always constant, never moving from it's initial orientation, but the X and Y axes do change, affected by other axes' rotations.

Can anyone explain what's causing this?

Below is the code I'm using to rotate the node:

let translation = sender.translation(in: sceneView)
        var newAngleX = Float(translation.y)*Float((Double.pi)/180.0)
        var newAngleY = Float(translation.x)*Float((Double.pi)/180.0)
        var newAngleZ = Float(translation.x)*Float((Double.pi)/180.0)

        if axisSelected == "x" {
            newAngleX += currentAngleX
            node.eulerAngles.x = newAngleX
            if(sender.state == .ended) {
                currentAngleX = newAngleX
            }
        }
        if axisSelected == "y" {
            newAngleY += currentAngleY
            node.eulerAngles.y = newAngleY
            if(sender.state == .ended) {
                currentAngleY = newAngleY
            }
        }
        if axisSelected == "z" {
            newAngleZ += currentAngleZ
            node.eulerAngles.z = newAngleZ
            if(sender.state == .ended) {
                currentAngleZ = newAngleZ
            }
        }

Solution

  • As I wrote earlier in your post it's a Gimbal Lock issue. Gimbal Lock is the loss of one DoF in a three-dimensional mechanism. There are two variables in SceneKit: eulerAngles (intrinsic euler angles, expressed in radians, represent a sequence of 3 rotations about the x-y-z axis with the following order: Z-Y-X or roll, yaw, pitch) and orientation (node’s orientation, expressed as quaternion).

    To get rid of gimbal lock in ARKit and SceneKit you need to use unit quaternions, whose components satisfy the equation:

    (x * x) + (y * y) + (z * z) + (w * w) == 1
    

    For applying quaternions correctly you need to use the fourth component of this structure too (w). Quaternions are expressed in SCNVector4, where x-y-z values are normalized components, and the w field contains the rotation angle, in radians, or torque magnitude, in newton-meters).

    enter image description here

    Gimbal Lock occurs when the axes (of two of the three gimbals) are driven into a parallel configuration.