For each object, you need to define a coordinate system at the snap point. If you only had a position and an orientation (depicted by the arrow in your images), you are leaving one degree of freedom (rotate around the arrow), and that leaves us with infinite solutions. Therefore, you need to define a complete coordinate system for the snap point.
For each object you should have:
- A transformation from object origin to world origin (let us call it
O
). You input (0, 0, 0)
and get the origin of the object in world coordinates.
- A trasnformation from snap point to object origin (let us call it
S
). You input (0, 0, 0)
and get snap point in object coordinates.
Composition of those transformation yields a transformation such that when you input (0, 0, 0)
it gives you the position of the snap point in world coordinates.
Remember that we use matrices to represent these transformations. Thus the composition of O
and S
(C = O ○ S
) is the product of their matrices (C = OS
).
As I said, we should have this for each object. We have two objects, thus there is O₁
, S₁
, and C₁
for the first object, and O₂
, S₂
, and C₂
for the second object.
We want the snap point of both objects to be in the same place. That is, we want C₂(0, 0, 0) = C₁(0, 0, 0)
. Futhgermore, we want them to be the same transformation (C₂ = C₁
). To acoomplish this, we are going to only move the second object, respect to the world. That is, O₂
is our unkown.
Work it as a matrix equation:
C₂ = C₁
O₂S₂ = O₁S₁
O₂S₂S₂⁻¹ = O₁S₁S₂⁻¹
O₂ = O₁S₁S₂⁻¹
O₂ = O₁S₁S₂⁻¹
That should give you the transformation for the second object.
In english: you take the inverse of the tranformation to the snap point of the second object, compose it with the transformation to the snap point of the first object, and then with the transformation to the origin of the first object.