- Home /
Gimbal lock hell even with quaternions?
I'm trying to make some flight controls and I think I'm running into gimbal lock issues since my camera view is periodically flipping out. I'm using quaternions as much as I can though, so I thought I wouldn't run into this issue. Specifically, I'm trying to change the roll and yaw and my camera will regularly spin around like crazy for a few frames, ultimately settling back into the correct position. My code is
float tarRollAngle = -tarAngle;
float tarYawAngle = tarAngle;
//Only update tar angle if it's changed enough
if(Mathf.Abs(rotateRollTarAngle - tarRollAngle) > 5)
{
rotateRollTarAngle = tarRollAngle;
rotateRollStartTime = Time.time;
rotateRollStartAngle = Globals.cockpit.transform.localRotation.eulerAngles.z;
rotateYawTarAngle = tarYawAngle;
rotateYawStartTime = Time.time;
rotateYawStartAngle = Globals.cockpit.transform.localRotation.eulerAngles.y;
}
Quaternion yawRot = Quaternion.AngleAxis(Mathf.Lerp(rotateYawStartAngle, rotateYawTarAngle, (Time.time - rotateYawStartTime) * rotSpeed), Vector3.up);
Quaternion rollRot = Quaternion.AngleAxis(Mathf.Lerp(rotateRollStartAngle, rotateRollTarAngle, (Time.time - rotateRollStartTime) * rotSpeed), Vector3.forward);
Globals.cockpit.transform.localRotation = yawRot * rollRot;
edit: I think the issue is actually caused by accessing the z and y angles from the rotations for my start angles. It's not accounting for the fact that I can get something like 357 instead of -3.
What values are you seeing in your tarYawAngle and tarRollAngle variables? If they're staying within a maximum 360-degree angle range (whether 0 to 360 or -180 to 180 or similar), then the problem actually lies in the implementation of AngleAxis using Lerp.
Otherwise, if the values continue to rise or fall endlessly while rotating, then the problem lies elsewhere.
The angles stay between -90 to 90. I have no idea how I could lerp from one angle to another without AngleAxis though.
Rather than reading in your rotation regularly, it would probably be best to keep track of your rotation yourself and implement them manually.
For example, it could look something like this:
float curRollAngle; // Your current rotation maintained explicitly
// ...
rotateRollTarAngle = tarRollAngle; // unchanged
rotateRollStartTime = Time.time; // unchanged
rotateRollStartAngle = curRollAngle;
Quaternion rollRot = Quaternion.AngleAxis($$anonymous$$athf.Lerp(rotateRollStartAngle, rotateRollTarAngle, (Time.time - rotateRollStartTime) * rotSpeed), Vector3.forward); // unchanged
Really, I only added one line and changed another. Elsewhere in your script, however, you would need to properly define a value for the current angle. Either way, the only important detail is that, rather than getting the starting rotation from the object's rotation, keep track of the value yourself. Then you'll always know what the value will be.
Your answer
Follow this Question
Related Questions
Quaternion using global values instead of local. Please help :( 0 Answers
Gimbal Lock with Quaternions 0 Answers
Rotation of child object is different in play mode 0 Answers
Quaternion rotation issue 1 Answer