I have created a simple camera class, moved it from my C# (XNA) source to C++ for d3d.

Commonly it is okay, but something is wrong with rotation. If i increase its pitch/yaw values, according to received player's mouse delta (cursor move), it rotates incorrect. It rotates 'around' 'world center', not position. 'world center' is changed if i change camera position.

enter image description here

After i move mouse left, camera is rotated like around 0;0;0 instead of own position. enter image description here

XMVECTOR m_vecPosition; // x,y,z camera pos
float m_fYaw, m_fPitch;// rotation

view matrix update function:

#define VectorForward       XMVectorSet(0,0,1,0)

XMMATRIX mtxRot = XMMatrixRotationRollPitchYaw(m_fPitch, m_fYaw, 0);
XMVECTOR vecPos = XMVector3Transform(m_vecPosition, mtxRot);
XMVECTOR vecLookAt = XMVector3Transform(VectorForward, mtxRot);//'forward' transformed
vecLookAt += vecPos;
XMVECTOR vecUp = XMVector3Transform(VectorUp, mtxRot);
m_mtxView = XMMatrixLookAtLH(vecPos, vecLookAt, vecUp);

In XNA project my camera worked fine, like in FPS games, but it is impossible to move it to C++, this code definitely not correct.

And another question about camera if some one could help me:) Do i need to limit(lock) picth,yaw abs to PI*2 ? Or i what i need to do with always increasing yaw, for example ?

My camera rotates around 0;0;0, not around its position. Rotation camera makes always its look-at in center of the world. If i move camera position, i can offset its 'relative world center'. I need to make my camera properly first person, or properly third person..

  • \$\begingroup\$ Please do post screenshots. A video link might help, too. \$\endgroup\$ Jul 27, 2013 at 19:21
  • \$\begingroup\$ Can't do it :( I updated the question.. \$\endgroup\$
    – Loryan55
    Jul 27, 2013 at 19:45
  • \$\begingroup\$ Upload the photos somewhere else and post the links. Then an editor can put them into your post for you. \$\endgroup\$ Jul 27, 2013 at 19:55
  • \$\begingroup\$ I solved my problem. Now anything is fine. I will post answer later, because "i cannot answer my own question more than one time per 8 h" or smth. \$\endgroup\$
    – Loryan55
    Jul 27, 2013 at 21:26
  • \$\begingroup\$ @Loryan55 Now you should be able to answer your own question. \$\endgroup\$ Jul 28, 2013 at 17:48

2 Answers 2


If you want the camera to rotate about itself, it stands to reason that the camera position will be the same both before & after the update. But in your code, as soon as you change your rotation matrix, you will alter the camera position so it will not appear to rotate in place like you want.

Your camera position should have nothing to do with the camera rotation. Try commenting out the line that transforms m_vecPosition. Then handle m_vecPosition independently of the camera rotation.

  • \$\begingroup\$ I've posted my code in another answer, marking your one as the solution because you are right. \$\endgroup\$
    – Loryan55
    Jul 28, 2013 at 18:32

Correct code (if someone will ever need it - first person camera):

MVector m_vecPosition;      // used as read-only current camera position
MVector m_vecTranslation;   // used for moving the camera
float m_fYaw, m_fPitch;     // rotation (Roll isn't used)

    // Pitch limitation (careful)
    #define PITCH_LIM_UP    (-XM_PIDIV2 + 0.8f) // pitch--
    #define PITCH_LIM_DOWN  (XM_PIDIV2 - 0.35f) // pitch++

View matrix update function:

// Move camera in world (using YAW rotation only):
if( !VectorEqual(m_vecTranslation, VectorZero) )
    MMatrix mtxYaw = XMMatrixRotationRollPitchYaw(0, m_fYaw, 0);
    m_vecTranslation = XMVector3Transform(m_vecTranslation, mtxYaw);
    m_vecPosition += m_vecTranslation;
    m_vecTranslation = VectorZero;

// Calculate the rotation matrix
MMatrix mtxRotation = XMMatrixRotationRollPitchYaw(m_fPitch, m_fYaw, 0);

// Calculate the new target
MVector forward = XMVector3Transform(VectorForward, mtxRotation);
MVector vecTarget = m_vecPosition + forward;

// Calculate the up vector
MVector up = XMVector3Transform(VectorUp, mtxRotation);

// View matrix
m_mtxView = XMMatrixLookAtLH(m_vecPosition, vecTarget, up);

Move and rotate functions:

void Move(MVector vecTranslation) { m_vecTranslation += vecTranslation; }

// Auto rotate camera (yaw, pitch)
void Rotate(float fYaw, float fPitch)
    // Rotate up/down has fixed limit angle:
    m_fPitch = Math::Clamp(m_fPitch + fPitch, PITCH_LIM_UP, PITCH_LIM_DOWN);

    // Avoid abs(Yaw) > XM_PI (360)
    m_fYaw += fYaw;
    if(m_fYaw > XM_PI)
        m_fYaw = -XM_PI + (m_fYaw - XM_PI);
    else if(m_fYaw < -XM_PI)
        m_fYaw = XM_PI - (-XM_PI + m_fYaw);

Rotate() is a little present from me if some one will need to make camera limited with up/down correctly, and make it possible to rotate infinite with Yaw (left/right) in bounds of XM_PI.

In my code i have some macroses or external stuff which is easy to understand what it does.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .