Okay, thank you. That’s very helpful. But if i want to move by the x and z axis. How can i attach it? And if they are already sticked. How can i separate it on move back?
The idea for the z axis would be the same as the x axis, check if the z position of the box is “inside” the attachment area, and if both x and z positions are inside, then attach the box. For separating, I’d check if the position I’m dragging to is “outside” the attachment area, and if it is, move the box to there.
Okay. For Z i understood how to do it. But it only works for first time when i’m dragging yellow box to red. If i move red box at first it takes bad position. May be i calculate bounding box incorrect?
That’s correct, it was a bounding box issue! The code was using boundingBox.minimum, which returns the minimum position of the bounding box in local space, which doesn’t change based on the position of the object (for the box with width 1, its local space is centered in the origin, so the minimum x will be -0.5). To get the minimum position the box occupies in world space, we need boundingBox.minimumWorld. And since the boxes can attach from either side, we need to take into account both the minimum and maximum positions of the bounding box, and move our box to the closest of them. A demo with those changes follows: Movement with attach points | Babylon.js Playground (babylonjs.com)
But can u help with one more feature, because i have no idea how to do it. I need the alignment by the second one axis. If it is close to edge, I need to align two boxes into one line.
Now, instead of just checking the minimum and maximum x positions, I also check their combinations with the z min and max too. I drew these spheres as a way to show where are all the possible attachment points, I find that looking at these visual guides helps me a lot while programming.
It was meant to be used internally with Babylon Native and the Hololens 2, but I see no reason why it wouldn’t work.
Simply instanciate this behavior, attach it to the mesh you want to stick to geometry, and fill the .meshes field of the behaviors with your colliders.