Snap object together

I want to perform a “snap” effect where one object snaps to the other when it is moved close.

I have points of connection for both objects. I want to move the position of object1 so that the point of connection of object1 touches that of object2 (in other words, have the same position).

The points of connection are Vector3. There are many methods for Vector3 available but I’m not sure how to calculate the “snapped” position of object1 using these methods.

Another thing I would like to do is to rotate object1 if it is in the wrong rotation to allow this snapping to happen without overlapping both objects.

I appreciate the help

You could do something like this maybe:

function moveObj(position: THREE.Vector3) {
      
      if(obj.position.distanceTo( obj2.position ) < .01) { // Any value you want .01 = 1cm
         
            position.copy(obj2.position)
      }

      obj.position.copy(position)
}

Thanks for trying to help but unfortunately this does not solve my problem. Please read my post closely as I mentioned all the details.

I coded this task, it seems it is similar with your question


maybe you find there some ideas https://github.com/Dragon3DGraff/room_TestTask

you can try it here
https://dragon3dgraff.ru/room/