游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2049|回复: 4

如何设置矩阵中的旋转角度?

[复制链接]

75

主题

102

帖子

110

积分

注册会员

Rank: 2

积分
110
发表于 2007-9-28 11:01:00 | 显示全部楼层 |阅读模式
位移与缩放都可以在唯一一个数组的位置找到,而旋转却要数组四个的成员才成完成,如果我要实现在三个editbox(分别代表x,y,z)输入数字可以分别在不同的轴向上旋转物体,请问用矩阵应该如何完成呢?

1

主题

149

帖子

149

积分

注册会员

Rank: 2

积分
149
QQ
发表于 2007-9-28 12:30:00 | 显示全部楼层

Re:如何设置矩阵中的旋转角度?

D3DXMATRIX * D3DXMatrixRotationYawPitchRoll(
  D3DXMATRIX * pOut,
  FLOAT Yaw,
  FLOAT Pitch,
  FLOAT Roll
);

或者
D3DXMATRIX * D3DXMatrixRotationX(
  D3DXMATRIX * pOut,
  FLOAT Angle
);

D3DXMATRIX * D3DXMatrixRotationY(
  D3DXMATRIX * pOut,
  FLOAT Angle
);

D3DXMATRIX * D3DXMatrixRotationZ(
  D3DXMATRIX * pOut,
  FLOAT Angle
);

1

主题

358

帖子

358

积分

中级会员

Rank: 3Rank: 3

积分
358
发表于 2007-9-28 22:27:00 | 显示全部楼层

Re:如何设置矩阵中的旋转角度?

完整的代码(四元数),可以根据自己的需要修改!

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

1

主题

358

帖子

358

积分

中级会员

Rank: 3Rank: 3

积分
358
发表于 2007-9-28 22:30:00 | 显示全部楼层

Re:如何设置矩阵中的旋转角度? 专门给那些懒惰的初学

完整的代码(普通矩阵),可以根据自己的需要修改!

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



1

主题

358

帖子

358

积分

中级会员

Rank: 3Rank: 3

积分
358
发表于 2007-9-28 22:32:00 | 显示全部楼层

Re:如何设置矩阵中的旋转角度?

当然还有其他更简单的方式,不过还是离不开2L所说的四元数,和矩阵!!!!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-21 05:22

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表