title: Calculation for View Matrix in C++ DirectX 9
body: <p>I'm trying to create a first-person shooter camera for my DirectX game. But I'm struggling in trying to get my view matrix right. At the moment I have only got a triangle to display on the screen, but when I rotate the camera and move forward it seems like the triangle is rotating around the camera instead. Here is the code I have done for the Camera Class</p>
  
  <p><strong>Camera.h</strong></p>
  
  <pre><code>#pragma once
  
  #include"Window.h"
  #include"Matrix.h"
  #include "Vector3.h"
  
  class Camera
  {
  public:
      Camera();
      Camera(float pitch, float yaw, Vector3 position);
      ~Camera();
  
      void Update();
  
      D3DXMATRIX BuildViewMatrix();
  
      Vector3 GetPosition() const { return position; }
      void SetPosition(Vector3 val) { position = val; }
  
  protected:
      float yaw;
      float pitch;
      Vector3 position;
  };
  </code></pre>
  
  <p><strong>Camera.cpp</strong></p>
  
  <pre><code>#include"Camera.h"
  Camera::Camera()
  {
      yaw = 0.0f;
      pitch = 0.0f;
  }
  
  Camera::Camera(float pitch, float yaw, Vector3 position)
  {
      this-&gt;pitch = pitch;
      this-&gt;yaw = yaw;
      this-&gt;position = position;
  }
  
  Camera::~Camera()
  {
  }
  
  void Camera::Update()
  {
      pitch = min(pitch, 90.0f);
      pitch = max(pitch, -90.0f);
  
      if(yaw &lt; 0)
          yaw += 360.0f;
  
      if( yaw &gt; 360.0f)
          yaw -= 360.0f;
  
  
      if(Window::GetKeyboard()-&gt;KeyDown(KEYBOARD_W))
      {
          D3DXMATRIX translateMat;
          D3DXMatrixTranslation(&amp;translateMat, position.x, position.y, position.z);
  
          D3DXMATRIX rotationMat;
          D3DXMatrixRotationY(&amp;rotationMat, D3DXToRadian(yaw));
  
          D3DXMATRIX forwardMat;
          D3DXMatrixTranslation(&amp;forwardMat, 0.0f, 0.0f, 10.0f);
  
          D3DXMATRIX transformMat = translateMat * rotationMat * forwardMat;
  
          position.x = transformMat._41;
          position.y = transformMat._42;
          position.z = transformMat._43;
  
          std::cout &lt;&lt; "X: " &lt;&lt; transformMat._41 &lt;&lt; std::endl;
          std::cout &lt;&lt; "Y: " &lt;&lt; transformMat._42 &lt;&lt; std::endl;
          std::cout &lt;&lt; "Z: " &lt;&lt; transformMat._43 &lt;&lt; std::endl;
      }
      else if(Window::GetKeyboard()-&gt;KeyDown(KEYBOARD_S))
      {
      D3DXMATRIX translateMat;
      D3DXMatrixTranslation(&amp;translateMat, position.x, position.y, position.z);
  
      D3DXMATRIX rotationMat;
      D3DXMatrixRotationY(&amp;rotationMat, D3DXToRadian(yaw));
  
      D3DXMATRIX forwardMat;
      D3DXMatrixTranslation(&amp;forwardMat, 0.0f, 0.0f, -10.0f);
  
      D3DXMATRIX transformMat = translateMat * rotationMat * forwardMat;
  
      position.x = transformMat._41;
      position.y = transformMat._42;
      position.z = transformMat._43;
  
      std::cout &lt;&lt; "X: " &lt;&lt; transformMat._41 &lt;&lt; std::endl;
      std::cout &lt;&lt; "Y: " &lt;&lt; transformMat._42 &lt;&lt; std::endl;
      std::cout &lt;&lt; "Z: " &lt;&lt; transformMat._43 &lt;&lt; std::endl;
      }
  
  
      if(Window::GetKeyboard()-&gt;KeyDown(KEYBOARD_Q))
      {
          yaw = yaw - 1.0f;
      }
  
      if(Window::GetKeyboard()-&gt;KeyDown(KEYBOARD_E))
      {
          yaw = yaw + 1.0f;
      }
  }
  
  D3DXMATRIX Camera::BuildViewMatrix()
  {
      D3DXMATRIX translationMat;
      D3DXMatrixTranslation(&amp;translationMat, (position.x * -1.0f), (position.y * -1.0f), (position.z * -1.0f));
  
      D3DXMATRIX yawMat;
      D3DXMatrixRotationY(&amp;yawMat, D3DXToRadian((yaw * -1.0f)));
  
      D3DXMATRIX pitchMat;
      D3DXMatrixRotationX(&amp;pitchMat, D3DXToRadian((pitch * -1.0f)));
  
      D3DXMATRIX viewMat = pitchMat * yawMat * translationMat;
  
      return viewMat;
  }
  </code></pre>
  
  <p>I'm using <code>device-&gt;SetTransform(D3DTS_VIEW, &amp;camera-&gt;BuildViewMatrix());</code> to send the view matrix to directX. What exactly am I doing wrong? Please help</p>
  
  <p><em><strong>UPDATE:</em></strong></p>
  
  <p>I have change the code and tried to use the <code>D3DXMatrixLookAtLH</code> function (doesn't use the Camera at all), but this time, it doesn't do anything - the triangle stays in the same position no matter how I change the LookAt parameter on the function. Here is the code for it:</p>
  
  <pre><code>void Renderer::Render()
  {
      device-&gt;Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(40, 40, 40), 1.0f, 0);
  
      device-&gt;BeginScene(); //Must be used as it tells DirectX we're starting to draw stuff.
  
      D3DXMATRIX worldMat;
      D3DXMatrixTranslation(&amp;worldMat, 0.0f, 0.0f, 0.0f);
      device-&gt;SetTransform(D3DTS_WORLD, &amp;worldMat);
  
      D3DXMATRIX viewMatrix;
      D3DXMatrixLookAtLH( &amp;viewMatrix, 
                          &amp;D3DXVECTOR3(0.0f, -100.0f, 1000.0f), //position
                          &amp;D3DXVECTOR3(0.0f, 1.0f, 0.0f), //Look at
                          &amp;D3DXVECTOR3(0.0f, 1.0f, 0.0f));
      device-&gt;SetTransform(D3DTS_VIEW, &amp;viewMatrix);
      //device-&gt;SetTransform(D3DTS_VIEW, &amp;camera-&gt;BuildViewMatrix());
  
      D3DXMATRIX projMatrix;
      D3DXMatrixPerspectiveFovLH( &amp;projMatrix,
                                  D3DXToRadian(45),
                                  (float)width/(float)height,
                                  1.0f,
                                  10000.0f);
      device-&gt;SetTransform(D3DTS_PROJECTION, &amp;projMatrix);
  
      device-&gt;SetFVF(VERTEXFORMAT);
      device-&gt;SetStreamSource(0, vertexBuffer, 0, sizeof(VERTEX));
      device-&gt;DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
  
      device-&gt;EndScene(); //Thank you for waiting, I have finished drawing stuff on the screen, please handle the rest Mr DirectX.
  
      device-&gt;Present(NULL, NULL, NULL, NULL);
  }
  </code></pre>