Unrolling rotations [Fixed]

This problem leads down a rabbit hole, but progress has been made. First I tried detecting axis flipping using the algorithm given here:

This worked beautifully. However, when the quaternions were converted back to UE rots, UE applied the anti gimble lock filters again, flipping my data ± 180 degree all over the place - so no dice!

So I created a simple macro that negates the ± 180 flipping:

It ain’t pretty, but it works. It transforms these curves:

Into these:

Naively, I thought I’d solved the problem at this point. However, it seems UE’s anti gimble lock filter has two components. The first is the axis flipping I detected and compensated for above.

The second is more subtle and only kicks in when multiple axes are non-zero and one axis is approaching 90 degrees - which happens a lot in actual mocap data, most notably in shoulder joints.

To demonstrate, here are some shoulder mocap curves in MotionBuilder:

Here are the same curves in UE:

See that little wiggle on the peak of the red curve? That does not look good when driving a JCM!

Note also how the other two curves have been amplified. I am presuming this is another artifact of UE’s anti gimble lock filter and is designed to get the joint to the desired position without any one curve approaching 90 degrees.

So my mission continues. I’m still working on my assumptions about UE’s anti gimble lock filtering - if anyone thinks I’m off track, please let me know and save me some pain!

[Footnote] Note I am using the ‘Preserve Local Transform’ options on animation import - this does help with some of the above issues, but does not remove the wiggle described in my last paragraph.