从矢量的Direct3D旋转矩阵,反之亦然 [英] Direct3D Rotation Matrix from Vector and vice-versa
问题描述
我需要从一个方向向量
和一个旋转矩阵的方向向量计算一个旋转矩阵。
I need to compute a rotation matrix from a direction vector, and a direction vector from a rotation matrix.
向上方向应该对应于
forward是y,right是x;
The up direction should correspond to the z-axis, forward is y and right is x;
D3DXMATRIX m; // the rotation matrix
D3DXVECTOR3 v; // this is the direction vector wich is given
D3DXVECTOR3 r; // resulting direction vector
float len = D3DXVec3Length(&v); // length of the initial direction vector
// compute matrix
D3DXMatrixLookAtLH(&m, &v, &D3DXVECTOR3(0,0,0), &D3DXVECTOR3(0,0,1));
// use the matrix on a vector { 0, len, 0 }
D3DXVec3TransformCoord(&r, &D3DXVECTOR3(0,len,0), &m);
现在,向量 r 应该等于 v ,但它isnt。
我需要做什么才能得到我需要的结果?
Now, the vector r should be equal to v, but it isnt. What exactly do I have to do to get the results I need?
推荐答案
问题是有点混乱我。使用方向矢量,您可以定义无限旋转矩阵,这将使相机指向/查看该方向,除非您定义一些额外的约束。
我将根据你的代码假设你想要向上向量 Z
: - )
The question is a bit confusing for me. Using a direction vector, you can define infinite rotation matrices that would make the camera point/look at that direction, unless you define some additional constraints.
I will assume, based on your code, that you want the up vector to be Z
:-)
首先,要小心:看起来你正在描述一个右手坐标系,但使用左手函数 D3DXMatrixLookAtLH
。
第二,这个函数的引用说:
In first place, be careful: it seems that you are describing a right-handed coordinate system, but using a left-handed function D3DXMatrixLookAtLH
.
In second place, reference for this function says that:
D3DXMATRIX* D3DXMatrixLookAtLH(
_Inout_ D3DXMATRIX *pOut,
_In_ const D3DXVECTOR3 *pEye,
_In_ const D3DXVECTOR3 *pAt,
_In_ const D3DXVECTOR3 *pUp
);
您正在创建一个查找矩阵,指定:
You are creating a look-at matrix specifying that:
- 相机位于
v
向量的末尾, - 相机寻找
[0 0 0]
- The camera is located at the end of the
v
vector, - Camera is looking towards
[0 0 0]
您确定这是正确的,
第三,将旋转矩阵应用于缩放的 Y
基本向量 [0 length 0]
。如果你想要旋转的向量 v
,我想你应该应用它缩放的 X
code> [len 0 0] 。
In third place, you are applying the rotation matrix to a scaled Y
basis vector [0 length 0]
. If you want the rotated vector to be v
, I think you should apply it to the scaled X
basis vector instead [len 0 0]
.
这篇关于从矢量的Direct3D旋转矩阵,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!