math.gl icon indicating copy to clipboard operation
math.gl copied to clipboard

Possible bug in Euler.fromQuaternion

Open nrabinowitz opened this issue 3 years ago • 3 comments

My knowledge of quaternions is limited, so feel free to close this if invalid. I'm trying to use quaternions to compose various quarter-turn rotations for a personal project. The library I'm using for rendering requires Euler angles as input. My approach is roughly:

  • Convert Euler input to quaternion q
  • Create another quaternion q2 with the desired rotation
  • Multiply q by q2
  • Convert q to Euler output

In (at least?) one case I've found, the Euler output is not what I expect - since all input is a quarter turn (TAU / 4), all output angles should be multiples of quarter turns. When I convert the quaternion to a Matrix4 and then to a Euler angle, I get the correct output, but when I convert directly from quaternion to Euler the output is incorrect.

Example code

  const TAU = 2 * Math.PI;
  const e = new Euler(TAU / 2, 0, TAU / 4);
  const q = e.toQuaternion();
  const q2 = new Quaternion().rotateY(-TAU / 4);
  q.multiply(q2, null);
  const m = new Matrix4().fromQuaternion(q)
  new Euler().fromRotationMatrix(m);   // 0, TAU/4, -TAU/4 
  new Euler().fromQuaternion(q);      // TAU/4, TAU/4, 0.4636476090008061

While I wouldn't necessarily expect Euler().fromQuaternion(q) to have the same results as the quaternion-matrix-Euler conversion, one of the Euler rotations has a totally unexpected value that is not a multiple of TAU / 4. Note that other input here (other starting angles and/or rotations) seems to mostly work correctly.

nrabinowitz avatar May 10 '22 00:05 nrabinowitz