Given some normal
vectorB there is an
infinite number of rotations (and hence infinite combinations of Euler angles) that would rotate any reference
vectorA to it -- there is no
unique solution for the information you want. To compute a unique solution you need more information than just the normal, for example not just
vectorA but also what is
up,
right, and
forward in its frame and how that should relate to the cardinal axes in B's frame.
That said, if all you know is
vectorA and
vectorB then typically there
is a unique rotation that brings A to B with the
shortest angle of rotation. Computing the
quaternion for that rotation is done like this:
Code: Select all
btVector3 axis = (vectorA.cross(vectorB)).normalized();
btScalar angle = btAcos(vectorA.dot(vectorB)) / (vectorA.length() * vectorB.length());
btQuaternion Q = btQuaternion(axis, angle);
Note that there is much normalization and length measuring going on, so the algorithm could be optimized if you are sure the two vectors are normalized. Also it breaks when the two vectors are parallel or anti-parallel so you should check for that case before using it.
Personally, I'd prefer to have the quaternion rather than the Euler angles, but if you want to decompose into Euler angles then you could do:
Code: Select all
btMatrix3x3 M = btMatrix3x3(Q);
btScalar yaw, pitch, roll;
M.getEulerXYZ(yaw, pitch, roll);
Note that there is more than one "definition" of Euler angles and the
btMatrix3x3 class provides a few different ways to decompose. Look at the
documentation to find the one you want to use.