satguru Posted April 30, 2016 Share Posted April 30, 2016 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 Quote Link to comment Share on other sites More sharing options...
Kesshi Posted April 30, 2016 Share Posted April 30, 2016 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) Quote Link to comment Share on other sites More sharing options...
satguru Posted April 30, 2016 Author Share Posted April 30, 2016 @Kesshi So if we cannot use decompose() to do this then what is the alternative ? Quote Link to comment Share on other sites More sharing options...
Kesshi Posted April 30, 2016 Share Posted April 30, 2016 You can use decompose but only under the conditions: - if you need to scale the parent, you can't rotate the child - if you need to rotate the child, you cant' scale the parent Sorry but i don't know a real solution for this issue. Quote Link to comment Share on other sites More sharing options...
adam Posted May 2, 2016 Share Posted May 2, 2016 This is the best solution I could come up with: http://www.babylonjs-playground.com/#28IXSE#8 Since you can only scale a mesh by x, y, z (width, height, depth), I gave box2 a container mesh to invert the scale of box1. It isn't perfect, but it might be good enough for what you are trying to accomplish. Quote Link to comment Share on other sites More sharing options...
adam Posted May 2, 2016 Share Posted May 2, 2016 I fixed the problem with box2 rotation after parenting: http://www.babylonjs-playground.com/#28IXSE#9 Quote Link to comment Share on other sites More sharing options...
satguru Posted May 3, 2016 Author Share Posted May 3, 2016 @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 Quote Link to comment Share on other sites More sharing options...
Kesshi Posted May 3, 2016 Share Posted May 3, 2016 @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. Quote Link to comment Share on other sites More sharing options...
adam Posted May 3, 2016 Share Posted May 3, 2016 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. Quote Link to comment Share on other sites More sharing options...
adam Posted May 3, 2016 Share Posted May 3, 2016 I discovered a bug when the parent is rotated. Here is the fix: http://www.babylonjs-playground.com/#28IXSE#11 Quote Link to comment Share on other sites More sharing options...
adam Posted May 4, 2016 Share Posted May 4, 2016 I created a parentMesh function that adds the extra mesh to the parent only if it is needed: http://www.babylonjs-playground.com/#28IXSE#13 Quote Link to comment Share on other sites More sharing options...
satguru Posted May 6, 2016 Author Share Posted May 6, 2016 @adam nice. thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.