Understanding how to get/set world position, rotation, and scale in a hierarchy

Long time lurker, first time poster.

I have been struggling to work with the coordinate systems inside of Babylon. Specifically when it comes to jumping between world (absolute) TRS (translation/rotation/scale) and local TRS with items that are nested somewhat deep in the hierarchy.

To get/set the absolute TRS I have simply been using setParent(null) but this is resulting in odd behavior. Refer to this sandbox:
https://www.babylonjs-playground.com/index.html#737ZP0#10

I have created a simple hierarchy that is three object deep.
-Root Box
-First Box
-Cone

The root box is at zero, the first box is 1 local unit in the x, the cone is also 1 local unit in the x.

Then I rotate the each node by 90deg in the local z and uniformly scale the first child by 2.

If you run the sandbox you will see that the cone lands up and left of the root box, as expect.

At this point I want to get (or set) the absolute position, rotation and scale of the cone.

Now uncomment line 38 where I set the cone’s parent to null and run it again.

You will note the cone ends up in a different position and rotation but with the correct scale. This is where I am confused. I expect the cone to visually stay in the same spot but have it’s TRS changed to the world coordinate system.

Furthermore, if you uncomment line 35 and 38 and run the sim, it works as expected. I have not tried a deeper hierarchy but having to deconstruct the entire hierarchy seems painful.

2 Questions:

  • Am I simply misunderstanding how the set parent call works?
  • Is there a better way to get/set world translation, rotation, and scale?

Hey! and welcome to the poster world :smiley:

So the best option to get your TRS is simply to decompose a mesh world matrix. Something along these lines:

var worldMatrix = mesh.getWorldMatrix();
var quatRotation =  new BABYLON.Qauternion();
var position = new BABYLON.Vector3();
var scale = new BABYLON.Vector3();

worldMatrix.decompose(scale, quatRotation, position);

Thanks @Deltakosh! What about setting world TRS? I see matrix.Compose in the docs but I am not seeing node.setWorldMatrix.

Yes this was not supported but next nightly will let you do that:

mesh.freezeWorldMatrix(yourMatrix);

Nice! Thank you.

1 Like