Search code examples
c#math3dquaternionseuler-angles

Translate Local quaternion rotation into global (gyro)


I need a way to get euler angles for rotation in global frame of reference from a local. I am using c#, wpf 3d and a gyroscope. I have a globe on the screen that should move in the same way as the gyro. Since the gyro sends movement relative to itself I need to use quaternions to keep the state of the object and update it but I'm stuck. If i do the following:

                var qu=eulerToQ(Gyro.X,Gyro.Y,Gyro.Z);
                GlobalQu = Quaternion.Multiply(qu, GlobalQu);

IT rotates correctly on one axis. When I rotate by A in one direction and then by B in another the rotations of the object and the gyro are no longer in the same direction because the above works for absolute rotations (relative to the world).

For example:

                    var qu=eulerToQ(KeyboardValue1,KeyboardValue2,KeyboardValue3);
                    GlobalQu = qu;

This works because I am increasing the roll,pitch,yaw ie keyboard values via keyboard always on the global axis. Gyro send rotations on LOCAL axis.

Switching the order of quaternion rotation doesn't help

                    var qu=eulerToQ(Giro.X,Giro.Y,Giro.Z);
                    GlobalQu = Quaternion.Multiply(GlobalQu,qu);

Solution

  • This actually is the proper way. If you multiply globalQuaternion * newQuaternion you get global rotation and newQuaternion * globalQuaternion then you get local rotation. I simply had a mistake in generating my quaternion.