extract 'relative' yaw from a world rotation on a sphere


I’ve been searching, and trying to solve this for quite some time now, but can’t seem to find an answer (and hardly have any hair left to pull out).

I need to find the yaw (angle) of an objects rotation (on a sphere). I was thinking of converting the rotation to have it’s up vector at 0,0,1, so I then can use atan2 to calculate the angle. However, I’m stuck there, and not sure if that’s even the proper solution. It also needs to work around the poles.

I lack the proper knowledge of matrices and quaternions to solve this. Was hoping anybody here could help solve this.


I did find this on stackoverflow, but not sure if that’s of any use (and not sure if/how applicable to ue4 matrices)