Given two 3D vectors A and B, I need to derive a rotation matrix which rotates from A to B.
This is what I came up with:
- Derive cosine from
acos(A . B) - Derive sine from
asin(|A x B| / (|A| * |B|)) - Use A x B as axis of rotation
- Use matrix given near the bottom of this page (axis angle)
This works fine except for rotations of 0° (which I ignore) and 180° (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.
Edit: Removed acos and asin (see Hugh Allen's post)