Code:
COrient3D::COrient3D()
{
m_vecRight=D3DXVECTOR3(1.0f,0.0f,0.0f);
m_vecUp=D3DXVECTOR3(0.0f,1.0f,0.0f);
m_vecLook=D3DXVECTOR3(0.0f,0.0f,1.0f);
m_vecPos=D3DXVECTOR3(0.0f,0.0f,0.0f);
m_iObjType=OBJECT_AIR;
m_fYaw=0.0f;
m_fRoll=0.0f;
m_fPitch=0.0f;
}
void COrient3D::Walk(float fUnits)
{
if (m_iObjType==OBJECT_GND)
m_vecPos+=D3DXVECTOR3(m_vecLook.x,0.0f,m_vecLook.z)*fUnits;
if (m_iObjType==OBJECT_AIR)
m_vecPos+=m_vecLook*fUnits;
}
void COrient3D::Roll(float fAngle)
{
m_fRoll+=fAngle;
D3DXMATRIX T;
D3DXMatrixRotationAxis(&T,&m_vecLook,fAngle);
D3DXVec3TransformCoord(&m_vecRight,&m_vecRight,&T);
D3DXVec3TransformCoord(&m_vecUp,&m_vecUp,&T);
}
void COrient3D::Pitch(float fAngle)
{
m_fPitch+=fAngle;
D3DXMATRIX T;
D3DXMatrixRotationAxis(&T,&m_vecRight,fAngle);
D3DXVec3TransformCoord(&m_vecUp,&m_vecUp,&T);
D3DXVec3TransformCoord(&m_vecLook,&m_vecLook,&T);
}
void COrient3D::Yaw(float fAngle)
{
m_fYaw+=fAngle;
D3DXMATRIX T;
if (m_iObjType==OBJECT_GND) D3DXMatrixRotationY(&T,fAngle);
if (m_iObjType==OBJECT_AIR) D3DXMatrixRotationAxis(&T,&m_vecUp,fAngle);
D3DXVec3TransformCoord(&m_vecRight,&m_vecRight,&T);
D3DXVec3TransformCoord(&m_vecLook,&m_vecLook,&T);
}
void COrient3D::GetRotMatrix(D3DXMATRIX *outMatrix)
{
D3DXVec3Normalize(&m_vecLook,&m_vecLook);
D3DXVec3Cross(&m_vecUp,&m_vecLook,&m_vecRight);
D3DXVec3Normalize(&m_vecRight,&m_vecRight);
D3DXVec3Cross(&m_vecRight,&m_vecUp,&m_vecLook);
D3DXVec3Normalize(&m_vecLook,&m_vecLook);
D3DXVECTOR3 vecPos=m_vecPos;
float x=D3DXVec3Dot(&m_vecRight,&vecPos);
float y=D3DXVec3Dot(&m_vecUp,&vecPos);
float z=D3DXVec3Dot(&m_vecLook,&vecPos);
(*outMatrix)(0,0)=m_vecRight.x;
(*outMatrix)(0,1)=m_vecUp.x;
(*outMatrix)(0,2)=m_vecLook.x;
(*outMatrix)(0,3)=0.0f;
(*outMatrix)(1,0)=m_vecRight.y;
(*outMatrix)(1,1)=m_vecUp.y;
(*outMatrix)(1,2)=m_vecLook.y;
(*outMatrix)(1,3)=0.0f;
(*outMatrix)(2,0)=m_vecRight.z;
(*outMatrix)(2,1)=m_vecUp.z;
(*outMatrix)(2,2)=m_vecLook.z;
(*outMatrix)(2,3)=0.0f;
(*outMatrix)(3,0)=0.0f;
(*outMatrix)(3,1)=0.0f;
(*outMatrix)(3,2)=0.0f;
(*outMatrix)(3,3)=1.0f;
}
What I've done here is take the GetViewMatrix() function from CCamera and used it to create the final rotation matrix for the object. I cannot do a D3DXMatrixRotationYawPitchRoll() because that suffers from gimbal lock which would effectively 'undo' all my axis angle rotations.