嘿,最近我一直在尝试在 DirectX 9 中制作一个工作良好的相机,但我遇到了问题。那么让我向您展示一些我的代码。
我不使用 D3DXMatrixLookAtLH
函数,因为我也想旋转相机。
D3DXMATRIX matView,
matVTranslate,
matVYaw,
matVPitch,
matVRoll;
D3DXTranslation(&matVTranslate, x, y, z);
D3DXRotationY(&matVYaw, yaw);
D3DXRotationX(&matVPitch, pitch);
D3DXRotationZ(&matVRoll, roll);
matView = matVTranslate * (matVPitch * matVYaw * matVRoll);
d3ddev->SetTransform(D3DTS_VIEW, &matView);
它创造了一个非常奇怪的效果。有没有更好的方法来创建 fps 相机?如果您想运行该程序,这里是 exe。 The Exe如果您想要代码,请告诉我。谢谢。
最佳答案
即使是 fps,您也可以轻松使用 D3DXMatrixLookAtLH
( doc )。角色的眼睛位置是 pEye。对于 View 的旋转,您可以持有一个 vector ,其中包含您的视角方向的归一化 vector 。您可以使用旋转矩阵对其进行转换,并将其添加到 pAt 的 pEye 中。
D3DXMATRIX matView,
matLook;
D3DXMatrixRotationYawPitchRoll(&matLook,pitch,yaw,roll);
D3DXVector3 lookdir;
// yaw,pitch,roll are assumed to be absolute, for deltas you must save the lookdir
lookdir = D3DXVector3(0.0,0.0,1.0);
D3DXVec3TransformCoord(&lookdir,&lookdir,&matLook);
D3DXVector3 at;
at = camerapos + lookdir;
D3DXMatrixLookAtLH(&matView,&camerapos,&at,&up);
d3ddev->SetTransform(D3DTS_VIEW, &matView);
关于C++ DirectX FPS 相机怪异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13792772/