# Forward motion

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

## Recommended Posts

I am having an issue with applying a forward motion to my camera. Basically, I know forward motion as the following equation "foward = -z (of matrix)" "velocity = foward * speed" "position = position + velocity" The problem I seem to be having is, what is the correct "z"? I built my camera's view matrix (viewMatrix) as an identity matrix and just filled out _41, _42, _43 with x, y, z. then later to get my forward matrix I do D3DXVECTOR3 forward = -D3DXVECTOR3(camera.m_viewMatrix._31, camera.m_viewMatrix._32, camera.m_viewMatrix._33); This will work fine, and if I apply forward motion I will just go straight along the z. However, that is only until I rotate the camera. I rotate my camera as
void CCamera::ViewRotateGlobalY(float fAngle)
{
D3DXMATRIX mRotation;
D3DXVECTOR4 Position(m_viewMatrix._41, m_viewMatrix._42, m_viewMatrix._43, m_viewMatrix._44);
D3DXMatrixRotationY(&mRotation, fAngle);

m_viewMatrix._41 = 0.0f;
m_viewMatrix._42 = 0.0f;
m_viewMatrix._43 = 0.0f;

D3DXMatrixMultiply(&m_viewMatrix, &m_viewMatrix, &mRotation);
m_viewMatrix._41 = Position.x;
m_viewMatrix._42 = Position.y;
m_viewMatrix._43 = Position.z;
m_viewMatrix._44 = Position.w
}

For example if I send in a 90 degrees (i convert it to radians as passed in), it will still travel along z. Am I do something wrong?

##### Share on other sites
Does anyone have an opinion on how I could solve this? Could it possibly be that I need to create my camera differently? The algorithm for forward motion should be correct.

##### Share on other sites
Make sure that you're clear on the difference between the camera view matrix and the camera model matrix.

Briefly, the two matrices are the inverses of each other. For moving the camera (rotations, moving forward and backward, etc.), it's really the model matrix you want to be working with, not the view matrix. The view matrix should simply be computed as the inverse of the model matrix; otherwise, you really shouldn't need to manipulate or modify it directly (aside from perhaps adjusting for handedness or something like that).

In your code, it looks like either you're trying to manipulate your camera by working with the view matrix directly, or that particular variable is just misnamed. In any case, I think getting clear on this aspect of things would be a good first step towards getting your camera working correctly.