Keep mesh on top of another mesh -> BoundingBox->maximumWorld

Hey.

I’ve created a simplified version of problem I am having.

https://www.babylonjs-playground.com/#944SLJ#1

When slider is moved, it updates the position of the ground, and then depending on that position of the ground sphere position will move.

How I’ve done that (and I have specific case scenario in my project where this seamed like a good approach) is that I a take maximumWorld.y position of the boundingBox of the ground. And I want to snap position of the sphere to that value.

Now, what you can experience is the same issue I am having in my project, when sliding up, sphere goes bellow ground, when sliding down, sphere goes above ground. Is there a way to avoid this, and to keep sphere always above ground. Why is it even swapping positions? In the case of the ground plane maximumWorld should always be in the “middle” of the mesh, or am I wrong?

Is there any other way to keep one mesh on top of another.

PS: comment line 13 on first run

Basically i need this lid to always stay on top of the bottom part.

And bottom part is controlled by scaling and specific formula, which I cannot use to set position of the lid, so I need lid to follow the bottom mesh, and boundingBox.maximumWorld seamed like great idea. But it has that weird flow.

Hi, it seems to work when the world matrix is computed first:

https://www.babylonjs-playground.com/#944SLJ#2

Wow thank you. That actually solved two of my problems. Working on that whole day -.-, and it was one line of code. :smiley:

Thank you. Cheers

1 Like

Why not just parent the lid to the container? I would assume that this is computationally lighter.

https://www.babylonjs-playground.com/#944SLJ#3

1 Like

Yes that would be better approach if I was controlling positions of both objects. But my use case is more like this.

https://www.babylonjs-playground.com/#944SLJ#5

As I mentioned above, I am controlling the scaling of the container with the very specific formula, which I cannot apply to control position of the lid, and parenting would affect scaling of the lid as well, so that doesn’t work for me.