<p>Given two 3D vectors A and B, I need to derive a rotation matrix which rotates from A to B.</p>

<p>This is what I came up with:</p>

<ol>
<li>Derive cosine from <strike>acos</strike>(A . B)</li>
<li>Derive sine from <strike>asin</strike>(|A x B| / (|A| * |B|))</li>
<li>Use A x B as axis of rotation</li>
<li>Use matrix given near the bottom of <a href="http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToMatrix/index.htm" rel="nofollow noreferrer">this page</a> (axis angle)</li>
</ol>

<p>This works fine except for rotations of 0&deg; (which I ignore) and 180&deg; (which I treat as a special case). Is there a more graceful way to do this using the Direct3D library? I am looking for a Direct3D specific answer.</p>

<p>Edit: Removed acos and asin (see <a href="https://stackoverflow.com/questions/286274/direct3d-geometry-rotation-matrix-from-two-vectors#286300">Hugh Allen's post</a>)</p>

No, you're pretty much doing it the best way possible.  I don't think there is a built-in DirectX function that does what you want.  For step 4, you can use
```
D3DXMatrixRotationAxis()
```
.  Just be careful about the edge cases, such as when |A| or |B| is zero, or when the angle is 0° or 180°.