这是你如何正确计算Vector3变换和TransformCoordinate?

时间:2011-12-30 06:55:36

标签: c++ math directx game-physics

我一直在调查D3DXVec3Transform和D3DXVec3TransformCoord函数是如何运行的,想知道这是否是数学应该是正确的方法,如果没有,有人可以解释原因。我知道已经有DirectX功能可以做到这一点,但是,我一直在尝试更多地了解幕后的数学以及我对此进行的研究。

D3DXVECTOR3 Vec3Transform(D3DXVECTOR3 &vCoordinate, D3DXMATRIX &mTransform)
{
    D3DXVECTOR3 vWorking;

    vWorking.x = (vCoordinate.x * mTransform._11) + (vCoordinate.y * mTransform._21) + (vCoordinate.z * mTransform._31) + mTransform._41;
    vWorking.y = (vCoordinate.x * mTransform._12) + (vCoordinate.y * mTransform._22) + (vCoordinate.z * mTransform._32) + mTransform._42;
    vWorking.z = (vCoordinate.x * mTransform._13) + (vCoordinate.y * mTransform._23) + (vCoordinate.z * mTransform._33) + mTransform._43;

    return D3DXVECTOR3(vWorking.x, vWorking.y, vWorking.z);
}

D3DXVECTOR3 Vec3TransformCoordinate(D3DXVECTOR3 &vCoordinate, D3DXMATRIX &mTransform)
{
    D3DXVECTOR4 vWorking;

    vWorking.x = (vCoordinate.x * mTransform._11) + (vCoordinate.y * mTransform._21) + (vCoordinate.z * mTransform._31) + mTransform._41;
    vWorking.y = (vCoordinate.x * mTransform._12) + (vCoordinate.y * mTransform._22) + (vCoordinate.z * mTransform._32) + mTransform._42;
    vWorking.z = (vCoordinate.x * mTransform._13) + (vCoordinate.y * mTransform._23) + (vCoordinate.z * mTransform._33) + mTransform._43;
    vWorking.w = 1 / ((vCoordinate.x * mTransform._14) + (vCoordinate.y * mTransform._24) + (vCoordinate.z * mTransform._34) + mTransform._44);

    return D3DXVECTOR3(vWorking.x * vWorking.w, vWorking.y * vWorking.w, vWorking.z * vWorking.w);
}

1 个答案:

答案 0 :(得分:0)

通过查看D3DX库中的类似功能,您似乎已忠实地实现了它们的功能。

使用4x4矩阵变换(x,y,z,0)的函数也可能有所帮助,因为这在转换方向时非常有用,例如对象的速度或曲面的法线。矢量将被转换而不应用翻译,这会破坏方向。