Download

Help with unrotating child component

I have a setup that uses some springarms. With springarms you can toggle inherit pitch, yaw, roll to be true or false. I have the parent component rotating and I need to have these inherits set to true so that the child inherits the rotation. However, sometimes I need to unrotate the child. So the child is rotated at state A, then the parent receives its rotation update, which also rotates the child. Now I need to unrotate the child. I’ve been trying to figure out how to do this and I’m coming up short.

A little more information on the setup. The child component location is set via InverseTransfornPosition from the parent components transform. This takes the current position of the child component at it’s current location and sets it at the parent components local location. This works perfectly. But the child component still inherits the overall rotation of the parent component. Turning off inherit Pitch, Yaw, Roll solves this but breaks the purpose of the setup which requires those to be true so that I can do rotations (when needed). As I mentioned before sometimes I need to not inherit the rotation so I’ve chosen to leave the inherits true and instead just fix the rotation when needed. At first I thought I could do this by using childcomponent->setRelativeRotation(FRotationMatrix::MakeFromZX(childCompOldUp, childCompOldForward)) but this isn’t working. I suspect because it’s returning the rotation before the parent component has rotated so it’s the wrong direction. This is where I started thinking that I could try and build a new rotation from the previous rotation state and the new rotation state, then apply the inverse. But I have no idea how to do that.

In case anyone stumbles on this the solution that worked for me was

FRotator delta = orgCenRot - newCenRot;
delta.Normalize();
UserArm->AddRelativeRotation(delta);

orgCenRot is the parent rotation before being rotated. And newCenRot is the parent rotation after. Subtracting gave the difference rotator, rotating towards the original rotation, which when applied to the child component during the pass would effectively remove the inherited rotation from the parent. I paired this with

UserArm->SetRelativeLocation(Focus->GetComponentTransform().InverseTransformPosition(oldCamLoc));

Focus is a middle component that is getting the parent rotation. UserArm is a child of that. This would take the old Location (acquired before the parent rotation was applied to the parent) in world space and transform it into the parents rotated local space. This puts the child component at the exact location it was in world space before the rotation took place. Leaving only the problem of inheriting the parents rotation which is resolved up above.