# Rotating plane to be parallel to given normal via change of basis

I have two planes and their respective normals. I would like to rotate the second plane, planeB, so that it is parallel to the first, planeA.

To do this, I am using a change of basis to rotate each vector of planeB. To create the new axes, I do the following:

``````cAxis = planeA.normal;
aAxis = planeA.normal parallel to yAxis ? then xAxis else yAxis;
bAxis = aAxis cross cAxis
``````

I create a rotation matrix from these new axes as such:

``````| aAxis.x  aAxis.y  aAxis.z |
| bAxis.x  bAxis.y  bAxis.z |
| cAxis.x  cAxis.y  cAxis.z |
``````

I then multiply each vector of planeB by the rotation matrix to get their new coordinates. I think that this is the correct method for rotating the plane, but I don't believe the new axes that I am creating are correct. The results are correct when planeA's normal is parallel to the z axis, but not otherwise. Is there a standard formula for creating the new axes?

-
To make all world vertices come to camera space we transform them with a basis formed by the camera's view, up and side vector expressed IN world coordinates. Likewsie, to transform points/vectors of plane B to A the rotation we need should be a set of basis IN plane A coordinates. Using this idea, I think aAxis seems fine, what I don't understand is the bAxis which seems to be in some other system's x or y-axis, cAxis seems fine too. – legends2k Mar 12 '13 at 17:59
@legends2k I'm not sure if you meant that you don't understand aAxis, which is the one with the x-axis or y-axis, or that bAxis (the one with the cross product) is incorrect. Could you please clarify for me? – B A Mar 12 '13 at 22:43
Oops, sorry, I meant aAxis only – legends2k Mar 13 '13 at 18:01

I see a few potential gaps in your methods. One might be that you are using (presumably) constant axes for your `aAxis` calculation, `xAxis` and `yAxis`. I would guess those are the absolute world axes X and Y. That might be ok, if your specific scenario only uses planes oriented to X or Y. That's where you go awry.

### To rotate from one direction to another, cross those angles

You cross one plane's normal and an absolute axis. That's not right.

``````vector3 axis = planeA.normal cross planeB.normal;
``````

However, this `axis` is only a vector with the same direction as the axis of rotation. A rotation axis needs to be a unit vector.

### Cross products need to be normalized

The result of any cross-product, `bAxis` or other, will only be a unit vector if both crossed vectors are unit vectors AND they are perpindicular to each other. This is a result of this property of cross products (and the fact that sin(90 degrees) = 1):

``````length(A x B) = sin(angle_between(A, B)) * length(A) * length(B)
// so if A and B are unit vectors (have length = 1)
length(A x B) = sin(angle_between(A, B))
// and if that length is 1...
1 = sin(angle_between(A, B)) = sin(90 degrees)  // A and B were perpindicular
``````

(Side note, you might be able to get a unit vector by crossing a weird combination of non-unit vectors and an angle that coincidentally still adds up to magnitude 1. But that's not the point, and both your planes' normal vectors ought to be unit vectors anyway.)

### Rotation matrices are hard to build by hand

If you had normalized your axis, your matrix would still be very wrong. They are not constructed directly from the components of the participating vectors. This is a rotation matrix (again from wikipedia):

Moral of the story: use a library to construct your rotation matrix. You'll thank yourself later.

### The correct axis and angle rotation matrix

``````// length(A x B) = sin(angle_between(A, B)