   First Person Camera Problem
  I've been trying the whole day to implement this.And no matter what I do it doesn't work or is weird.The problem right now is that my initial position is wrong (How do I even set the initial position and where I look at ??).And my Pitch and Yaw doesn't even do anything.Please help me there:My UpdateCamera Method:void CGraphics::UpdateCamera(){m_vec3Right = D3DXVECTOR3(1.0f, 0.0f, 0.0f);m_vec3Up= D3DXVECTOR3(0.0f, 1.0f, 0.0f);m_vec3Look= D3DXVECTOR3(0.0f, 0.0f, 1.0f);D3DXMATRIX yawMatrix, pitchMatrix;// Yaw TransformationD3DXMatrixRotationAxis(&yawMatrix, &m_vec3Up, D3DXToRadian(m_fYaw));D3DXVec3TransformCoord(&m_vec3Look, &m_vec3Look, &yawMatrix);D3DXVec3TransformCoord(&m_vec3Right, &m_vec3Right, &yawMatrix);// Pitch TransformationD3DXMatrixRotationAxis(&pitchMatrix, &m_vec3Right, D3DXToRadian(m_fPitch));D3DXVec3TransformCoord(&m_vec3Look, &m_vec3Look, &pitchMatrix);D3DXVec3TransformCoord(&m_vec3Up, &m_vec3Up, &pitchMatrix);D3DXMatrixIdentity(&m_matView);m_matView._11 = m_vec3Right.x;m_matView._12 = m_vec3Up.x;m_matView._13 = m_vec3Look.x;m_matView._21 = m_vec3Right.y;m_matView._22 = m_vec3Up.y;m_matView._23 = m_vec3Look.y;m_matView._31 = m_vec3Right.z;m_matView._32 = m_vec3Up.z;m_matView._33 = m_vec3Look.z;m_matView._41 = -D3DXVec3Dot(&m_vec3CamPosition, &m_vec3Right);m_matView._42 = -D3DXVec3Dot(&m_vec3CamPosition, &m_vec3Up);m_matView._43 = -D3DXVec3Dot(&m_vec3CamPosition, &m_vec3Look);}I'm calling the Update Method every Render Loop and afterwards set the View Matrix for the effect file:void CGraphics::Render(){if(IsDeviceResponding()){m_pD3DDev9->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);m_pD3DDev9->BeginScene();UpdateCamera();m_pD3DEffect->SetMatrix("xView", &m_matView);m_pD3DEffect->SetMatrix("xProj", &m_matProj);m_pD3DEffect->SetFloat("xTime", m_fTimeValue);// Draw SkydomeDrawSkydome();// Draw TerrainDrawTerrain();// Draw Airplane MeshDrawCharacter();// Draw Header FontDrawTextToScreen();m_pD3DDev9->EndScene();m_pD3DDev9->PresentEx(NULL, NULL, NULL, NULL, NULL);// HLSL Time Variable (Daytime switch)if(m_fTimeValue >= 1.0f){m_fTimeValue = 0.0f;// Set Time of Day in contrast to Time ValueChangeTimeOfDay();}}}These are my Input/Update Methods:void CInput::StrafeRight(float moveAmount){D3DXVECTOR3 pos = m_pCGraphics->GetCameraPosition();pos += m_pCGraphics->GetCamRight() * moveAmount;m_pCGraphics->SetCameraPosition(pos);}void CInput::StrafeLeft(float moveAmount){D3DXVECTOR3 pos = m_pCGraphics->GetCameraPosition();pos += m_pCGraphics->GetCamRight() * -moveAmount;m_pCGraphics->SetCameraPosition(pos);}void CInput::WalkForward(float moveAmount){D3DXVECTOR3 pos = m_pCGraphics->GetCameraPosition();pos += m_pCGraphics->GetCamLook() * moveAmount;m_pCGraphics->SetCameraPosition(pos);}void CInput::WalkBackward(float moveAmount){D3DXVECTOR3 pos = m_pCGraphics->GetCameraPosition();pos += m_pCGraphics->GetCamLook() * -moveAmount;m_pCGraphics->SetCameraPosition(pos);}void CInput::YawLeft(float moveAmount){m_pCGraphics->SetYaw(-moveAmount);}void CInput::YawRight(float moveAmount){m_pCGraphics->SetYaw(moveAmount);}void CInput::PitchUp(float moveAmount){m_pCGraphics->SetPitch(moveAmount);}void CInput::PitchDown(float moveAmount){m_pCGraphics->SetPitch(-moveAmount);}Also the Set Methods:void SetYaw(float amount) { m_fYaw += amount; };void SetPitch(float amount) { m_fPitch += amount; };
  Rather that rotating your forward/right/up vectors and then creating a LookAt matrix, a more generalized approach is to create a normal transformation matrix (world matrix) and then invert it to create a view matrix. So in your case you would do something like this:D3DXMATRIX rotX, rotY, world;D3DXMatrixRotationX(&rotX, D3DXToRadian(m_fPitch));D3DXMatrixRotationX(&rotY, D3DXToRadian(m_fYaw));world = rotX * rotY;world._41 = vec3CamPosition.x;  // You could also use a translation matrix insteadworld._42 = vec3CamPosition.y;  // of setting these directlyworld._43 = vec3CamPosition.z;D3DXMatrixInverse(&m_matView, NULL, &world);Having a world matrix for your camera is also pretty useful in general. For instance you can easily extract your forward/up/right vectors, and you can use it for transform hierarchies so that you can have something like a gun attached to the camera.   ;  Hmmm inverted World Matrx is a View Matrix...?Could you explain your approach to me?And even then I'm not sure if/how that fixes my problem.   ;  This is how I create a view matrix based on a position vector (x,y,z) and a rotation vector (pitch,yaw,roll). It uses D3DXMatrixLookAtLH to face the same direction as the rotation vector:D3DXMATRIX Camera::CalculateViewMatrix(D3DXVECTOR3 position, D3DXVECTOR3 rotation){D3DXMATRIX matRot, matView;D3DXVECTOR3 eye, at;D3DXVECTOR3 up = D3DXVECTOR3(0.0f, 1.0f, 0.0f);D3DXVECTOR3 look = D3DXVECTOR3(0.0f, 0.0f, 1.0f);D3DXVECTOR3 right = D3DXVECTOR3(1.0f, 0.0f, 0.0f);// Rotate yaw/pitch/rollD3DXMatrixRotationYawPitchRoll(&matRot, rotation.y, rotation.x, rotation.z);// Transform vectorsD3DXVec3TransformNormal(&look, &look, &matRot); // Look same direction as modelD3DXVec3TransformNormal(&up, &up, &matRot); // Up same orientation as modeleye = position;at = position * look;D3DXMatrixLookAtLH(&matView, &eye, &at, &up);return matView;};  That is an interesting approach.But I'm confused with the Rotation and Position you are inserting there.And how do you use that function? Where do you update it?Still nobody told me what's wrong with my code.;  Alright solved it.Interesting solutions though.But I'll stick to the rough one with simple vector adding for now.As stupid as it sounds I just forgot to initialize the values.I could kill myself right now :p   ;Quote:Original post by lipsrymeHmmm inverted World Matrx is a View Matrix...?Yes. If you think about it, it makes sense: a world matrix transforms coordinates from a local coordinate space (relative to some object's position and orientation) to an absolute world space. A view matrix transforms coordinates in absolute world coordinate space to a local coordinate space relative to the camera's position in orientation. Therefore if you have a matrix representing a camera's position and orientation, its inverse is a view matrix for that camera.I was just recommending this approach because I think it makes it easier to solve problems like the one you're having, and it also lets you have a more generalized approach to your camera relative to everything else in your scene graph.