Rotation order switch?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Is there a way to make conditions switch based which axis of a sphere is vertical?
I have a sphere that rotates when it moves. When it moves along the X axis it rotates on its Z, when it moves along Z, it rotates on X. The problem I run into is gimbal lock. I think I know away around it but I am not sure how to implement it. My rotation order is zyx, so it works perfectly in going in Z direction because the X rotation is dominant. However the Z rotation axis and the Y rotation axis are spinning around X, so every 90 degrees they switch. So I put an if/else on the Y and Z rotation so that when it hits 45 degrees it switches which axis to rotate on for X direction from Z to Y. It works but not I need to expand it so that it works indefinitely.
Meaning after 45 the first degrees of rotation on the X (when translated in Z) and every 90 degrees afterward (next at 135 then 225, etc) it switches which axis between Z and Y to rotate when moved on the X axis.