Custom snap algorithm

Dear reader :slight_smile:

I have an issue with calculating a world position based on combining world and relative positions/transforms.

I have two components (A & B). Each component has a child component (A1 and B1). Now I would like to move B1 to A1, but it should move it parent as well. So I would like to calculate the new world position of Component B, but the child-components A1 and B1 should snap together (Forward vectors are pointing to each other).

This is the (pseudo) code I currently have:

FVector posA1 = compA1->GetComponentLocation();
FVector posRelB1 = compB1->GetOffset(); // Custom method
FVector posB;

auto v1 =  compA1->GetRightVector() * posRelB1.X;
auto v2 =  compA1->GetForwardVector() * posRelB1.Y;
auto v3 =  compA1->GetUpVector() * posRelB1.Z;

posB = posA1 + v1 + v2 + v3; // New world position!

What is the best way to get the relative position of a component relative to its parent?
How can combine the rotation of component A1 and the relative rotation of component B1?

Are there better approaches to do these kind of position calculations?