Direct3D 几何:两个向量的旋转矩阵
给定两个 3D 向量 A 和 B,我需要导出一个从 A 旋转到 B 的旋转矩阵。
这就是我想到的:
- Derive cosine from
acos(A . B) - Derive sine from < Strike>asin(|A x B| / (|A| * |B|))
- 使用 A x B 作为旋转轴
- 使用 此页面(轴角度)
除了0°旋转(我忽略)和180°(我将其视为特殊情况)。 是否有更优雅的方法使用 Direct3D 库来执行此操作? 我正在寻找 Direct3D 特定的答案。
编辑:删除了 acos 和 asin (请参阅 Hugh Allen 的帖子)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,你几乎已经用最好的方式做到了。 我认为没有内置的 DirectX 函数可以满足您的要求。 对于步骤 4,您可以使用
D3DXMatrixRotationAxis()
。 只需要小心边缘情况,例如 |A| 时 或|B| 为零,或者当角度为 0° 或 180° 时。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°.这可能更像是一个打字错误,而不是一个thinko,但acos(AB) 是角度,而不是余弦。 与第 2 点类似。
您可以使用 sin^2 + cos^2 = 1 从 cos 计算 sin。即 sin = sqrt(1-cos*cos)。 这比您使用的矢量表达式更便宜,并且还消除了 0/180 度的特殊情况。
It's probably more of a typo than a thinko, but acos(A.B) is the angle, not its cosine. Similarly for point 2.
You can calculate the sin from the cos using sin^2 + cos^2 = 1. That is, sin = sqrt(1-cos*cos). This would be cheaper than the vector expression you are using, and also eliminate the special cases for 0/180 degrees.
您可以查看 siggraph 链接文本
You might look at the following article from siggraph link text
也许你可以使用 D3DXMatrixLookAtLH ?
Maybe you can use D3DXMatrixLookAtLH ?