简体   繁体   English

C ++ DirectX FPS相机怪异

[英]C++ DirectX FPS Camera Weirdness

Hey recently I have been trying to make a good working camera in DirectX 9 but I have had problems. 嘿,最近我一直在尝试用DirectX 9制作一款性能良好的相机,但是遇到了问题。 So let me show you some of my code. 因此,让我向您展示一些代码。

I don't use the D3DXMatrixLookAtLH function because i want to rotate the camera too. 我不使用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);

It creates a very weird effect. 它产生一个非常奇怪的效果。 Is there a better way to create a fps camera? 有没有更好的方法来创建fps相机? Here is the exe if you'd like to run the program. 如果您想运行该程序,则为exe文件。 The Exe if you'd like the code please let me know. exe文件 ,如果你想代码,请让我知道。 Thank you. 谢谢。

You can easily use D3DXMatrixLookAtLH ( doc ) even for a fps. 您甚至可以为fps轻松使用D3DXMatrixLookAtLHdoc )。 The eye-position of the character is pEye. 角色的眼睛位置为pEye。 For the rotation of your view you can hold a vector, which contains a normalized vector of your viewdirection. 为了旋转视图,您可以保存一个向量,其中包含视图方向的标准化向量。 This one you can transform with your rotationmatrix, an add it to the pEye for the pAt. 您可以使用您的旋转矩阵进行变换,然后将其添加到pEye的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);

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

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