Camera capture with mouse

Started by
-1 comments, last by Pushapjit 18 years, 3 months ago
I am rendering a 3D model, small city. Movement of the camera with arrow keys are OK. I also want to rotate the camera view with mouse. Here is the problem. Camera updates two views: One view with the arrow keys ; the other with mouse click. For eg If I move to different location with arrow keys, now if I rotate the camera with mouse it shows original location not the new location. Second problem camera moves freely in all direction. It should only moves UP, DOWN, LEFT, RIGHT Following function is called from HRESULT CMyD3DApplication::FrameMove() function

VOID CMyD3DApplication::UpdateCamera()
{
	FLOAT fElapsedTime;

    if( m_fElapsedTime > 0.0f )
        fElapsedTime = m_fElapsedTime;
    else
        fElapsedTime = 0.05f;

    FLOAT fSpeed        = .9f*fElapsedTime;
    FLOAT fAngularSpeed = .5f*fElapsedTime;

    // De-accelerate the camera movement (for smooth motion)
    m_vVelocity      *= 0.9f;
	 
    // Process keyboard input
    if( m_bKey[VK_RIGHT] )   m_vVelocity.x   -= fSpeed; // Slide Right
    if( m_bKey[VK_LEFT] )    m_vVelocity.x   += fSpeed; // Slide Left
    if( m_bKey[VK_UP] )      m_vVelocity.z   -= fSpeed; // Move Forward 
    if( m_bKey[VK_DOWN] )    m_vVelocity.z   += fSpeed; // Move Backward 
	

    // Update the position vector
    D3DXVECTOR3 vT = m_vVelocity * fSpeed;
   D3DXVec3TransformNormal( &vT, &vT, &m_matView );
    m_vPosition += vT;
    if( m_vPosition.y < 1.0f )
        m_vPosition.y = 1.0f;

	    
    // Set the view matrix
      D3DXMatrixAffineTransformation( &m_matView, 1.25f, NULL, NULL, &m_vPosition );

	// When the window has focus, let the mouse adjust the camera view
    if( m_bCapture )
    {
        D3DXMATRIXA16 matCursor;
        D3DXQUATERNION qCursor = D3DUtil_GetRotationFromCursor( m_hWnd );
        D3DXMatrixRotationQuaternion( &matCursor, &qCursor );
        D3DXMatrixMultiply( &m_matView, &m_matTrackBall, &matCursor );

	
        D3DXMATRIXA16 matTrans;
        D3DXMatrixTranslation( &matTrans, 0.0f, 0.0f, 3.0f );
        D3DXMatrixMultiply( &m_matView, &m_matView, &matTrans );
	  m_pd3dDevice->SetTransform( D3DTS_VIEW,  &m_matView );
    }    
    m_pd3dDevice->SetTransform( D3DTS_VIEW,  &m_matView );
}


LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
                                    LPARAM lParam )
{
   // Record key presses
	if ( WM_KEYDOWN == uMsg )
	{
		m_bKey[wParam] = 1;
	}

	// Capture mouse when clicked
    if( WM_LBUTTONDOWN == uMsg )
    {
        D3DXMATRIXA16 matCursor;
        D3DXQUATERNION qCursor = D3DUtil_GetRotationFromCursor(m_hWnd );
        D3DXMatrixRotationQuaternion( &matCursor, &qCursor );
        D3DXMatrixTranspose( &matCursor, &matCursor );
        D3DXMatrixMultiply( &m_matTrackBall, &m_matTrackBall, &matCursor );

        SetCapture( m_hWnd );
        m_bCapture = TRUE;
        return 0;
    }
	
	if( WM_LBUTTONUP == uMsg )
    {
        D3DXMATRIXA16 matCursor;
        D3DXQUATERNION qCursor = D3DUtil_GetRotationFromCursor( m_hWnd );
        D3DXMatrixRotationQuaternion( &matCursor, &qCursor );
        D3DXMatrixMultiply( &m_matTrackBall, &m_matTrackBall, &matCursor );

        ReleaseCapture();
        m_bCapture = FALSE;
        return 0;
    }
    
	return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}


This topic is closed to new replies.

Advertisement