Converting Quaternions to the Euler Angles that is closest to another Euler Angle

289 Views Asked by At

I have an euler angle representing an objects rotation. I want to rotate the object by a quaternion, while still preserving continuity in the euler angle representation. In the following example all Euler Angles are in xyz:

Source Euler Angle: (0, 0, 310)
Rotate by Euler Angle: (0, 0, 60)

I convert the angles to quaternions, perform the rotation and the continuity is lost:

After rotating Euler Angle: (0, 0, 10)
Desired result: (0, 0, 370)

I tried to solve this by comparing and manipulating the resulting euler angles for x, y and z respectively using the following algorithm:

new_euler.x = repeat_working(new_euler.x - src_euler.x + 180, 360) + src_euler.x - 180
new_euler.y = ...

func repeat_working (t):
    return (t - (floor(t / 360) * 360))

It still produces jumping values for Y and Z when rotating around the X-Axis. When rotating around the Y-Axis the values are alternating instead of increasing continously (Rotations are performed around the Z axis, the X axis, and the Y axis, in that order).

Could someone point out my mistake, or a better alternative method of doing this? Ideally some code examples would be appreciated.