Gimbal Lock Problem
Hello everyone, I have this problem with Quaternions, I tried to pass the rotation of some objects to the rotation of one object, but at some angles I get the Gimbal Lock problem, like when the X rotation is 90, the Y rotation rotates on the Z axis, this is the code:
switch (axis)
{
case Axis.X:
switch (typeOfEditing)
{
case EditType.Rotation:
if (oldRotAxis != transform.localEulerAngles.x)
{
newRotAxis = transform.localEulerAngles.x - oldRotAxis;
objToMove.localRotation = Quaternion.AngleAxis(newRotAxis, Vector3.right) * objToMove.localRotation;
follower.localRotation = Quaternion.Euler(newRot);
oldRotAxis = transform.localEulerAngles.x;
}
break;
}
break;
case Axis.Z:
switch (typeOfEditing)
{
case EditType.Rotation:
if (oldRotAxis != -transform.localEulerAngles.x)
{
newRotAxis = -transform.localEulerAngles.x + oldRotAxis;
objToMove.localRotation = Quaternion.AngleAxis(newRotAxis, Vector3.forward) * objToMove.localRotation;
follower.localRotation = Quaternion.Euler(newRot);
oldRotAxis = -transform.localEulerAngles.x;
}
break;
}
break;
case Axis.Y:
switch (typeOfEditing)
{
case EditType.Rotation:
if (oldRotAxis != transform.localEulerAngles.y)
{
newRotAxis = transform.localEulerAngles.y - oldRotAxis;
objToMove.localRotation = Quaternion.AngleAxis(newRotAxis, Vector3.up) * objToMove.localRotation;
follower.localRotation = Quaternion.Euler(newRot);
oldRotAxis = transform.localEulerAngles.y;
}
break;
}
break;
}
Is there a method, to apply a rotation to each axis and evading the Gimbal Lock? I have tried also the Quaternion.Euler but gives me the same results.
Answer by hectorales · Mar 05, 2018 at 06:53 PM
Quaternions are a complicated thing...
So I got the answer from this:
https://forum.unity.com/threads/understanding-rotations-in-local-and-world-space-quaternions.153330/
which I change:
objToMove.localRotation = Quaternion.AngleAxis(newRotAxis, Vector3.up) * objToMove.localRotation;
to:
objToMove.localRotation = objToMove.localRotation * Quaternion.AngleAxis(newRotAxis, Vector3.up);
So pretty much the order actually matters, and now it works.
Can anyone explain why the order matters?
Your answer
Follow this Question
Related Questions
Need help understanding Quaternion.AngleAxis 0 Answers
Rotate a GameObject a fixed amount on key press struggles. Stops at 180. 1 Answer
Making 2D Compass From Quaternion 1 Answer
How do I orient one axis of an object following LookAt/LookRotation? 0 Answers
Rotating transform more than 180 degrees 2 Answers