Author Topic: child transformations  (Read 2339 times)

Offline dutch_delight

  • int
  • **
  • Posts: 58
    • View Profile
child transformations
« on: March 26, 2011, 07:41:54 pm »
Just wandering if anyone could help me (again)

I've got player helicopter in a hierarchy in this order:

playerlift  - (dummy) controls the forward speed and Y rotation (steering)
playertilt  - (dummy) tilts the chopper forwards and backwards to indicate movement
player - (Object3D) rotates in the Z direction for banking the chopper
missiles - (Object3D) inherits all rotations untill fired.

The helicopter height never changes.

when I set up my models, the missiles are parented to the player so everything works fine. The problem happens when I fire a missile. I un-parent the missile so that it can continue it's current direction. But when doing so it resets to it's original position and rotation.

Now, my maths being what it is I'm a little bit stuck here (again).
There are some threads about this but could anyone give me a pointer on how to do this?

I've got this from the forum:
direction.matMul(parent.getWorldTransformation().invert3x3());

But how would I use this in a hierarchy like mine? Should I get the transform of each parent and apply that to my missiles?


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: child transformations
« Reply #1 on: March 28, 2011, 08:23:51 am »
Get the wolrld transformation from the missle, store (maybe a copy of) the returned matrix, decouple the matrix from its parent, split the stored matrix into the rotational part (the upper left 3x3 matrix) and the translational part (getTranslation() in Matrix). Then set these values as new rotation and translation for the missle.

Offline dutch_delight

  • int
  • **
  • Posts: 58
    • View Profile
Re: child transformations
« Reply #2 on: March 29, 2011, 10:56:42 am »
Thanks, I'll try doing that tonight.
(now, where did i put my "Maths for dummies" book?)   ;)

Offline dutch_delight

  • int
  • **
  • Posts: 58
    • View Profile
Re: child transformations
« Reply #3 on: April 07, 2011, 12:44:48 am »
Sorry to keep buggin you but I cant get my missiles to rotate properly. With this code, the missiles are translated roughly to the right position but the rotation doesnt work. Any chance you could point me in the right direction? Also, is there a nicer way to do the rotation code?

Code: [Select]
SimpleVector missile_translate = this_missile.getWorldTransformation().getTranslation();
Matrix m = new Matrix(playerTilt.getRotationMatrix());
float[] dm = m.getDump();

this_missile.removeParent(player);

this_missile.translate(missile_translate);
this_missile.rotateX(dm[12]);
this_missile.rotateY(dm[13]);
this_missile.rotateZ(dm[14]);


Thanks for any help.





Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: child transformations
« Reply #4 on: April 07, 2011, 07:14:12 am »
Try this (not tested):
Code: [Select]
Matrix wt=this_missile.getWorldTransformation();
 
  SimpleVector missile_translate = wt.getTranslation();
  Matrix m = new Matrix(wt);
  m.setRow(3,0,0,0,1);
         
  this_missile.removeParent(player);

  this_missile.translate(missile_translate);
  this_missile.setRotationMatrix(m);
 

Offline dutch_delight

  • int
  • **
  • Posts: 58
    • View Profile
Re: child transformations
« Reply #5 on: April 11, 2011, 09:28:44 pm »
Excellent, thank you very much.

The missiles now move along the correct axis but seem to be offset from the main body for some reason. I think it may have something to do with my initial setup.
This is what I have to do on the initial setup to get them in the correct position (mounted underneath the chopper wings)

Code: [Select]
SimpleVector[] missile_offset = new SimpleVector[missile_count];
missile_offset[0] = new SimpleVector(-390, 572, 130);
missile_offset[1] = new SimpleVector(-480, 572, 130);
missile_offset[2] = new SimpleVector(-407, 572, 130);
missile_offset[3] = new SimpleVector(-467, 572, 130);
missile_offset[4] = new SimpleVector(-390, 582, 130);
missile_offset[5] = new SimpleVector(-480, 582, 130);
missile_offset[6] = new SimpleVector(-407, 582, 130);
missile_offset[7] = new SimpleVector(-467, 582, 130);

texMan.addTexture("tmissile", new Texture(res.openRawResource(R.raw.tmissile)));
missile_template = (Loader.loadSerializedObject(res.openRawResource(R.raw.mmissile)));
missile_template.setTexture("tmissile");
missile_template.compile();
missile_template.rotateX((float) -Math.PI / 2f);
missile_template.rotateMesh();
missile_template.clearRotation();

for (int i = 0; i < 8; i++) {

missile_array[i] = missile_template.cloneObject();
world.addObject(missile_array[i]);
missile_array[i].setScale(0.1f);
missile_array[i].addParent(player);
missile_array[i].setOrigin(missile_offset[i]);
missile_status[i] = 1;
}

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: child transformations
« Reply #6 on: April 11, 2011, 10:59:34 pm »
Some screen shot that shows this? I'm not sure what this "offset" looks like...

Offline dutch_delight

  • int
  • **
  • Posts: 58
    • View Profile
Re: child transformations
« Reply #7 on: April 12, 2011, 08:56:01 pm »
This image shows the left and right missile firing. too far to the left and right. Chopper is not rotated.
http://www.richterdesigns.co.uk/stuff/no_rotation_translation.jpg

Here the chopper is rotated to the left about 90 degrees. The missiles fire from the right of the screen.
http://www.richterdesigns.co.uk/stuff/rotated_90_translation.jpg

And here I am rotated 180 degrees. missiles still from the left and slightly behind the chopper.
http://www.richterdesigns.co.uk/stuff/rotated_180_translation.jpg

THis happens when I dont translate the missiles after your bit of code:
http://www.richterdesigns.co.uk/stuff/no_translation.jpg

Almost like they are rotated around an invisible point


Code: [Select]
Matrix wt=this_missile.getWorldTransformation();
 
SimpleVector missile_translate = wt.getTranslation();
Matrix m = new Matrix(wt);
m.setRow(3,0,0,0,1);
         
this_missile.removeParent(player);

this_missile.translate(missile_translate);
SimpleVector test = new SimpleVector (0,300,-150);
this_missile.translate(test);


this_missile.setRotationMatrix(m);


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: child transformations
« Reply #8 on: April 12, 2011, 10:07:36 pm »
Might be because the rotation pivots differ...well...you might want to try to skip the extraction of the translation and feed the transformation matrix as a whole into the rotation matrix. That's not correct in what jPCT thinks what a rotation matrix should actually be, but it might make it's way through the pipeline anyway...it's worth a try.

Offline dutch_delight

  • int
  • **
  • Posts: 58
    • View Profile
Re: child transformations
« Reply #9 on: May 27, 2011, 10:52:19 am »
Well, I had nearly given up but I fixed it last night. Here's the code if anyone needs it. I also forgot about a pivot reset in my Missile_move function which caused another offset. (or could have been the problem al along)

Thanks Mr Olsen. ;)

Code: [Select]
Matrix wt = this_missile.getWorldTransformation();
Matrix m = new Matrix(wt);
m.setRow(3,0,0,0,1);

this_missile.removeParent(player);
this_missile.setRotationMatrix(m);
this_missile.setTranslationMatrix(wt);

//missiles are too high in world space for some reason, this lowers them
SimpleVector move_down = new SimpleVector(0,7,0);
this_missile.translate(move_down);