I’m making an attempt to manage the orientation of a field in Scene Package (iOS) utilizing gestures. I’m utilizing the interpretation in x and y to replace the x and y rotation of the SCNNode.
After a protracted search I’ve realised that x and y rotation will all the time result in z rotation, because of this wonderful publish: gamedev stack trade: I am rotating an object on two axes, so why does it preserve twisting across the third axis?
So I’m making an attempt to get the z rotation causes, after which take away this from my object by making use of the inverse quaternion nevertheless once I rotate the article 90 deg round x, after which 90 deg round Y it behaves VERY weirdly.
It’s nearly behaving as it’s in gimbal lock, however I didn’t assume that utilizing quaternion in the way in which that I’m would trigger gimbal lock on this approach.
I’m positive it’s one thing I’m lacking, or maybe I’m not in a position to take away the z rotation on this approach.
Thanks!
The code I’m utilizing to rotate throughout pan gesture is:
@objc func panGestureQ(_ gestureRecognize: UIPanGestureRecognizer) {
guard let boxNode = boxNode else { return }
let translation = gestureRecognize.translation(in: gestureRecognize.view!)
let angleY = Float(translation.x) * 0.003
let angleX = Float(translation.y) * 0.003
let rotationX = simd_quaternion(angleX, simd_float3(x: 1, y: 0, z: 0))
let rotationY = simd_quaternion(angleY, simd_float3(x: 0, y: 1, z: 0))
let mixed = rotationX * rotationY
let tempNode = SCNNode()
tempNode.simdOrientation = boxNode.simdOrientation
let zBefore = tempNode.eulerAngles.z
tempNode.simdOrientation = mixed * tempNode.simdOrientation
let zAfter = tempNode.eulerAngles.z
let zDiff = zAfter - zBefore
let rotationZ = simd_quaternion(zDiff, simd_float3(x: 0, y: 0, z: 1))
boxNode.simdOrientation = rotationZ.inverse * mixed * boxNode.simdOrientation
gestureRecognize.setTranslation(CGPoint.zero, in: gestureRecognize.view)
}
I’ve added a video of the unusual behaviour right here
And the code instance is right here