Aiming the camera in D3D IM

Started by
1 comment, last by CGameProgrammer 24 years ago
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( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
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

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

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

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.

This topic is closed to new replies.

Advertisement