Author Topic: after object rotation what happens to object axis?  (Read 3294 times)

Offline tjm

  • byte
  • *
  • Posts: 17
    • View Profile
after object rotation what happens to object axis?
« on: March 29, 2010, 11:10:18 pm »
This has been driving me crazy all weekend and all day!!

If an object is rotated 90 degrees around it's X axis (so the object's Y axis is now aligned with the world's Z axis ), when it's translated along the Z axis it actually moves along the world's Y axis ..... is this correct?

And as a follow on, after rotating an object how can it be re-aligned with the world's axis so that the rotation is not undone?

TIA,
Tim.

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: after object rotation what happens to object axis?
« Reply #1 on: March 30, 2010, 02:40:59 am »
If an object is rotated 90 degrees around it's X axis (so the object's Y axis is now aligned with the world's Z axis ), when it's translated along the Z axis it actually moves along the world's Y axis ..... is this correct?
For the behavior you are looking for here, you need to convert your translation from world-space into object-space.  This is done by taking the rotations of the rotated Object3D and applying them to the translation Vector3D:

Code: [Select]
        // Grab a handle to the Object3D's rotation matrix:
        Matrix m = new Matrix( myObject3D.getRotationMatrix() );
        // Apply this rotation matrix to the translation vector:
        myVector3D.matMul( mI );

Important note:  If your rotated Object3D is the child of another Object3D, you will most likely want to take ALL of the inherited rotations into account.  This takes a few more lines of code, but it's not too difficult:

Code: [Select]
        // Grab a handle to the Object3D's world transformation matrix:
        Matrix m = new Matrix( myObject3D.getWorldTransformation() );
        // Turn that into a rotation matrix:
        float[] dm = m.getDump();
        for( int i = 12; i < 15; i++ )
        {
            dm[i] = 0;
        }
        dm[15] = 1;
        m.setDump( dm );
        // Apply this rotation matrix to the translation vector:
        myVector3D.matMul( mI );

And as a follow on, after rotating an object how can it be re-aligned with the world's axis so that the rotation is not undone?
This can be done by rotating the mesh of the Object3D and then replacing its rotation matrix with a new one:

Code: [Select]
        myObject3D.rotateY( (float)Math.PI / 2.0f );  // do some rotations
        myObject3D.rotateMesh();
        myObject3D.setRotationMatrix( new Matrix() );

Note: I pulled these off the top of my head, so you may need to check for typos  ;D

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: after object rotation what happens to object axis?
« Reply #2 on: March 30, 2010, 07:09:07 am »
If you want to translate along the (virtual) object axes only, you can use the get?Axis()-methods in Object3D as well to get the correct axes in world space.
« Last Edit: March 30, 2010, 07:48:13 am by EgonOlsen »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: after object rotation what happens to object axis?
« Reply #3 on: March 30, 2010, 08:52:34 am »
This has been driving me crazy all weekend and all day!!
BTW: That's totally normal when starting with 3D... ;D
« Last Edit: March 31, 2010, 03:10:42 pm by EgonOlsen »

Offline tjm

  • byte
  • *
  • Posts: 17
    • View Profile
Re: after object rotation what happens to object axis?
« Reply #4 on: March 31, 2010, 02:49:53 pm »
Thanks for the help.  I was rotating the object, not the mesh. Clearly that has been causing the unexpected object behaviors.

Sorry about the delay in replying .... it's Spring break here and my 2 kids (ages 6 and 3) are home all week.
Not getting much done at all right now.

--Tim.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: after object rotation what happens to object axis?
« Reply #5 on: March 31, 2010, 03:11:40 pm »
Just keep in mind that rotating the mesh is expensive. You shouldn't do this every frame. Rotating the object is actually the right way, you just have to take care of your translations.