Drag Mesh and programmatically adjust height to not collide with other Meshes

With my mouse pointer, I would like to move a Mesh around as in this Playground. I’ve modified the original to not move with collisions because I would instead like to have the Mesh move such that:

  1. The Mesh always moves to the picked point (as it is in the Playground)
  2. As I move the Mesh around, it should dynamically move its height so that it is above any other object that it would otherwise collide with.

So if I move the red cube around and it would collide with the purple cube, it will instead be just above the purple cube. If the purple cube is stacked on top of the blue cube and the red cube would be moved to collide with the blue cube, the red cube will move above both the blue and purple cubes.

What I’ve tried: As I move around a Mesh, I check to see if it’s intersecting any other Mesh in the scene. If it is, then move the position up a touch, check for intersection, then move it up again until finally there’s no intersection with any other Mesh. The problem with this is that if I have a Mesh that I am moving and it intersects with a second Mesh, then when I move its position up to a point where it should be clear, intersectsMesh will always return true unless I re-render the scene with a scene.render().

Surely there’s a better way to do this and I imagine someone might have an example Playground where it’s already been done.

That’s strange, I guess a PG would help to see what’s going on. Also, there can be some precision problems in intersection computation where you would get a false positive if the two meshes are too close.

I added some console.out statements showing what happens if I manually move a Mesh and check for intersections out of band of re-rendering. https://www.babylonjs-playground.com/#1TZI08#16

I don’t think it’s a precision issue as the position can be such that the Meshes are quite far apart.

The side effect of render is that it will recompute the world matrices of the meshes.

When you move them and use their new positions in some computations you need to update their world matrix by calling computeWorldMatrix():


Sweet, thanks for that! I still wonder if how I’m trying to manage this is a good way or if there’s a better way to do what I am trying to accomplish.