@@ -678,6 +678,10 @@ public void GenerateRotationAnimation(IIGameNode gameNode, List<BabylonAnimation
678678 {
679679 if ( isRotationAnimated ( gameNode ) || force )
680680 {
681+ BabylonQuaternion q_previous = null ;
682+ var s_babylon = new BabylonVector3 ( ) ;
683+ var t_babylon = new BabylonVector3 ( ) ;
684+
681685 ExportQuaternionAnimation ( "rotationQuaternion" , animations , key =>
682686 {
683687 var localMatrix = gameNode . GetLocalTM ( key ) ;
@@ -688,17 +692,28 @@ public void GenerateRotationAnimation(IIGameNode gameNode, List<BabylonAnimation
688692 }
689693
690694 var tm_babylon = new BabylonMatrix ( ) ;
691- tm_babylon . m = localMatrix . ToArray ( ) ;
692-
693- var s_babylon = new BabylonVector3 ( ) ;
694695 var q_babylon = new BabylonQuaternion ( ) ;
695- var t_babylon = new BabylonVector3 ( ) ;
696696
697+ tm_babylon . m = localMatrix . ToArray ( ) ;
698+
697699 tm_babylon . decompose ( s_babylon , q_babylon , t_babylon ) ;
698700
701+ if ( q_previous != null )
702+ {
703+ // avoid jump
704+ // flip the sign of the current quaternion if it makes the distance between the quaternions in the 4D vector space smaller.
705+ // any given rotation has two possible quaternion representations, and then the sign is ambigous.
706+ // If one is known, the other can be found by taking the negative of all four terms.This has the effect of reversing both the rotation
707+ // angle and the axis of rotation.So for all rotation quaternions, (q 0, q 1, q 2, q 3) and(− q 0, − q 1, − q 2, − q 3) produce identical rotations
708+ if ( ( q_babylon - q_previous ) . SquaredNorm ( ) > ( q_babylon + q_previous ) . SquaredNorm ( ) )
709+ {
710+ q_babylon = - q_babylon ;
711+ }
712+ }
713+ q_previous = q_babylon ;
699714 // normalize
700715 var q = q_babylon ;
701- float q_length = ( float ) Math . Sqrt ( q . X * q . X + q . Y * q . Y + q . Z * q . Z + q . W * q . W ) ;
716+ float q_length = ( float ) Math . Sqrt ( q . SquaredNorm ( ) ) ;
702717
703718 return new [ ] { q_babylon . X / q_length , q_babylon . Y / q_length , q_babylon . Z / q_length , q_babylon . W / q_length } ;
704719 } ) ;
0 commit comments