How to implement automatic docking of objects?

Hello. I want to implement the ability to bind objects so that the objects are magnetic to each other. I did not see anything like that in the examples. Advise where to look.

Duplicate of Snap object together.