Arturs Lapins
Arturs Lapins

Reputation: 81

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. So let me show you some of my code.

I don't use the D3DXMatrixLookAtLH function because i want to rotate the camera too.

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? Here is the exe if you'd like to run the program. The Exe if you'd like the code please let me know. Thank you.

Upvotes: 0

Views: 1092

Answers (1)

Gnietschow
Gnietschow

Reputation: 3180

You can easily use D3DXMatrixLookAtLH (doc) even for a fps. The eye-position of the character is 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.

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);

Upvotes: 3

Related Questions