Suppose we have V1 and V2. Now we need V1-> V2 rotation angles to construct a rotation matrix. There are multiple options:
(1) We can first find the dot product, and then call the ACOs function to find the angle. There is a problem here, because the value returned by ACOs is within [0, Pi], so when the angle of V1-> V2 is greater than Pi, we will not get the expected result. Assume that the angle is Theta> Pi, and V1 rotates Theta around n to get V2, which is equivalent to V1 rotating 2PI-Theta through-N to get V2. In this case, 2PI-Theta <Pi, which is within the range returned by ACOs. Therefore, the rotation angle of a vector V1-> V2 is required. We can always limit the rotation angle to [0, Pi] and obtain it by changing the direction of the rotation axis. If the rotation axis is required, you can use V1 x V2 to obtain the rotation axis. Note that the rotation angle obtained using this method will be the smallest rotation angle. Because it is always in [0, Pi.
// Assume that vec1 & vec2 are unit vector <br/> // calculate the ration matrix from vec1 to vec2 </P> <p> d3dxvector3 vec1, vec2; <br/> d3dxvector3 rotationaxis; <br/> d3cxmatrixa16 m; </P> <p> float cosval = d3dxvec3dot (& vec1, & vec2 ); <br/> d3dxvec3cross (& vec1, & vec2, & rotationaxis); <br/> d3dxmatrixrotationaxis (& M, & rotationaxis, ACOs (cosval ));
(2) because the return value of the atan2 function is in the [-Pi, Pi] function, sometimes we can directly use the atan2 function to find the rotation angle. For example, in 2D, we can find the Rotation Angle of the V1 vector and the X axis.
// Calcuate the angle from V1 to v2 in 2D Coordinate System <br/> d3dxvector2 V1, V2; <br/> // 1. calculate the angle from + X to V1 <br/> float angle1 = atan2 (v1.y, v1.x); <br/> // 2. calculate the angle from + X to V2 <br/> float angle2 = atan2 (v2.y, v2.x); <br/> // 3. calcualte the angle from V1 to V2 <br/> float angle = angle2-angle1;