# Angular Difference between two rotation matrices on XZ plane

As the title says, I have two rotation matrices, $R_1$ and $R_2$. Both are rotation matrices that transform from the origin coordinate system $O$ to positions $1$ and $2$ (ignoring any translation).

What I want to calculate is the angular difference on the XZ plane between the Z1 and Z2 axis.

The way I see it is I can either align the $front$ vectors of $R_1$ and $R_2$ or I have to project the $up$ vector (Z unit vector) of $R1$ to the XZ plane formed by the $up(Z-axis)-right(X-axis)$ unit vectors of $R2$.

Is my thinking correct ? The reason I am asking is because I tried both and I am not getting the expected results.

1. Option 1 - Align front vectors

• I got the front vector (3rd row) from $R_1$ and $R_2$ and calculated the cross product and dot product, got the skew symmetric matrix and then calculated the rotation $R$ using the Rodrigues formula.
• Rotated my original $R_1$ to $R_{1}^{'} = R * R_1$ and finally
• Got the angle from dot product for the up vectors (2nd row) of the $R_{1}^{'}$ and $R_2$
2. Option 2 - Project to XZ plane

• Get the up and right, vector from $R_2$ that is 1st and 2nd row and calculate the cross product to get the normals of the plane. (Dont really need to calculate as it will be the 3rd row of the rotation matrix)
• Project up vector of $R_1$ to XZ plane
• Get the angle from the dot product of the projected vector and the up vector of the $R_2$

Problem is I am seeing some rotations that I was not expecting so clearly something is wrong. By that I mean that rotation around X or Y axis should give me a $0$ rotation angle around the Z.

Any ideas ?
I can provide examples if needed, just did not want to clutter the post.

• It sounds weird to need that kind of a rotation angle -- so much so, that I would wager there is a better way to solve the underlying problem. In other words, why do you need that angle? What is the problem you intend to solve with that angle? I bet there is an easier way. – Nominal Animal May 18 '16 at 15:31
• Well, there is a reason that I need that angle. Lets consider the case where we have 2 cameras in the 3d object space. Each camera will have its own rotation matrix. Now what I need it to find out the relative "roll" of one camera to the other. More than happy to do it in a different way if there is an easier way. – xerion May 19 '16 at 3:47

## 2 Answers

I think a way to calculate what you are asking is:

• Calculate front vectors of both rotation matrices: v1 and v2

• Calculate angle between front vectors (v1 and v2) and XZ plane: angle1 and angle2

• Calculate difference between these two angles: angle1 and angle2

This is the value you are looking for.

• The angle for one of the front vectors to the XZ plane will be 90 degrees, as it will be perpendicular. For the other one it will be something else. Then the difference between these 2 angles will be the angle that shows the difference of where the cameras are point. What I want is to check if I am in landscape vs portrait mode – xerion May 20 '16 at 21:58

What I was proposing was correct. The error I had was that I was not normalizing one of my vectors and thus I had incorrect results in my dot product as I was considering that I had unit lengths.

So what I did was pretty much what I mentioned before. To recap

1. Project up vector $u_1$ of $R_1$ to the $XZ$ plane of $R_2$, the normal of $XZ$ is the the front vector $f_2$ so it is fairly easy
2. Calculate the angle between the projected point $pu_1$ and up vector $u_2$ of $R_3$

That way you can get the difference in Roll, that is portrait vs landscape rotation to make it a bit more clear.