5
\$\begingroup\$

I have Transform component storing it's rotation as Quaternion. Transform has set/getEulerAngles (rotation order oy, ox, oz). I want to rotate object in any direction but while testing I get jumping(glitch) objects stop rotating at specific angles.

Euler angles in problem frames:

1) oy 0 ox -90 oz 0 - previous frame.

2) oy 180 ox -90 oz 180 - next frame after incrementing angle of the object.

my increment looks like:

tr->setEulerAngles(tr->eulerAngles()+Vector3(5,0,0));//rotate around ox by 5 degrees

visually this rotations are equal, but when I get angles 2) from quaternion and increment them it starts rotating in oposite direction, next frame rotation is again as 1), so there is infinite loop at this rotation (some kind of black hole;-)). What can it be? Something wrong with math?

\$\endgroup\$
4
  • 7
    \$\begingroup\$ Your problem is more or less the definition of gimbal lock. \$\endgroup\$ May 13, 2013 at 23:37
  • 7
    \$\begingroup\$ From a practical perspective, the best way to fix this is to avoid Euler angles altogether; precompute a quaternion that corresponds to a 5-degree rotation about ox and just multiply your current orientation by that 5-degree rotation each frame. \$\endgroup\$ May 14, 2013 at 0:46
  • 3
    \$\begingroup\$ As intuitive as Euler angles seem at first, they are equally that evil when you go beyond 2d. In 3d, avoid them whenever possible. \$\endgroup\$
    – Steve H
    May 14, 2013 at 4:37
  • \$\begingroup\$ Unity3d has same problem when rotation around ox axis. Thanks for help, I solved problem multiplying current rotation by quaternion created from axis angle. Comment about gimbal lock and euler angles equally evil)) I'll mark as an answer if you write it there. I thought that using quaternion do not make gimbal lock at all, but conversion to from euler angles does. \$\endgroup\$
    – Yevhen
    May 14, 2013 at 12:32

1 Answer 1

0
\$\begingroup\$

Gimbal lock is the answer, avoid using euler angles even converting to and from quaternion, use multiplication by quaternion created from angle axis.

Unity3d has same problem when rotating around ox axis:

transform.eulerAngles += new Vector3(5,0,0); //problem
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .