What is the absolute position of a childMesh after repositioning the parent?

Hi guys,
I have an imported mesh from Blender which has several child meshes.
After importing I reposition the child meshes by using childMesh.position = new Vector3(x, y, z) based on some parameters the user gives me.
Then I reposition the whole mesh around the scene and at some point I need to get the position of one of the child meshes to replace it with a new mesh.
When I try to get the position of the child mesh I want to replace, with getAbsolutePosition(), I get the position of the parent mesh. (With childMesh.position I get the local position.)
I tried using computeWorldMatrix() before taking the position but it made no difference. Any thoughts on how I can get the position of this child mesh?
Any help is appreciated, thanks in advance :slightly_smiling_face:

1 Like

Hey there, welcome to the forum! :slight_smile: Did you try passing true to computeWorldMatrix to force an update before calling getAbsolutePosition?

3 Likes

@pikachu Welcome to the forums!

Would you be able to provide a playground so that we can more easily help you debug?

2 Likes

Thank you @bghgary :smile:
I can’t use the real model in the playground but I created a simplified version of the situation.
Basically I need the world coordinates of the merged spheres on the left because, I need to replace them with other meshes.
Here is the playground https://playground.babylonjs.com/#0FM9CG#1.

I’m starting to think this is related to the center point of the merged meshes. In that case how can I set the center of the merged sphere in the actual center of gravity?

Thank you @Blake :smile:
I tried that and it didn’t work. From what I tested on a simplified playground I think it is related to the fact that the child mesh is actually a mesh created from other meshes with Mesh.MergeMeshes([]) (with a simple mesh, not a merged one it works just fine).
Basically when I use computeWorldMatrix() I get the position of the parent mesh.

Aah, you’re using MergeMeshes, that creates a new mesh with position and rotation (0, 0, 0) and scaling (1, 1, 1) because the merged meshes’ world matrices are now baked into the vertices of the new mesh.

2 Likes

Is there a way to get the absoulte position of this mesh, cause it is not actually positioned in (0,0,0)?

In your playground where you pass true to computeWorldMatrix the new mesh’s absolute position is (0, 2, 0) as expected. If you don’t pass true then you get the outdated absolute position of (0, 0, 0).

But (0,2,0) is the position of the box, which is a parent to the merged spheres. I need the actual position in world coordinates for the merged object made of two spheres. How can I get the center of mass for that object? I mean I understand why we get this result but is there a workaround for me to get the actual center of the merged object?

Because the new mesh has position (0, 0, 0), its absolute position is the same as the parent as expected.

I think the world center position of the new mesh’s bounding box is what you want, like below logged on line 34. :slight_smile:

2 Likes

Thank you very much! That is exactly what I needed :smiling_face:

2 Likes