Eric,

As was pointed out (several times) this is a tricky topic, but here is some
code that we have used successfully. The code is based on an algorithm from
the Matrix and Quaternion FAQ (search the web). I have posted this code to
the list before.

The M+Q FAQ is an excellent reference for these types of questions, as no
one can remember this kind of thing for very long! I wouldn't claim to be
able to explain the details of the algorithm off the top of my head, so if
you have questions or issues I suggest you consult the FAQ and your nearest
friendly mathematician (always a good person to know!).

Note that the return value is three rotation angles *in radians*. You can
ignore (or better, implement!) the ASSERT methods. As you can see the method
is peppered with trigonometric functions, so I wouldn't go using it on every
frame or anything... Now, who would go and do a thing like that! Mr.
Billboard behavior. ;-)

Sincerely,

Daniel Selman

[EMAIL PROTECTED]
http://www.tornadolabs.com


static Point3d GetEulerRotation( Transform3D t3d )
{
        Point3d Rotation = new Point3d();

        Matrix3d m1 = new Matrix3d();
        t3d.get( m1 );

        // extract the rotation angles from the upper 3x3 rotation
        // component of the 4x4 transformation matrix
    Rotation.y = -java.lang.Math.asin( m1.getElement( 2, 0 ) );
    double c = java.lang.Math.cos( Rotation.y );
        double tRx, tRy, tRz;

        if( java.lang.Math.abs( Rotation.y ) > 0.00001 )
        {
                tRx = m1.getElement( 2, 2 ) / c;
                tRy = -m1.getElement( 2, 1 )  / c;

                Rotation.x = java.lang.Math.atan2( tRy, tRx );

                tRx = m1.getElement( 0, 0 ) / c;
                tRy = -m1.getElement( 1, 0 ) / c;

                Rotation.z = java.lang.Math.atan2( tRy, tRx );
        }
        else
        {
                Rotation.x  = 0.0;

                tRx = m1.getElement( 1, 1 );
                tRy = m1.getElement( 0, 1 );

                Rotation.z = java.lang.Math.atan2( tRy, tRx );
        }

        Rotation.x = -Rotation.x;
        Rotation.z = -Rotation.z;

        // now try to ensure that the values are positive by adding 2PI if
necessary...
        if( Rotation.x < 0.0 )
                Rotation.x += 2 * java.lang.Math.PI;

        if( Rotation.y < 0.0 )
                Rotation.y += 2 * java.lang.Math.PI;

        if( Rotation.z < 0.0 )
                Rotation.z += 2 * java.lang.Math.PI;

        ASSERT( Rotation.x >= 0.0 && Rotation.x <= 2 * java.lang.Math.PI );
        ASSERT( Rotation.y >= 0.0 && Rotation.y <= 2 * java.lang.Math.PI );
        ASSERT( Rotation.z >= 0.0 && Rotation.z <= 2 * java.lang.Math.PI );

        return Rotation;
}

-----Original Message-----
From: Discussion list for Java 3D API
[mailto:[EMAIL PROTECTED]]On Behalf Of Eric Reiss
Sent: 03 December 1999 01:40
To: [EMAIL PROTECTED]
Subject: [JAVA3D] geting rotation angles from transform3D


I was wondering if you ever got an answer to the question you posted to the
list:

"Is there a way to determine the exact rotation angles for each axes from a
given Transform3D?"

This seems to be a common issue and I am fighting with it now.


***********************************************************************
Eric Reiss - http://www.sigda.acm.org/Eric/
Email: [EMAIL PROTECTED]

SIGDA Internet Server Manager - http://www.sigda.acm.org/

Assistant Systems Manager - School of Engineering
University of Pittsburgh
***********************************************************************

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to