0

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.

Arturs Lapins
  • 81
  • 1
  • 11

1 Answers1

3

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);
Gnietschow
  • 3,070
  • 1
  • 18
  • 28
  • Thanks but could you show me that in code because I do too much research every day I'm tired of it. Thanks. – Arturs Lapins Dec 10 '12 at 15:02
  • I've added some example code. I haven't tested it, but the general idea should be clear, hopefully :) – Gnietschow Dec 10 '12 at 16:49
  • This is not working out good could you write a simple program with one object in and a perfect working camera. If you don't want to write the simple template I can do it for you? – Arturs Lapins Dec 10 '12 at 17:21
  • I've forgotten to add the lookdir to the cameraposition in the example, to get the at. Hopefully now it's working. I'm usually coding in another language then C++, so I can't test the code or write a simple program. – Gnietschow Dec 10 '12 at 18:01
  • Thanks it works now. Would you like to explain to me what this function does `D3DXVec3TransformCoord(&lookdir,&lookdir,&matLook);` I don't mind if you don't have time but thanks for everything anyways. You were the only person to help. – Arturs Lapins Dec 10 '12 at 18:26
  • The function simply multiply the matrix with the vector :) The Coord in the name says that it handles that the matrix is 4x4, it does the extension of the vector with w=1 and the backprojection to 3D for you. There are sure many others which would have helped, but I was the first :) – Gnietschow Dec 10 '12 at 19:22