Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object Rotations... #227

Open
LucaEstiva opened this issue Aug 20, 2013 · 0 comments
Open

Object Rotations... #227

LucaEstiva opened this issue Aug 20, 2013 · 0 comments

Comments

@LucaEstiva
Copy link

//------------------------------------------------------------------------------------------------------------
/**

  • Rotate the object passed as param by the angle specified. The rotation happen only about the specified axis

  • @param SourceObject:Object - The object that must by rotated

  • @param X:Number( Degrees ) - The angle, of rotation

  • @param Y:Number( Degrees ) - The angle, of rotation

  • @param RotationAxis:Vector3D - The vector tha specify the axis of rotation
    */
    //------------------------------------------------------------------------------------------------------------
    public function RotateObjectByArbitraryAxis( SourceObject:Object, X:Number, Y:Number, RotationAxis:Vector3D ):void
    {

    // GET THE ORIGINAL OBJECT TRANSFORM MATRIX
    var m:Matrix3D = SourceObject.transform;
    // ROTATION EULER BY QUATERNION
    var rv:Vector3D = new Vector3D();

    // The first Vector3D object holds the translation elements.
    // The second Vector3D object holds the rotation elements.
    // The third Vector3D object holds the scale elements.
    var VM:Vector. = m.decompose();
    // ROTATION QUATERNION
    var QRotation:Quaternion = new Quaternion();

    // NEW TRANSFORM MATRIX
    var mm:Matrix3D = new Matrix3D();

    // A Vector of 16 Numbers, where every four elements is a column of a 4x4 matrix.
    var RawMatrixData:Vector. = m.rawData;

    // ONLY ROTATION DATA REMAIN UNCHANGED
    RawMatrixData[3] = 0;
    RawMatrixData[7] = 0;
    RawMatrixData[11] = 0;
    RawMatrixData[12] = 0;
    RawMatrixData[13] = 0;
    RawMatrixData[14] = 0;
    RawMatrixData[15] = 1;
    // COPY ROTATION DATA IN TO THE NEW TRANSFORM MATRIX
    mm.copyRawDataFrom( RawMatrixData );

    // MAKE A NEW QUATERNION FROM THE NEW TRANSFORM MATRIX
    QRotation.fromMatrix( mm );
    // NORMALIZE QUATERNION
    QRotation.normalize();

    // ADD ROTATION BY ARBITRARY AXIS
    if( RotationAxis.x ) // ROLL
    {
    // Add rotation about X axis
    QRotation.fromAxisAngle( Vector3D.X_AXIS, X * DEGREES_TO_RADIANS );
    }
    else if( RotationAxis.y ) // YAW
    {
    // Add rotation about Y axis
    QRotation.fromAxisAngle( Vector3D.Y_AXIS, Y * DEGREES_TO_RADIANS );
    }
    else if( RotationAxis.z ) // PITCH
    {
    // Add rotation about Z axis
    QRotation.fromAxisAngle( Vector3D.Z_AXIS, X * DEGREES_TO_RADIANS );
    }

    //
    QRotation.normalize();
    // GET ANGLE OF ROTATIONS FROM QUATERNION
    QRotation.toEulerAngles( rv );

    // UPDATE THE NEW TRANSFORM MATRIX
    mm.appendRotation( rv.x * RADIANS_TO_DEGREES, Vector3D.X_AXIS );
    mm.appendRotation( rv.y * RADIANS_TO_DEGREES, Vector3D.Y_AXIS );
    mm.appendRotation( rv.z * RADIANS_TO_DEGREES, Vector3D.Z_AXIS );
    mm.appendTranslation( VM[0].x, VM[0].y, VM[0].z );
    mm.appendScale( VM[2].x, VM[2].y, VM[2].z );
    // COPY THE NEW MATRIX IN TO THE ORIGINAL MATRIX
    m.copyFrom( mm );
    // APPLY THE NEW TRANSFORM TO THE OBJECT
    SourceObject.transform = m;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant