• Advertisement

Archived

This topic is now archived and is closed to further replies.

Aiming the camera in D3D IM

This topic is 6520 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have not found a tutorial on doing this. It''s not documented in the MSDN tutorials either. MSDN explains how to set the view matrix when given the camera''s position, lookat target, and up vector. So, I can move the camera around by moving both its lookat and position by the same amount. But if I just moved one, it should tilt the camera, but it doesn''t -- it totally screws up the viewport. So, can someone tell me how to do this? ~CGameProgrammer( );

Share this post


Link to post
Share on other sites
Advertisement
To tilt the camera using that model you have to change the up vector, i.e. the vector that describes the direction that the cameras top is facing.

---

Another way to place the camera is to use Euler angles: yaw, pitch and roll.

D3DVECTOR Pos;
float Pitch, Yaw, Roll;
D3DXMATRIX View, T, Rx, Ry, Rz;

D3DXMatrixTranslation(&T, -Pos.x, -Pos.y, -Pos.z);
D3DXMatrixRotationX(&Rx, -Pitch);
D3DXMatrixRotationY(&Ry, -Yaw);
D3DXMatrixRotationZ(&Rz, -Roll);

D3DXMatrixMultiply(&View, &T, &Ry);
D3DXMatrixMultiply(&View, &View, &Rx);
D3DXMatrixMultiply(&View, &View, &Rz);

pD3DDev->SetTransform(D3DTRANSFORMSTATE_VIEW, (D3DMATRIX*)&View);

To rotate the camera using this model you only need to change the euler angles. To move the cameras position is a bit more complicated but here is one way of doing it:

D3DVECTOR MoveVector;
D3DXMATRIX Rot, Rx, Ry, Rz;
D3DXMatrixRotationX(&Rx, Pitch);
D3DXMatrixRotationY(&Ry, Yaw);
D3DXMatrixRotationZ(&Rz, Roll);

D3DXMatrixMultiply(&Rot, &Rz, &Rx);
D3DXMatrixMultiply(&Rot, &Rot, &Ry);

D3DXVec3TransformCoord(&MoveVector, &MoveVector, &Rot);
Pos += MoveVector;

Here you would set the move vector to (0,0,1) if you want to move forward, (1,0,0) if you want to move right, etc.

One thing that you need to be careful with is the order of the matrix multiplication otherwise you might get unexpected results. Notice also that the rotation matrix in the second code snippet is the inverse of the first excluding the translation.

Of course, you don't have to use D3DX functions to do this, the same exist in the sample framework or you can make your own functions.

The above way isn't the most effective way, with the matrix multiplications, but since you only move the camera once per frame, you probably won't notice any optimizations made to the code.

D3DX has another function that will save you some lines of code to write: D3DXMatrixRotateYawPitchRoll(). If you want to use that one you'll need to do some experimenting on your own as I'm not 100% sure how it works. Presumably it computes the Ry*Rx*Rz concatenation. To get the Rz*Rx*Ry matrix you'll need to transpose the matrix from D3DXMatrixRotateYawPitchRoll() with D3DXMatrixTranspose().

---

Perhaps I should try to write a tutorial on this, what do you say?

Edited by - WitchLord on 4/14/00 8:41:01 AM

Share this post


Link to post
Share on other sites
That might be a good idea -- writing a tutorial. Your response was a lot of help, thanks. I''ll just trace the D3DX functions to find out how they do it, and perform the calculations myself (why not? I already do it for vertex transformations).

~CGameProgrammer( );

Share this post


Link to post
Share on other sites

  • Advertisement