Get vector rotation in local coordinates

I have object A, which is rotated. I have a vector in world coordinates. I want to apply the vector’s rotation to object B, but as if object B were in object A’s coordinate system.

Just concatenate the rotations.

// Final Rotation is equal to our ObjectA's rotation applied first, then our world vectors rotation applied.
FQuat FinalRotation = FQuat(ObjectA.GetRotator()) * MyWorldVector.ToOrientationQuat();
ObjectB.SetActorRotation(FinalRotation); // May need to be a rotator, which FRotator can take in a Quat. 

Or are you asking for something else?

That gives me wrong result. But this is what gives me correct result:

FRotator Rotation = ObjectA->GetActorRotation().UnrotateVector(MyVector).Rotation();
Rotation = UKismetMathLibrary::ComposeRotators(Rotation, ObjectA->GetActorRotation());

I am still curious how to rewrite this using quaternions…

That looks like it’s backwards from what you are asking. You’re unrotating a vector (removing a rotation) using ObjectA. So, VectorWorldRotation - ObjectAWorldRotation = VectorRotationRelativeToObjectA

But if it works, go for it. FQuat takes in a Rotator. So it would just be

FQuat FinalRotation = FQuat(Rotation) * ObjectA->GetActorQuat();

I was curious if I can «unrotate» using quaternions.

Also, this should be the opposite:

FQuat FinalRotation = ObjectA->GetActorQuat() * FQuat(Rotation);

Ah, correct. Yea, you want the ObjectA’s rotation THEN the other rotation, not vice versa.

1 Like

This will rotate an object on yaw in tick() using FQuat
FQuat Quat1Rotation = FQuat(FRotator(WheelPitch, this->Wheel1Speed, WheelRoll));
this->GetWheel1Mesh()->AddLocalRotation(Quat1Rotation, false, nullptr, ETeleportType::None)|