5

I am trying to calculate the rotation of my device when I rotate it around the y-axis in ARKit. For clarification the y-axis in ARKit is the axis pointing upwards perpendicular to the ground.

I used eulerangles to get the rotation of the camera like this:

var alpha = sceneView.pointOfView?.eulerAngles.y

This approximately works when 0=<alpha<pi/2 and when -pi/2<alpha<=0, but for what is supposed to be other angles I get the wrong readings. I suspect this has to do with gimbal lock and that I have to somehow use quaternions to be able to get the correct angle regardless of quadrant. Any help is much appreciated!

A. Claesson
  • 529
  • 4
  • 16
  • The range of Euler angles depend on what order you define. With Euler angles, order of rotation is important (X,Y,Z or X,Z,Y doesn't describe the same 3D rotation even with same angles values). So what exactly are you trying to do ? I suppose you already read this ? https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles – Pierre Baret Mar 05 '19 at 10:20
  • I was trying to get the angle my device rotates around the Y-axis in ARKit when standing still and looking around. I needed to get the angle in a range of 0 to 360 degrees, which I didn't manage with eulerangles. However, I solved it using quaternion calculations instead. – A. Claesson Mar 05 '19 at 15:48

1 Answers1

9

Solved it by using quaternions instead of eulerangles. This is the code I used:

  guard let cameraNode = sceneView.pointOfView else { return }

  //Get camera orientation expressed as a quaternion
  let q = cameraNode.orientation

  //Calculate rotation around y-axis (heading) from quaternion and convert angle so that
  //0 is along -z-axis (forward in SceneKit) and positive angle is clockwise rotation.
  let alpha = Float.pi - atan2f( (2*q.y*q.w)-(2*q.x*q.z), 1-(2*pow(q.y,2))-(2*pow(q.z,2)) )

I used the quaternion to heading/euler conversion listed on this website.

A. Claesson
  • 529
  • 4
  • 16
  • Thanks. That's exactly what I also needed to resolve the gimbal lock problem. Although, I had to use the same formula, but without first doing "pi - ...". I guess it's only a question of where you start to measure the angle of rotation and in which direction. – rafi Feb 11 '22 at 14:14