Sign in to follow this  
sansured

D3DXMatrixRotationYawPitchRoll

Recommended Posts

sansured    138
I am using D3DXMatrixRotationYawPitchRoll function for camera movement by mouse.

D3DXMATRIX mCameraRot;

D3DXMatrixRotationYawPitchRoll( &mCameraRot, m_fCameraPitchAngle, 0, m_fCameraYawAngle );


But i have a problem.Camera's up and down movement doesn't work normal.Camera rolls around x axis for up and down movement. In the beginning when i look up cam. rolls around x axis normally and looks up but when i turn to left 90 degrees and try to look up again, the cam. can't move up and down just rolls around it self. My axis is stable(dosen't move with me) this is the reason but i can't update the axis(x,y,z) with me. Do you know any good way to move camera with mouse?

Share this post


Link to post
Share on other sites
sansured    138
Thanks for the order (YAW PITCH ROLL).
I am trying to open a bsp map in my engine and i am using CFirstPersonCamera class in SDK Update August 2005 "dxutmisc.cpp, dxutmisc.h".When i opend the map in first time i saw my axis are not match with the map so my player camera looks down in the map(i am walking in the wall :) )

Here is the mouse movement code from CFirstPersonCamera class:



VOID CFirstPersonCamera::FrameMove( FLOAT fElapsedTime, IDirect3DDevice9* pd3dDevice )
{
if( DXUTGetGlobalTimer()->IsStopped() )
fElapsedTime = 1.0f / DXUTGetFPS();

if( IsKeyDown(m_aKeys[CAM_RESET]) )
Reset();

// Get the mouse movement (if any) if the mouse button are down
if( (m_nActiveButtonMask & m_nCurrentButtonMask) || m_bRotateWithoutButtonDown )
UpdateMouseDelta( fElapsedTime );

// Get amount of velocity based on the keyboard input and drag (if any)
UpdateVelocity( fElapsedTime );

// Simple euler method to calculate position delta
D3DXVECTOR3 vPosDelta = m_vVelocity * fElapsedTime * 10;

// If rotating the camera
if( (m_nActiveButtonMask & m_nCurrentButtonMask) || m_bRotateWithoutButtonDown )
{
// Update the pitch & yaw angle based on mouse movement
float fYawDelta = m_vRotVelocity.x;
float fPitchDelta = m_vRotVelocity.y;

// Invert pitch if requested
if( m_bInvertPitch )
fPitchDelta = -fPitchDelta;

m_fCameraPitchAngle += fPitchDelta;
m_fCameraYawAngle += fYawDelta;

// Limit pitch to straight up or straight down
m_fCameraPitchAngle = __max( -D3DX_PI/2.0f, m_fCameraPitchAngle );
m_fCameraPitchAngle = __min( +D3DX_PI/2.0f, m_fCameraPitchAngle );
}

// Make a rotation matrix based on the camera's yaw & pitch
D3DXMATRIX mCameraRot;

D3DXMatrixRotationYawPitchRoll( &mCameraRot, m_fCameraYawAngle, m_fCameraPitchAngle, 0 );

// Transform vectors based on camera's rotation matrix
D3DXVECTOR3 vWorldUp, vWorldAhead;
D3DXVECTOR3 vLocalUp = D3DXVECTOR3(0,1,0);
D3DXVECTOR3 vLocalAhead = D3DXVECTOR3(0,0,1);
D3DXVec3TransformCoord( &vWorldUp, &vLocalUp, &mCameraRot );
D3DXVec3TransformCoord( &vWorldAhead, &vLocalAhead, &mCameraRot );

// Transform the position delta by the camera's rotation
D3DXVECTOR3 vPosDeltaWorld;
if( !m_bEnableYAxisMovement )
{
// If restricting Y movement, do not include pitch
// when transforming position delta vector.
D3DXMatrixRotationYawPitchRoll( &mCameraRot, m_fCameraYawAngle, 0.0f, 0.0f );
}
D3DXVec3TransformCoord( &vPosDeltaWorld, &vPosDelta, &mCameraRot );

// Move the eye position
m_vEye += vPosDeltaWorld;
if( m_bClipToBoundary )
ConstrainToBoundary( &m_vEye );

// Update the lookAt position based on the eye position
m_vLookAt = m_vEye + vWorldAhead;

pd3dDevice->SetTransform( D3DTS_VIEW, &m_mView );


// Update the view matrix
D3DXMatrixLookAtLH( &m_mView, &m_vEye, &m_vLookAt, &vWorldUp );

D3DXMatrixInverse( &m_mCameraWorld, NULL, &m_mView );

}




For fixing my look and upvec. way i changed


// This
D3DXVECTOR3 vLocalUp = D3DXVECTOR3(0,1,0);
D3DXVECTOR3 vLocalAhead = D3DXVECTOR3(0,0,1);

// To This
D3DXVECTOR3 vLocalUp = D3DXVECTOR3(0,0,1);
D3DXVECTOR3 vLocalAhead = D3DXVECTOR3(0,1,0);





This fix the look way and upvec. but this time my mouse movement doesn't work good.
Do you have any idea about fixing the problem? or do i need to make my own camera class my self.
Which one looks much better to you?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this