I have a procedurally generated map where I know where I want to place things and am using Arrow Component to lego piece blocks together.
Here is my Actor (nodes) and Child Actor (node edges) component setup
USceneComponent = Root (ANode)
UMeshComponent
UChildActorComponent
USceneComponent = Root (ANodeEdge)
UArrowComponent
UBoxComponent
UChildActorComponent
...
Now, I SetWorldTransform() for one arrow component to where I want it to be, but it moves relative of everything else. I am guessing I need to calculate an offset and move the Node root component instead. I have been experimenting but can’t get it to snap correctly.
What offset do I need to calculate for “Node” USceneComponent when I know exactly what world transform I want for the child “NodeEdge” UArrowComponent? Or maybe there is a way to lock the transforms together?
Well, I realized I had to apply the rotations before the translations in order to get proper offsets.
I’ll just leave this here…
void SnapParentToChildSubComponent(AActor* const Parent,
const USceneComponent* const Offset,
const FTransform& TowardTransform)
{
// Step 0. Calculate and apply mirror rotation to update world space
FRotator SnapRotator = TowardTransform.Rotator();
const float ParentYaw = Parent->GetTransform().Rotator().Yaw;
const float OffsetYaw = Offset->GetComponentTransform().Rotator().Yaw;
const float TowardYaw = SnapRotator.Yaw;
SnapRotator.Yaw -= 180.f;
SnapRotator.Yaw += (ParentYaw - OffsetYaw);
Parent->SetActorRotation(SnapRotator.Quaternion());
// Step 1. Calculate and apply distance offsets
const FVector ParentTranslation = Parent->GetTransform().GetLocation();
const FVector OffsetTranslation = Offset->GetComponentTransform().GetLocation();
const FVector TowardTranslation = TowardTransform.GetLocation();
const FVector SnapTranslation = TowardTranslation +
(ParentTranslation - OffsetTranslation);
Parent->SetActorLocation(SnapTranslation);
}