通过矩阵变换实现绕三个坐标轴的特定角度的旋转:
//-----------------------------------------------------------------------------
// 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 );
}

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

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


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述