Make mesh align in the same direction as another mesh

I have a (static) mesh and I want it to align in the same direction as another dynamic mesh whose position and hence direction keeps on changing.

Can someone please suggest how this can be done and maybe link me to some relevant documentation ?

Hey, why not making your mesh a child of the other mesh?

1 Like

I did that already @Deltakosh
But, for example in this case Babylon.js Playground
the kps array keeps on changing.

sorry I’m not sure to understand then
can you repro your precise issue in the pG?

I think this is a better way of explaining the issue
https://playground.babylonjs.com/#B6XF7C#1

SO the head mesh is dynamically generated
Thus it can be present anywhere
Let’s say its positions are at one instance of time given by kps1 and at another instance by kps2 (these two arrays are present in the PG)

I need to find a way such that cap is on the head of the face mesh and is correctly aligned in terms of direction whether it be for positions in kps1 array or kps 2 array

Does this make sense @Deltakosh

@Deltakosh if in https://playground.babylonjs.com/#B6XF7C#1 you check line 15852

			var positions = kps2.slice(0,kps2.length)

interchange kps1 and kps2 probably that will give you a better understanding of what I am trying to do.

I have no line 15852 (which is far too much for debugging anyway :))

the actual code is just 100 lines(lesser than that tbh). Most of the extra fluff is just coordinate values of the custom mesh.

Oh wait…I see
So you want the face to be under the hat right?

1 Like

Yes. Exactly.
But the position and orientation of face is dynamic and changes with time.
Which I have tried to explain by creating a kps1 array and kps2 array.

ok this is a bit more tricky than expected but what about this:

  • evaluate the bounding boxes of each mesh
  • align them (so it is easier)
1 Like

Ok, I will try that. Also @Deltakosh here’s the same code properly indented and converted to <50 lines https://playground.babylonjs.com/#B6XF7C#2 . I hope this won’t give anyone anxiety by looking at it.

There’s one weird thing though that positions[5211], positions[5212], positions[5213] are supposed to be the x, y, and z coordinates of the center of the forehead.

But when I do

customMesh.position.set(positions[5211], positions[5212], positions[5213]);

this does not place the hat on the face :confused:

Can you see any reasons?

Can you make sure that your customMesh is centered on its origin?

1 Like

Aren’t meshes when created centered on the origin @Deltakosh

When meshes are created their center is placed at the origin of the world axes and their position is always placed relative to the world axes .

https://doc.babylonjs.com/babylon101/position

Besides @Deltakosh the customMesh1(hat) should be the one that changes its orientation in accordance to head(customMesh)

They are but this does not prevent you from dumping vertices where you want. You are creating a custom mesh so you are fully in control

1 Like

@Deltakosh the vertices for customMesh are actually coming from a Deep learning model. So, I don’t really have a control on where they will exactly be .

Also @Deltakosh isnt

customMesh.position.set(0,0,0,);

Doing exactly that?

it only change the position used to build the world matrix

if you mesh has all vertices with a z = 10000. Even if you set it to position(0,0,0) the mesh will be visible on z = 10000. Does it make sense?

You will have to set position to (0,0,-10000) to have your mesh visually at 0,0,0 (it is your pivot)

1 Like

Some useful links:

1 Like