Jump to content


Mouse Capture


1 reply to this topic

#1 Pushapjit

    Member

  • Members
  • PipPip
  • 34 posts

Posted 27 September 2007 - 06:50 AM

I am developing a 3D virtual environemnt using DirectX APIs. In this virtual environment I am able to move the user camera using keyboard keys. Now, I want to move the camera using mouse but not able to do it. following is the code used for mouse capture. Problem is user camera is not moving with the mouse click.
Any Suggestion please.

// Mause Capture

	BOOL m_bCapture;

	D3DXMATRIXA16 m_matTrackBall;



m_bCapture          = FALSE;



HRESULT My3DApplication::OneTimeSceneInit()

{

	 D3DXMatrixIdentity( &m_matTrackBall );

	return S_OK;

}



HRESULT My3DApplication::FrameMove()

{


// 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 );

    }

}





LRESULT My3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,

                                    LPARAM lParam )

{

// 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;

    }

}



#2 Kenneth Gorking

    Senior Member

  • Members
  • PipPipPipPip
  • 911 posts

Posted 27 September 2007 - 01:54 PM

For reason beyond me, you are (almost) applying the rotations two times in two different functions. The kicker is, that in MsgProc() you apply the transpose of the rotation, but in FrameMove() you don't transpose it... :huh:

The two things wrong with this scenario is:
1. Matrix maths dictates that the transpose of a rotation matrix is guaranteed to be the same as its inverse, and that a matrix multiplied by its inverse will produce and identity matrix.
2. Doing the rotations twice is unnecessary, and will just make it rotate faster (if it worked) and burn CPU cycles.

So the solution is simple, remove one of the rotations :)

My suggestion would be changing FrameMove() to

HRESULT My3DApplication::FrameMove()

{

    // When the window has focus, let the mouse adjust the camera view

    if( m_bCapture )

    {

        D3DXMATRIXA16 matTrans;

        D3DXMatrixTranslation( &matTrans, 0.0f, 0.0f, 3.0f );

        D3DXMatrixMultiply( &m_matView, &m_matTrackBall, &matTrans );

    }


    return S_OK;

}


"Stupid bug! You go squish now!!" - Homer Simpson





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users