How do I make camera move at same speed when rotating and moving forward
Posted
by
dez
on Game Development
See other posts from Game Development
or by dez
Published on 2012-06-14T21:15:10Z
Indexed on
2012/06/15
15:28 UTC
Read the original article
Hit count: 204
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)
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
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:
ProcessMouseInput();
}
}
void MovePlayer( D3DXVECTOR3 in_vec )
{
D3DXMATRIX CameraRot;
D3DXMatrixRotationY(&CameraRot,D3DXToRadian(AngleY));
D3DXVECTOR3 CameraRotTarget;
D3DXVec3TransformNormal(&CameraRotTarget,&in_vec,&CameraRot);
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;
AngleY+=m_timeElapsed*dx*7.0f;
}
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;
D3DXMatrixRotationX(&CameraRotX,D3DXToRadian(AngleX));
D3DXMATRIX CameraRotY;
D3DXMatrixRotationY(&CameraRotY,D3DXToRadian(AngleY));
CameraRot = CameraRotX * CameraRotY;
D3DXVECTOR3 CameraRotTarget;
D3DXVec3TransformNormal(&CameraRotTarget,&CameraOrigTarget,&CameraRot);
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 );
}
© Game Development or respective owner