Author Topic: Translating Child Object3D  (Read 1881 times)

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Translating Child Object3D
« on: December 28, 2008, 10:29:45 pm »
I have noticed that translating a child Object3D does not take the parent's rotations into account.  Is this a bug, or do I just need to account for parent rotations "manually"?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: Translating Child Object3D
« Reply #1 on: December 28, 2008, 10:33:51 pm »
I think it's a feature, but i'm not 100% sure if i really understand what you mean exactly. Maybe a little drawing would help!? 

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Translating Child Object3D
« Reply #2 on: December 28, 2008, 10:57:50 pm »
Oh, sorry, here is a simple test case:

http://www.paulscode.com/source/TranslateChild/

The direction of the translation vector in this case is from the base parent cube to toward the small target cube.  As you can see, the child cube moves in the wrong direction if the parent cube has been rotated with the mouse (i.e. in object space instead of world space).

Source code for the applet:
http://www.paulscode.com/source/TranslateChild/TranslateChild.java

How would I go about compensating for the parent rotations?  It seems that the computations could get complicated if the object in question is a "great great grandchild" for example.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: Translating Child Object3D
« Reply #3 on: December 28, 2008, 11:31:16 pm »
Ok, you managed to confuse me for a while, but the behavior is actually correct...albeit confusing. The "problem" is: The translation of the child gets rotated. It has to, because otherwise it would be impossible to keep the three objects in place when rotating the parent. Would it be different, this

Code: [Select]
target.translate( 0, -1.2f, 0 );
 child.translate( 0, -3, 0 );

wouldn't work when the parent rotates, because the translations would have to change constantly to keep the objects in place. In your case, this either means that you have to use a static translation (0,1,0) and (0,-1,0) and it will work, or, if that's not an option because the actual case is more complicated, revert the rotation like so (works only in this simple way, if the world transformation matrix contains no translations...if it does, you have to set them to 0 before using Matrix.set(...)) :

Code: [Select]
direction.matMul(parent.getWorldTransformation().invert3x3());
« Last Edit: December 28, 2008, 11:33:02 pm by EgonOlsen »

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Translating Child Object3D
« Reply #4 on: December 29, 2008, 12:34:14 am »
Thanks, that works nicely!

Here is the method I came up with, if anyone is interested:

Code: [Select]
    private void translateChild( Object3D childObject, SimpleVector translation )
    {
        Object3D[] parentObjects = childObject.getParents();
        if( parentObjects == null || parentObjects.length == 0 )
        {
            childObject.translate( translation );
            return;
        }

        Object3D parentObject = parentObjects[0];
       
        Matrix m = new Matrix( parentObject.getWorldTransformation() );
        float[] dm = m.getDump();
        for( int i = 12; i < 15; i++ )
        {
            dm[i] = 0;
        }
        dm[15] = 1;
        m.setDump( dm );
       
        SimpleVector worldTranslation = new SimpleVector( translation );
        worldTranslation.matMul( m.invert3x3() );
        childObject.translate( worldTranslation );
    }
« Last Edit: December 29, 2008, 12:36:53 am by paulscode »