# Calculating the required rotation.

This topic is 1845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi.

Given

(A) a constant 3D vector of (0,1, 0)

(B) any normalized 3D vector

How would I calculate the 3x3 rotation matrix that when used to transform (A) will result in (B).

##### Share on other sites

Just a thought: Transform A and B into spherical coordinates, find their difference, and now you know the theta and phi rotations that must be applied to A to get B. Use theta and phi to create a 3x3 rotation matrix. There might be a better way, but that's what I'm thinking of off the top of my head.

Since A is fixed and known, you should be able to simplify it further.

Edited by Cornstalks

##### Share on other sites
This problem is ill-posed. The solution is not unique.

In case of (0,1,0) you can construct a special solution:
1. Choose two orthogonal vectors to B and to each other, call them B1 and B2.
2. Arrange these vectors into the columns of a matrix like this: R=[B2, B, B1], with B=B1 x B2.

The more general problem (any vector A) is easy as well, but still ill-posed. Edited by max343

##### Share on other sites

There is a notion of a "most natural" rotation that maps A to B. I found this link for you.

##### Share on other sites
There's indeed a most natural rotation. It's the one that leaves vectors perpendicular to the plane of A and B invariant.

To derive such a matrix, note that any composition of 2 (or any even number of) reflections is a rotation. With this, you can easily construct a matrix that satisfies your requirements.

Let S_1 be the matrix that reflects A to -A, and let S_2 be the matrix that reflects A + B to -(A + B). The composition S_2 S_1 is then your rotation matrix.

In vectors, reflecting a vector x in a normal vector n is

{eqn} x' = x - 2 n (n \cdot x) {/eqn}

or in matrix notation, where vectors are column vectors

{eqn}
x' = x - n (n^T x)
= (I - n n^T) x
{/eqn}

so your reflection matrix is then {eqn} I - n n^T {/eqn} so your rotation matrix, which rotates a to b

{eqn} R = (I - \frac{(a + b) (a + b)^T}{|a + b|^2)(I + \frac{a a^T}{|a|^2}) {/eqn}

EDIT: Can't get latex to work Edited by quasar3d

##### Share on other sites

Thanks I got it working.