Jump to content

Matrix decompose() issue ?


satguru
 Share

Recommended Posts

Not sure if this is a bug or just my misunderstanding.

I am trying to make a mesh  parent of another mesh without changing the transforms (position, rotation and scaling) of the  child mesh in the world frame of reference

Similar to this discussion

Keep child's world position when parenting

As shown in the above discussion I use the following kind of function to do this

function makeParent(child, parent) {
  var invParentMatrix = Matrix.Invert(parent.getWorldMatrix());  
  var newMatrix = child.getWorldMatrix().multiply(invParentMatrix);
  newMatrix.decompose(child.scaling, child.rotationQuaternion, child.position);
  child.parent = parent;
}

This works fine as long as the scaling of the parent is the same in x, y and z direction.

It breaks down if I change the parent scaling in any one of those direction.

Here is a playground example

http://www.babylonjs-playground.com/#28IXSE#4

Here we have two meshes - box1 and box2.

I am trying to make box1 parent of box2.

Clicking the canvas calls a parenting function (similar to the one above) to do so.

To try

First time,

do a Run and then click the canvas.

box1 will become parent of box2 and nothing will change  on the screen - as expected.

 

Second time

uncomment line 19 - this changes the scale of box1 in the z direction.

Again click run and then click the canvas.

This time box2 is distorted.

 

Sorry for the long post.

I have spent hours on this and can't figure out what's wrong :(

 

 

 

 

 

 

Link to comment
Share on other sites

Matrix.decompose() is working correctly in this case.
The problem is that the two boxes are rotated differently. If you scale the parent it means that you will create a shearing on the child because your are not scaling in the local coordinate system of the child anymore. Its impossible to decompose the shearing matrix into plain local transforms (scale, rotate, position)

Link to comment
Share on other sites

@adam

That is great !!

Don't understand all the maths though :)

Thanks for working on this.

As far as decompose is concerned I am still not convinced that it can't handle different scales.

@Kesshi

Not sure why you think this is a case of shear matrix.

As far as my little understanding of transform matrix goes, a shear matrix would occur if the axis of a frame of reference are not at right angles to each other.

That is not the case here even though the effect looks like a shear 

 

Link to comment
Share on other sites

@satguru The workaround from @adam works because he adds an additional object in the hierarchie.
So you have box1 -> box2 -> innerbox2
innerbox2 is the box geometry which can be freely rotated and scaled. box2 is an helper which is used to negate the transformations of box1.
Because box2 is not rotated you will not have shearing ... the scaling directions are identical.

I used this kind of workaround in AutoCad some years ago.There you have the same problem if you group (block) objects.
If you scale the parent and one children is rotated, you can't ungroup them anymore.

Link to comment
Share on other sites

If there was a function that would allow us to scale an object on any axis, then your first method would work.  The benefit of using this workaround is that after you parent the object to a scaled mesh, you can freely rotate the inner box/mesh and not have to worry about the scaling.  If you didn't use an inner mesh, you'd have to update the scaling when rotating the child mesh.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...