# How to calculate the transformation such that two points with different position, orientation and origins line up?

Let's say you have two different 3D Objects which both contain a node that is off center (not at the origin) and have an orientation. How do you calculate the transform for one of the object so that both of these nodes line up? As in both nodes end up at the same position in space and the same orientation?

In gaming terms, both 3D objects have "snap" points and I'd like to calculate how to transform one of them so that they snap together at that specific node.

The cubes are the 3D objects, the X/Y/Z axis represent the origin of each of these objects, and the arrow is the node. The goal would be in this case both arrows overlap and thus the cubes end up at the exact same place. Note the origin is NOT at the same spot for both objects.

## 1 Answer

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.

• worked like a charm, thanks a lot! Jan 8, 2020 at 20:43