# how to build a view matrix for a third person game(likes " world of warcraft ")

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

## Recommended Posts

The code below was building view matrix, it used quaternion. so zhe view matrix is very useful for flight simulations. i have understand all the steps ,but i want to rebuild a view matrix for a third person game ,how to improve those code ? //************************************************************ // camera stuff //************************************************************ fRoll = fPitch = fYaw = 0.0f; D3DXQUATERNION qR; D3DXVECTOR3 vPos(0.0f, 0.0f, 0.0f); static D3DXMATRIX matView = D3DXMATRIX(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,-5.0f, 1.0f); // Process keyboard input if (m_bKey[VK_UP]) fPitch += fOneStep; // Pitch Up if (m_bKey[VK_DOWN]) fPitch -= fOneStep; // Pitch Down if (m_bKey[VK_LEFT]) fYaw += fOneStep; // Turn Left if (m_bKey[VK_RIGHT]) fYaw -= fOneStep; // Turn Right if (m_bKey['C']) fRoll += fOneStep; // Rotate Left if (m_bKey['X']) fRoll -= fOneStep; // Rotate Right if (m_bKey[VK_HOME]) vPos.z += fOneStep; // Move Forward if (m_bKey[VK_END]) vPos.z -= fOneStep; // Move Backward if (m_bKey[VK_NUMPAD4]) vPos.x -= fOneStep; // Slide Left if (m_bKey[VK_NUMPAD6]) vPos.x += fOneStep; // Slide Right if (m_bKey[VK_NUMPAD8]) vPos.y += fOneStep; // Slide Down if (m_bKey[VK_NUMPAD2]) vPos.y -= fOneStep; // Slide Up // Update position and view matricies D3DXMATRIX matR, matTemp; D3DXQuaternionRotationYawPitchRoll (&qR, fYaw, fPitch, fRoll); D3DXMatrixRotationQuaternion (&matR, &qR); D3DXMatrixMultiply (&matView, &matR, &matView); D3DXMatrixTranslation (&matTemp, vPos.x, vPos.y, vPos.z); D3DXMatrixMultiply (&matView, &matTemp, &matView); D3DXMatrixInverse (&matTemp, NULL, &matView); m_pd3dDevice->SetTransform(D3DTS_VIEW, &matTemp ); Those are intact code , my problem is that i want to do a little change on those code , and rebulid a new view matrix for a third person game ( use mouse to control camera orientation ) otherwise i don't know how to set the cursor to the center of my window each time ,would the multithreading be used ? can you teach me zhe most importent steps? (i know some multithreading knowledge about java ,but i don't know C++ multithreading) please give some useful imformation to me , I' m really Thank you !

##### Share on other sites
The code for a third person game is the same except that instead of using the orientation and position of the player, you use the orientation and position of the camera (which is no longer inside the head of the player).

1. This code:
    D3DXQuaternionRotationYawPitchRoll (&qR, fYaw, fPitch, fRoll);     D3DXMatrixRotationQuaternion (&matR, &qR);
can be replaced by this:
    D3DXMatrixRotationYawPitchRoll (&matR, fYaw, fPitch, fRoll);
There is no benefit to computing the quaternion.
2. The view matrix is basically the inverse of the camera's position/orientation matrix. It is only the last line of computation that actually computes the view matrix. The rest of the code computes the matrix containing the camera's position and orientation. You might want change your matrix names to reflect what they really contain.

##### Share on other sites
i fell your standpoint is different from others
i have read zhe the direct3d book "Beginning Diect3D Game Programming,2nd Edfition" .it says quaternion was used in almost all the third games

I'm lost now , i don't know which is right .

The other biggest difficult problem is that how to transform player(which is in front of camera ) to world
we know we only need to rotation the camera in first person game, that is very simple . but in third person game ,there is a player
in front of camera , and when zhe player turn left or right ,camera must be rotated around zhe player position (this is only my thought ), this is my difficult problem , how to do this .

Thank you !

##### Share on other sites
You're right, quaternions are used in most of the games. But you are *not* actually using quaternions, you are working with Yaw, Pitch and Roll and merely constructing a new quaternion from that each frame.

To properly make use of quaternions, you would have to totally remove your fYaw, fPitch and fRoll variables and store the orientation of the camera in a quaternion exclusively. Your camera class has to store its orientation as a quaternion, not as angles to gain any benefits from quaternions.

if (m_bKey[VK_UP]) fPitch += fOneStep; // Pitch Up

you would rotate the camera's quaternion there.

-Markus-

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634071
• Total Posts
3015340
×