How to implement automatic docking of objects?

Duplicate of Snap object together.