Calculate drift offset and replace rotation of a box

Hey guys, i have this scenario:

I got rotation data from a gyro sensor (xyzw). In the first 5 seconds the data is drifting, but after that time it finds its position and the data is without drift.

So when the data is steady I want to calculate the offset from the current position to the original position and attach it back to the box, so I can use the Gyro but in the original position.

Does anybody know how to achieve that?

Let’s solve this by pretending the drift is happening in one dimension, then converting that logic to Quaternion functions.

You can store the initial rotation (A) in a Quaternion, then store the “post-drift” rotation (B) in a second Quaternion. Then the third important value is the current rotation (C). If these were numbers, you’d be able to calculate the current value of the number with

Final = C - (B - A)

You can use the * operator to combine rotations (which is analogous to adding numbers), and you have .Inverse to make a quaternion “negative”. So let’s slightly alter the formula to make this easier to convert:

Final = C + -(B + (-A) )

So let’s calculate the quaternion equivalent of -(B + (-A) ), which is:

(B * A.Inverse() ).Inverse()

Since this is never going to change after the drift period, store that result. Now every frame, you just need to:

transform.rotation = C * storedDriftResult;

So there’s a starting point - see if that accomplishes the result you’re looking for.

Hey StarManta, thanks for you’re answer. I will try it out!