Rotation and scaling of child not preserved properly when parent is non-uniformly scaled and rotated

I am attempting to set the parent of a child mesh, and my goal is to have the original “appearance” of the child mesh preserved. In other words, the global scale, position, and rotation of the child should not change.

Generally speaking, calling child.setParent(parent) works, and the correct calculations are done to make the child’s scaling, position, and rotation relative to the parent, but this is not working when the parent is non-uniformly scaled AND rotated along some axis.

Note in this example, everything works as expected:
https://playground.babylonjs.com/#0SE830#1

Despite the fact that the parent is non-uniformly scaled and rotated on the Y axis, the child mesh’s scaling, position, and rotation remain correct after the call to setParent. (You can comment and uncomment line 30 in the playground to verify this.)

Now, consider this playground example, where the parent is rotated on X axis instead:
https://playground.babylonjs.com/#GQVQ49#2

Now, the scaling and rotation of the child are not preserved properly and it is warped. Note that even in the first playground, you can get results like this by changing the rotation of either the child and the parent to certain values.

This seems like a common problem others would encounter. Is there some obvious or easy way to overcome it? Thank for any help that can be provided!

1 Like

Hey @JesseDunlap, added the line

parent.bakeCurrentTransformIntoVertices();

https://playground.babylonjs.com/#GQVQ49#3

Hope this helps! I’m not sure if there are better ways to overcome this. Also not sure what are the full consequences of using bakeCurrentTransformIntoVertices and how that affects future parenting and rotating/scaling/translating

The method you use to apply a parent affects the result Use a Parent - Babylon.js Documentation

1 Like

Hi @gbz! I think there’s a bit of nuance to my project that might make this a little difficult to implement as a solution, but I’m talking through it now with the other two people I’m working with. In any sense, this seems to accomplish the task, so thank you for taking the time to reply! :slightly_smiling_face:

2 Likes

Good point, we noticed that as well. Originally we were attempting to just assign to the parent property, which wasn’t doing any kind of calculations/adjustments to keep the child from moving/rotating/scaling. setParent seems to get the closest, but not quite where we want it.

At a first glance, it looks like a bug to me, I ll see what I can do but at least I ll try to find the culprit :slight_smile:

1 Like

Looking more into it, I do not think it is actually possible to solve without adding extra nodes in the tree :frowning: due to the extra parent rotation adding shearing to the matrix. Basically, there is no transform that could cancel the shear induced by the parent not rotate+scale. Baking is probably the easiest here.