使用矩阵和四元数实现三维模型的空间定位

rel="File-List" href="file:///C:%5CDOCUME%7E1%5Czjua%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">

      通过矩阵变换实现绕三个坐标轴的特定角度的旋转:

// -----------------------------------------------------------------------------
//  Desc: 设置世界矩阵
// -----------------------------------------------------------------------------
VOID SetWorldMatrix()
{
    
static long curTime=0;
    
static float elapsetime=0;
    elapsetime 
= (timeGetTime()-curTime)/1000.0f;
    curTime 
= timeGetTime();

    
//创建并设置世界矩阵
    float fRoll, fPitch, fYaw;
    fRoll 
= fPitch = fYaw = 0.0f;

    
if (m_bKey['D']) fRoll  -= 3*elapsetime;
    
if (m_bKey['A']) fRoll  += 3*elapsetime;

    
if (m_bKey['S']) fPitch -= 3*elapsetime;
    
if (m_bKey['W']) fPitch += 3*elapsetime;
    
    
if (m_bKey['Q']) fYaw   -= 3*elapsetime;
    
if (m_bKey['E']) fYaw   += 3*elapsetime;

    
//更新网格模型姿态
    static D3DXVECTOR3 vRight, vUp, vLook, vPos;

    vRight.x 
= g_matWorld._11;
    vRight.y 
= g_matWorld._12;
    vRight.z 
= g_matWorld._13;
    vUp.x    
= g_matWorld._21;
    vUp.y    
= g_matWorld._22;
    vUp.z    
= g_matWorld._23;
    vLook.x  
= g_matWorld._31;
    vLook.y  
= g_matWorld._32;
    vLook.z  
= g_matWorld._33;
    vPos.x   
= g_matWorld._41;
    vPos.y   
= g_matWorld._42;
    vPos.z   
= g_matWorld._43;

    D3DXVec3Normalize(
&vLook, &vLook);
    D3DXVec3Cross(
&vRight, &vUp, &vLook);
    
    D3DXVec3Normalize(
&vRight, &vRight);
    D3DXVec3Cross(
&vUp, &vLook, &vRight);
    
    D3DXVec3Normalize(
&vUp, &vUp);

    
static D3DXMATRIX matPitch, matYaw, matRoll;
    
    D3DXMatrixRotationAxis(
&matYaw, &vUp, fYaw);
    D3DXVec3TransformCoord(
&vLook,  &vLook, &matYaw);
    D3DXVec3TransformCoord(
&vRight, &vRight, &matYaw);

    D3DXMatrixRotationAxis(
&matRoll, &vLook, fRoll);
    D3DXVec3TransformCoord(
&vRight, &vRight, &matRoll);
    D3DXVec3TransformCoord(
&vUp,    &vUp, &matRoll);

    D3DXMatrixRotationAxis(
&matPitch, &vRight, fPitch);
    D3DXVec3TransformCoord(
&vLook, &vLook, &matPitch);
    D3DXVec3TransformCoord(
&vUp,   &vUp,  &matPitch);

    g_matWorld._11 
= vRight.x;
    g_matWorld._12 
= vRight.y;
    g_matWorld._13 
= vRight.z;
    g_matWorld._21 
= vUp.x ;
    g_matWorld._22 
= vUp.y  ;
    g_matWorld._23 
= vUp.z;
    g_matWorld._31 
= vLook.x;
    g_matWorld._32 
= vLook.y;
    g_matWorld._33 
= vLook.z;

    
//向前移动
    if (m_bKey['F'])
    
{
        g_matWorld._41 
+= 30*elapsetime * vLook.x;
        g_matWorld._42 
+= 30*elapsetime * vLook.y;
        g_matWorld._43 
+= 30*elapsetime * vLook.z;
    }


    
//向后移动
    if (m_bKey['V']) 
    
{
        g_matWorld._41 
-= 30*elapsetime * vLook.x;
        g_matWorld._42 
-= 30*elapsetime * vLook.y;
        g_matWorld._43 
-= 30*elapsetime * vLook.z;
    }

    g_pd3dDevice
->SetTransform( D3DTS_WORLD, &g_matWorld );
}

rel="File-List" href="file:///C:%5CDOCUME%7E1%5Czjua%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">

      通过四元数实现绕三个坐标轴的特定角度的旋转:


// -----------------------------------------------------------------------------
//  Desc: 设置世界矩阵
// -----------------------------------------------------------------------------
VOID SetWorldMatrix()
{
    
static long curTime=0;
    
static float elapsetime=0;
    elapsetime 
= (timeGetTime()-curTime)/1000.0f;
    curTime 
= timeGetTime();

    
//创建并设置世界矩阵 
    float fRoll, fPitch, fYaw;
    fRoll 
= fPitch = fYaw = 0.0f;

    
if (m_bKey['D']) fRoll  -= 3*elapsetime;
    
if (m_bKey['A']) fRoll  +=  3*elapsetime;
    
if (m_bKey['S']) fPitch -= 3*elapsetime;
    
if (m_bKey['W']) fPitch += 3*elapsetime;
    
if (m_bKey['Q']) fYaw   -= 3*elapsetime;
    
if (m_bKey['E']) fYaw   += 3*elapsetime;

    
//更新网格模型姿态
    D3DXQUATERNION qR;
    D3DXMATRIX matRot;
    D3DXQuaternionRotationYawPitchRoll (
&qR, fYaw, fPitch, fRoll);    
    D3DXMatrixRotationQuaternion (
&matRot, &qR);
    D3DXMatrixMultiply (
&g_matWorld, &matRot, &g_matWorld);

    
//获取网格模型前向量
    static D3DXVECTOR3 vLook;
    vLook.x 
= g_matWorld._31;
    vLook.y 
= g_matWorld._32;
    vLook.z 
= g_matWorld._33;

    
//向前移动
    if (m_bKey['F'])
    
{
        g_matWorld._41 
+= 10*elapsetime * vLook.x;
        g_matWorld._42 
+= 10*elapsetime * vLook.y;
        g_matWorld._43 
+= 10*elapsetime * vLook.z;
    }


    
//向后移动
    if (m_bKey['V']) 
    
{
        g_matWorld._41 
-= 10*elapsetime * vLook.x;
        g_matWorld._42 
-= 10*elapsetime * vLook.y;
        g_matWorld._43 
-= 10*elapsetime * vLook.z;
    }


    g_pd3dDevice
->SetTransform( D3DTS_WORLD, &g_matWorld );
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值