简体   繁体   English

3D-方向矢量的旋转矩阵(向前,向上,向右)

[英]3D - Rotation Matrix from direction vector (Forward, Up, Right)

I need to get rotation matrix from direction vector (vForward) I also have vRight and vUp vectors. 我需要从方向向量(vForward)获取旋转矩阵。我也有vRight和vUp向量。 All those vectors are unit vectors. 所有这些向量都是单位向量。

I just need to get rotation matrix. 我只需要获取旋转矩阵。

To get rotation matrix for rotation in only one plane (xy) parallel to ground, I do this: XMMATRIX xmResult; 为了获得仅在平行于地面的一个平面(xy)中旋转的旋转矩阵,我这样做:XMMATRIX xmResult;

Vec3f vFwd = pPlayer->VForward;
vFwd.z = 0;
vFwd.Normalize();

xmResult = XMMatrixSet( vFwd.y,     -vFwd.x,    0,      0,
    vFwd.x,      vFwd.y,    0,      0,
    0,          0,          1,      0,
    0,          0,          0,      1);

Above code only get rotation matrix to rotate around Z axis: 上面的代码仅获得绕Z轴旋转的旋转矩阵:

I would like to get the code to rotate around all axis. 我想让代码绕所有轴旋转。 This is coordinate system I'm forced to use. 这是我被迫使用的坐标系。 I know it is strange: 我知道这很奇怪:

我正在使用的坐标系

This is how I'm using my matrix later in code: 这就是我稍后在代码中使用矩阵的方式:

XMStoreFloat3((XMFLOAT3*)&vStart, XMVector3Transform(XMLoadFloat3((XMFLOAT3*)&vStart), xmTransformation));
XMStoreFloat3((XMFLOAT3*)&vEnd, XMVector3Transform(XMLoadFloat3((XMFLOAT3*)&vEnd), xmTransformation));

根据您使用矩阵的方式,“向右”,“向上”和“向前”应对应于矩阵的行或列。

xmResult = XMMatrixSet( vRight.x, vRight.y, vRight.z, 0, vFwd.x, vFwd.y, vFwd.z, 0, vUp.x, vUp.y, vUp.z, 0, 0, 0, 0, 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM