I was getting some odd results with what I was expecting from transforms so I setup a test. There are several problems here:
- When I move the parent bone -100 in x, the local location of the child bone is -100 - it should be 0. In world space it should be -100.
- ChildTransform and ChildTransform2 should be the same.
Here’s a pastebin with cleaner formatting:
FName ParentBoneName = TEXT("Root");
FName ChildBoneName = TEXT("Turret");
Turret->Mesh->SetBoneLocationByName(ParentBoneName, FVector(-100, 0, 0), EBoneSpaces::WorldSpace);
TestEqual(TEXT("Parent Transform"), Turret->Mesh->GetBoneTransformByName(ParentBoneName, EBoneSpaces::WorldSpace).GetLocation(), FVector(-100, 0, 0));
TestEqual(TEXT("Child Location"), Turret->Mesh->GetBoneLocationByName(ChildBoneName, EBoneSpaces::ComponentSpace), FVector(0, 0, 20));
TestEqual(TEXT("Child Location"), Turret->Mesh->GetBoneLocationByName(ChildBoneName, EBoneSpaces::WorldSpace), FVector(-100, 0, 20));
Turret->Mesh->SetBoneRotationByName(ParentBoneName, FRotator(45, 0, 0), EBoneSpaces::ComponentSpace);
TestEqual(TEXT("Parent Transform"), Turret->Mesh->GetBoneTransformByName(ParentBoneName, EBoneSpaces::WorldSpace).GetRotation().Rotator(), FRotator(45, 0, 0));
TestEqual(TEXT("Child Rotation"), Turret->Mesh->GetBoneRotationByName(ChildBoneName, EBoneSpaces::ComponentSpace), FRotator(0, 0, 0));
TestEqual(TEXT("Child Rotation"), Turret->Mesh->GetBoneRotationByName(ChildBoneName, EBoneSpaces::WorldSpace), FRotator(45, 0, 0));
FTransform ChildTransform = Turret->Mesh->GetBoneTransformByName(ChildBoneName, EBoneSpaces::WorldSpace);
FTransform LocalChildTransform = ChildTransform.GetRelativeTransform(Turret->Mesh->GetBoneTransformByName(ParentBoneName, EBoneSpaces::WorldSpace));
FTransform ChildTransform2 = LocalChildTransform.GetRelativeTransformReverse(Turret->Mesh->GetBoneTransformByName(ParentBoneName, EBoneSpaces::WorldSpace));
TestEqual(TEXT("Transform Location"), ChildTransform.GetLocation(), ChildTransform2.GetLocation());
TestEqual(TEXT("Transform Rotation"), ChildTransform.GetRotation(), ChildTransform2.GetRotation());