Hi! I have a question about the pivots and how those should work. I’m trying to do the following:
Create a box and set its pivot point to the corner
Create transform node to work as dummy for rotating box at specific position
Set transform node as parent for box
Make rotation to transform node
Set new parent to box and I would like that it’s position and rotation stays visually in same place
But if I replace parent to box after rotating transform node problems starts. Box position is something quite diffrent than I would expect. Is this how those parenting methods should work? Or are there some other simple ways to achieve my goal?
I created playground to demonstrate the issue. There are two boxes in the demo where other box use “mesh.parent=” method and other use “mesh.setParent()”. In both cases, the boxes jump to a new position after replacing parent.
Doing mesh.parent= will reset the mesh’s position in the scene, so the translation applied by the parent will vanish, but not the rotation (which will happen according to the pivot): Node | Babylon.js Documentation
Since the parent node already acts a pivot, you don’t need to set a pivot point for it, removing that and using setParent leaves the mesh in the same position and rotation at the end: https://playground.babylonjs.com/#96JZL6#1
Hi @carolhmj and thank you for the answer! I tested you example scene and it works as you describe
But I still wondering that is this setParent() working as intended? At least I thought that this method would also take into account the pivot change made to the mesh and that should not affect the position/rotation in the way it now does in my example scene?
I changed the scene a bit in an attempt to make things a little bit clearer. I’m drawing the pivots as yellow balls, I’m also not rotating the axis along with the node (so that we have a fixed reference point) and I set the 45 rotation on the parent without switching to the otherNode at first:
But when I change the pivot position of mesh - this method doesn’t work this way because the position is jumping after changing parent and node will not remain exactly where it is.
I made a new simply example that demonstrate a problem. And if pivot is not changed then method works as documented.
I just noticed that if I change playground version from 5.0.0-rc.0 to 4.x then this box is moving gradually to the left! I still think that this isn’t the way this method should work… and docs isn’t what should be corrected.
The setParent function doesn’t take the pivot into account, This is per design:
// current global transformation without pivot
Matrix.ComposeToRef(this.scaling, currentRotation, this.position, composedMatrix);
if (this.parent) {
composedMatrix.multiplyToRef(this.parent.computeWorldMatrix(true), composedMatrix);
}
If the question is about - why isn’t the documentation correct, then - you are right, the documentation doesn’t state the pivot is being ignored when setParent is called. There are reasons behind this design. Mainly the way the pivot is applied, and the fact that the pivot matrix will have to reset when setting parent (which would be unexpected as well, i assume). Here is a demo of the way it would work:
Thank you @RaananW for the answer and clarification! And yes my question was that is the pivot change taken into account when using setParent method. Now I can see that it just works differently than I had previously thought. Is there any possibilities that in the future this method could take into account the changes in the pivot for example with a parameter?
sure! we can look into that as a feature for the next versions. You can submit a github issue. It will probably not happen any time soon, as we are preparing for releasing 5.0