Help: Transformations and approximation errors

I have a scenegraph where I rotate objects around multiple points. To do this I have to move the objects between different nodes and after a rotation I copy the WorldRotation to the LocalRotation.



This brings up some small approximation errors and objects get out of alignment with each other. In this simulation I know that my object only can have a certain amount of rotation angles.



Each object can only have the angles 0, 1/2pi, pi or 3/2pi around UNIT_X,Y or Z.



My plan was: Realign the objects every X frames



My implementation:



cube.getLocalRotation().toAngles( m_angles );
for( int m = 0; m < 3; m++ )
{
    m_angles[m] = FastMath.HALF_PI * FastMath.floor( m_angles[m]/FastMath.HALF_PI + 0.5f );
}
cube.getLocalRotation().fromAngles( m_angles );



This solve the alignment problem but the axes flips for some angles.

Just doing
cube.getLocalRotation().toAngles( m_angles );
cube.getLocalRotation().fromAngles( m_angles );

doesn't give you the same Quaternion when the x angle is 0 and the y and z angle is 1/2pi.

Question:

Anyone see any obvious solution to this problem? Still pretty new to jMonkey so I might have missed some nice feature or misunderstood something ;).

You can also make your computation more stable, by making it independent of previous results.

Yeah I have been thinking about that solution and I will try. It will require some more computation each frame instead of a fix every x frames.



But I still don't understand why I can't do



cube.getLocalRotation().toAngles( m_angles );

cube.getLocalRotation().fromAngles( m_angles );



without flipping axes in some cases. I would still like to use fromAngles and get the same quat I had before :confused:



Update: Kindof missunderstood how euler angles worked.