Resolve Gimbal Lock by implementing 3D components with Quaternion-based rotations #6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The existing 2D gyroscope components suffer from Gimbal Lock, a mathematical limitation that occurs when using Euler angles for rotations. This manifests as:
As reported in the issue, users experience problems like "My phone starts at -30. I want this to be 0" and inconsistent behavior between lying the phone flat vs. holding it upright.
Solution
This PR introduces a complete set of 3D gyroscope components that use Quaternion arithmetic throughout the calculation pipeline to eliminate Gimbal Lock entirely.
New Components Added
Rotator Components:
GyroRotator3D
- Abstract base class using Quaternion operationsGyroRotator3DAttitude
- Device attitude rotation without Gimbal LockGyroRotator3DGravity
- Gravity-based rotation with smooth interpolationMover Components:
GyroMover3D
- Abstract base class with full 3D positioning supportGyroMover3DAttitude
- Position movement based on device attitudeGyroMover3DGravity
- Position movement based on gravity vectorTechnical Implementation
The key innovation is avoiding Euler angle arithmetic in the critical path:
Before (2D - Gimbal Lock prone):
After (3D - Gimbal Lock free):
Key Improvements
Migration Path
Users can easily migrate to the new components:
Documentation & Testing
Backward Compatibility
The existing 2D components remain unchanged and fully functional. The new 3D components are additive - users can adopt them at their own pace while maintaining existing functionality.
Verification
The new components resolve the specific issues mentioned:
✅ "When I place my mobile on a table, it looks like the first screenshot" - 3D components maintain consistent behavior regardless of initial orientation
✅ "My phone starts at -30. I want this to be 0" - Quaternion-based origin handling provides stable reference points
✅ "Gimbal Lock issue exists in this tool" - Mathematically resolved through Quaternion arithmetic
This implementation provides the robust 3D gyroscope solution requested while maintaining the familiar API that users already know and love.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.