Convert absolute position to local position of a mesh

Hello,

I hope I do not ask too much, but I am a beginner in this and after hours wasted I just have to ask.

How can I convert the absolute position of a mesh2 into a local position of mesh1?
or
How can I create and start an animation taking global positions of the node?

Background:
I have a .glb avatar which gets imported into the scene. I have to do some scaling, rotating & positioning of the root node to get it to the correct spot. The scaling is like
scene_results.meshes[0].scaling = new BABYLON.Vector3(- SCALE, + SCALE, + SCALE)
The minus x scale is to have the correct position of left and right (Maybe this is important why it doesn’t work)

Now at a trigger, I want the right hand transformNode to go to a certain absolute position of a figure. The problem is with BABYLON.Animation.CreateAndStartAnimation you assume to give the local positions from this Node as starting and ending positions. The transform node has a not-so-intuitive local position.
I tried This approach which seems intuitive, but it is not working in my example. The hand always moves to the false positions. (I think because of the scaling):

let m = new BABYLON.Matrix();        
right_hand.getWorldMatrix().invertToRef(m);
const target_pos = BABYLON.Vector3.TransformCoordinates(figure_position, m);

Is this correct? Maybe I am handling my imports wrong why it doesn’t work? Maybe the 2nd line should be

m = right_hand.getWorldMatrix().invertToRef(m);

I would rather just work with absolute positions in the animation but I think it is not possible, right? I hope you understand my dilemma, because I do not know how to recreate this situations as my project is already huge. Thanks in Advance!

Have you tried setting the scene to use right handed system like below right after you create the scene? That might help part of the problem but a PG might be needed too… :thinking:

scene.useRightHandedSystem = true;
1 Like

Your method should work, a repro in the PG would unfortunately be necessary to figure what is going wrong.

@sebavan @Blake Thank you for the responses. The right hand system actually thins out the necessity to scale with a -x. But the problem preserveres until now.

I have made a playground which I think simulates the problem https://playground.babylonjs.com/#IF31BI#1

1 Like

@Evgeni_Popov could you have a look ?

I think this is what you’re going for? I changed getWorldMatrix() to computeWorldMatrix(true) to get the updated matrix. Also invertToRef returns the unmodified matrix, which was being assigned to m.
https://playground.babylonjs.com/#IF31BI#2

2 Likes

@Blake beat me to it, I was about to answer the same thing!

Depending on what you want to do, maybe this doc (IK controller) can be useful: Bones and Skeletons | Babylon.js Documentation

2 Likes

Thanks. It worked for the playground but sadly not for my project. But now I know that this works in this way

1 Like

Did u see this ?

Maybe u can do it without code. It maybe isnt so easy with bones/joints though, i havnt tried it yet. That ball bounce is silky smooth though.