Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I made a camera in DX9. To move forward I press the Up arrow. To rotate on the Y axis I use the mouse. When I perform these movements on their own the camera moves at the speed I want.

However, if I hold down Up and move the mouse at the same time then the camera moves a lot faster than it should. I want it to move at the same speed as it does when only the Up arrow is pressed.

I think I need to normalize something somewhere but not sure what and not sure where. Have tried various combinations without success so if anyone can point me in the right direction that would be great. Thanks.

I've post code below.

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

    if( KEY_DOWN(VK_UP))    MovePlayer(D3DXVECTOR3(0, 0, -1.0f));
    if( KEY_DOWN(VK_DOWN))  MovePlayer(D3DXVECTOR3(0, 0, 1.0f));

    switch( msg )
        case WM_MOUSEMOVE:

void  MovePlayer( D3DXVECTOR3 in_vec )
    D3DXMATRIX CameraRot; 
    D3DXVECTOR3 CameraRotTarget;
    CameraPos +=  (m_timeElapsed * CameraRotTarget);

void ProcessMouseInput()       
    GetCursorPos( &CurrentMouseState );

    if ((CurrentMouseState.x != GameMouseState.x) || (CurrentMouseState.y != GameMouseState.y))        
        int dx = CurrentMouseState.x - GameMouseState.x;                
        int dy = CurrentMouseState.y - GameMouseState.y;   
    GameMouseState = CurrentMouseState;
    // Set back to window center in Render function

VOID UpdateCamera()
    D3DXVECTOR3 CameraOrigTarget(0, 0, -1); 
    D3DXVECTOR3 CameraOrigUp(0, 1, 0);

    D3DXMATRIX CameraRot; 
    D3DXMATRIX CameraRotX; 
    D3DXMATRIX CameraRotY; 
    CameraRot = CameraRotX * CameraRotY;
    D3DXVECTOR3 CameraRotTarget;

    D3DXVECTOR3 CameraTarget;
    CameraTarget = CameraPos + CameraRotTarget;  

    D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
    D3DXMatrixLookAtLH( &matView, &CameraPos, &CameraTarget, &vUpVec );
    g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );

    D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f );
    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
share|improve this question

the problem is that the key functions are inside the message proc:

if( KEY_DOWN(VK_UP))    MovePlayer(D3DXVECTOR3(0, 0, -1.0f));

the MsgProc will probably be called a lot of times if the mouse is moved a lot and cause mutliple WM_MOUSEMOVE events during a single tick/frame/update.


if( KEY_DOWN(VK_UP))    MovePlayer(D3DXVECTOR3(0, 0, -1.0f));
if( KEY_DOWN(VK_DOWN))  MovePlayer(D3DXVECTOR3(0, 0, 1.0f));

should not be inside the msgproc.

You mixed up the GetAsyncKeyState function that might only be called once per frame, with the WM_KEYDOWN, WM_KEYUP events

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.